I am creating, writing to and reading from a shared segment on Linux using the ftok/shmget/shmat/shmdt functions.
If I write to the segment in one program which then exits, and subsequently read the segment from another program later, I am surprised to find the data still there.
I would have expected that when the last process sharing a segment does a shmdt, the segment would be free'd.
Can I rely on this behavior? Or is it analogous to continuing to use a pointer after free()'ing it?
The shared memory area remains until it is deleted via shmctl(shmid,IPC_RMID,...)
[or system reboot]. This will take effect after every process has done shmdt
[or terminated].
The semantics are very similar to a file. shmat
is like open
. shmdt
is like close
and the [above] shmctl
is like unlink
.
If you close a file, you'd like it to persist until specifically deleted, wouldn't you? So, shared memory segments operate similarly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With