wtorek, 7 maja 2019

mmap/munmap gcc

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define size_Value 1000
#define size_F 0x100

int *first_Tab;
double *sqrt_Tab;
void *use_Func1;
void *use_Func2;
int file_Date;
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 0;
}
void init_File(char *name_File)
{
file_Date=open(name_File,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
lseek(file_Date,size_F+1,SEEK_SET);
write(file_Date,"",1);
lseek(file_Date,0,SEEK_SET);
}
void init_Tab()
{
int i,j;
srand(time(NULL));
first_Tab=malloc(size_Value*sizeof(int));
sqrt_Tab=malloc(size_Value*sizeof(double));
i=0;j=2;
do
{
if(is_First(j))
{
first_Tab[i]=j;
sqrt_Tab[i]=sqrt(1.0*j);
}
++j;
}while(i<size_Value);
}
int read_Int(int value)
{
return first_Tab[value];
}
int read_Double(int value)
{
return sqrt_Tab[value];
}
int random_Result()
{
int value;
value=rand()%size_Value;
}
void destroy_Tables()
{
free(sqrt_Tab);
free(first_Tab);
}

int main(int argc, char **argv)
{
int i;
char *name="test_mmap";
init_Tab();
i=random_Result();
init_File(name);
use_Func1=mmap(0,size_F,PROT_WRITE,MAP_SHARED,file_Date,0);
use_Func2=mmap(0,size_F,PROT_WRITE,MAP_SHARED,file_Date,0);
close(file_Date);
sprintf((char*) use_Func1,"%d\n",read_Int(i));
sprintf((char*) use_Func2,"%.2f\n",read_Double(i));
destroy_Tables();
munmap(use_Func2,size_F);
munmap(use_Func1,size_F);

return 0;
}

Brak komentarzy:

Prześlij komentarz