#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define max 1000
void init_Random()
{
srand(time(NULL));
}
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return 1;
else
return -1;
}
int *tab,fd;
void *take;
char *test_file="first_numbers2";
void init_Tab()
{
int i,j;
tab=malloc(max*sizeof(int));
i=0;
j=2;
do
{
if(is_First(j)==1)
{
tab[i]=j;
++i;
}
++j;
}while(i<max);
}
void make_File(int file)
{
lseek(file,0x100+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
}
int main(int argc, char **argv)
{
int i;
init_Random();
init_Tab();
fd=open(test_file,O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
make_File(fd);
take=mmap(0,0x100,PROT_WRITE,MAP_SHARED,fd,0);
close(fd);
for(i=0;i<max;i++)
{
sprintf((char*)take,"%d\n",tab[i]);
}
munmap(take,0x100);
return 0;
}