poniedziałek, 16 września 2019

Map, munmap, lseek (example gcc)


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#define max 100
#define FILE1 0x100
#define FILE2 0x200
#define FILE3 0x300
#define FILE4 0x400
#define FILE5 0x500
#define FILE6 0x600
int is_P(int a,  int b,  int c)
{
int delta;
delta=(b*b)-(4*a*c);
if(delta>=0)
return 1;
else
return -1;
}
int plus_Minus()
{
int x,y;
x=rand()%2;
if(x==0)
y=-1;
else
y=1;
return y;

}

int main(int argc, char **argv)
{
char file_Name[20];
void *mem1,*mem2,*mem3,*mem4,*mem5,*mem6;
int a,b,c,delta,e,file;
double x1,x2;
srand(time(NULL));
do
{
a=(rand()%max+1)*plus_Minus();
b=(rand()%max+1)*plus_Minus();
c=(rand()%max+1)*plus_Minus();

}while(is_P(a,b,c));
delta=(b*b)-(4*a*c);
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
strcpy(file_Name,"math_values");


file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE1+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem1=mmap(0,FILE1,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem1,"%d\n",a); munmap(mem1,FILE1);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE2+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem2=mmap(0,FILE2,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem2,"%d\n",b); munmap(mem2,FILE2);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE3+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem3=mmap(0,FILE3,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem3,"%d\n",c); munmap(mem3,FILE3);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE4+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem4=mmap(0,FILE4,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem4,"%d\n",delta); munmap(mem4,FILE4);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE5+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem5=mmap(0,FILE5,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem5,"%f\n",x1); munmap(mem5,FILE5);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE6+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem6=mmap(0,FILE6,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem6,"%f\n",x2); munmap(mem6,FILE6);









return 0;
}

Brak komentarzy:

Prześlij komentarz