Home > Back-end >  Questions about the Linux mmap function
Questions about the Linux mmap function

Time:10-06

Using the mmap function for the communication between two unrelated process, the data can only be read once?

- the following code is read process -- -- -- -- --
Int main (int arg c, char * argv [])
{
Int fd=open (" temp ", O_RDWR | O_CREAT, 0664); The ftruncate (fd, 4096); Int len=lseek (fd, 0, SEEK_END); Void * PTR=mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0).
If (PTR==MAP_FAILED)
{perror (" mmap "); exit(1);
} the while (1)
{sleep (1); Printf (" % s \ n ", (char *) PTR + 1024);
}//release
Int ret=munmap (PTR, len);
If (ret==1)
{perror (" munmap "); exit(1);
} return 0;
}

-- -- -- -- -- - the following is the write process code -- -- -- -- -- --

Void * PTR=mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0).
If (PTR==MAP_FAILED)
{
Perror (" mmap ");
exit(1);
}

While (1)
P={char * PTR (char *); P +=1024;
Strcpy (p, "hello, parent, I am your friend!!! \n");
Sleep (2);
}

//release
Int ret=munmap (PTR, 4096);
If (ret==1)
{
Perror (" munmap ");
exit(1);
}

return 0;

A line will output a blank line hello parent, I am your friend!!!!!!
Why is the output gap? Wrote in PTR + 1024 location data, after I read what is missing? Still hope leaders help to reassure,

CodePudding user response:

Seek to reassure to reassure

CodePudding user response:

Is there a big help to have a look
  • Related