1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
void abort(const char* message)
{
perror(message);
exit(1);
}
void read_data(const char* filename,int nb_int)
{
int data;
int fd=open(filename,O_RDONLY);
for (int i=0; i<nb_int; ++i)
{
pread(fd,&data,sizeof(int),sizeof(int)*i);
printf("data[%d]=%d\n",i,data);
}
close(fd);
}
int main(int argc,char* argv[])
{
char filename[1024];
char arraytmp[1024];
const int nb_int=100;
const int len=nb_int*sizeof(int);
int shfd;
int* buffer;
if ((shfd=shm_open("test_rafix",O_RDWR|O_CREAT,0766))==-1)
abort("shm_open");
// Read out the link to our file descriptor
sprintf(arraytmp,"/proc/self/fd/%d",shfd);
memset(filename,0, sizeof(filename));
readlink(arraytmp,filename,sizeof(filename)-1);
printf("Filename : %s\n",filename);
if (lseek(shfd,len-1,SEEK_SET)==-1)
abort("lseek");
if ( write(shfd,"",1)==-1)
abort("lseek/write");
if ((buffer=(int*)mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,shfd,0))==MAP_FAILED)
abort("mmap");
// from 150 to 249 in buffer
for (int i=0; i<nb_int; ++i)
buffer[i]=150+i;
if (msync(buffer,len,MS_SYNC))
abort("msync");
// Read data in a deep part of the code...
read_data(filename,nb_int);
munmap(buffer,len);
close(shfd);
if (shm_unlink(filename)==-1)
abort("unlink");
return 0;
} |
Partager