Is it possible to turn a shared memory segment into private memory?

Let's say I have a c-program (in linux environment) that uses shared memory to send data to and from several processes. Say later, the program ends parallel processes, and I have only one process. Now, but I want fork () to disable another process, however this time I do not want this memory segment to be shared, I want the parent and child processes to be able to change values ​​without affecting each other, as if it were private memory. Is there any way to do this; convert shared memory to private memory, but occupy the same space in virtual memory or make a copy of shared memory?

+3
source share
1 answer

Well, the only way I can come up with a portable POSIX API is to have the child map a new segment of the same size somewhere else (random), copy the data and then separate the original segment and reattach the new segment to the correct address. That sounds ugly.

You can disconnect a new segment after you do this so that other people do not join it.

Now, when I look at the man page, if you have an FD for the shm object, you can try rewinding the shm object as MAP_PRIVATE to a child at the right address. However, `` It is not indicated whether changes made to the file after calling mmap () will be visible in the displayed area. '', Therefore, you either need to check this, or it is dangerous to live, or use other equipment.

+3
source

All Articles