Is there any way, linux specific or not, to have posix shared memory segments (obtained with shm_open()) removed when no process is using them. i.e. have them reference counted and have the system remove them when the reference becomes 0
A few notes:
-
Establishing an atexit handler to remove them doesn’t work if the program crashes.
-
Currently, the linux specific way, I embed the pid in the segment name, and try to find unused segments by walking /dev/shm in an external program. Which has the drawback of having to periodically clean them up externally in a rather hackish way.
-
As the program can run multiple copies, using a well defined name for the segment that the program reuses when it starts up is not feasible.
No – at lest on Linux, the kernel doesn’t contain anything that can do this. It’s up to some application to call shm_unlink() at some point to get rid of a shared memory segment.