Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to interprocess memory if one of the processes dies unexpectedly?

if you are interested in motivation Ill elaborate it in next few sentences, if not just skip to the Q.

I was thinking about making fast logger but the one that is not affected when program crashes(aka few last log msgs arent lost). So my idea is to write to the shared memory(ringbuffer) and have another low prio process read from it and do the dumping. But for that to work I need to know what happens to shared memory if one process exits(normal exit, SEGFAULT)...

So my question is: What happens to shared memory (in Linux, but you can A for Win also) when one of the procs die? Is it UB?

like image 977
NoSenseEtAl Avatar asked Dec 20 '22 09:12

NoSenseEtAl


2 Answers

What happens to shared memory (in Linux, but you can A for Win also) when one of the procs die?

Nothing. When a process dies, the shared memory is left as it is. It is mapped as a file under /dev/shm/ directory. It is removed either when the system reboots, or when all processes unmmap the shared memory file and the shm_unlink() is called.

Is it UB?

No, it is well defined. See the man page for shm_overview(7) :

POSIX shared memory objects have kernel persistence: a shared memory object will exist until the system is shut down, or until all processes have unmapped the object and it has been deleted with shm_unlink(3)

like image 173
BЈовић Avatar answered Dec 24 '22 00:12

BЈовић


Read boost IPC mechanisms, they explain persistence concept about shared memory.

http://www.boost.org/doc/libs/1_53_0/doc/html/interprocess/some_basic_explanations.html#interprocess.some_basic_explanations.persistence

like image 38
JP Cordova Avatar answered Dec 24 '22 01:12

JP Cordova