#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#define max_size 100
int *first_Table;
void *test_memory;
void init_Random()
{
srand(time(NULL));
}
int is_First(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
++sum;
if(sum==2)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
void init_Table()
{
int i,j;
first_Table=malloc(max_size);
i=0;
j=2;
do
{
if(is_First(j))
{
first_Table[i]=j;
++i;
}
++j;
}while(i<max_size);
}
int random_from_Table()
{
int value;
value=rand()%max_size;
return first_Table[value];
}
void delete_Table()
{
free(first_Table);
}
int file;
void open_File(char *name)
{
file=open(name,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
}
void lseek_File()
{
lseek(file,0x200,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
test_memory=mmap(0,0x200,PROT_WRITE,MAP_SHARED,file,0);
}
void write_Memory()
{
close(file);
int i;
i=random_from_Table();
sprintf((char*)test_memory,"%d\n",i);
}
int main(int argc, char **argv)
{
init_Random();
init_Table();
char file_name[20];
scanf("%s",file_name);
open_File(file_name);
lseek_File();
write_Memory();
munmap(test_memory,0x200);
delete_Table();
return 0;
}
Brak komentarzy:
Prześlij komentarz