Question

I'm sharing memory between a parent process and multiple children processes by allocating shared memory segments with shm_open/mmap in OS X. Either parent or children may create the segment then communicate the identifying name to either. My understanding is that the parent has to call shm_unlink on each of these segments when it quits to cleanup memory, otherwise the shared memory is permanently leaked.

What I had initially thought from reading the documentation is that shared segments are cleaned up when no processes with it mapped are alive. However experiments show that this isn't the case and someone has to explicitly use shm_unlink.

Is there any way in OS X to list all the currently existing shared memory segments? The problem is that the parent may crash and so not have a chance to call shm_unlink. In Linux my solution is to clean out /dev/shm, but in OS X I would need some way of listing open shared segments.

Was it helpful?

Solution

The answer seems to be: you can't.

First, see this question, which quotes a comment in the kernel:

  • TODO:
    (2) Need to export data to a userland tool via a sysctl. Should ipcs(1) and ipcrm(1) be expanded or should new tools to manage both POSIX kernel semaphores and POSIX shared memory be written?

Also see this post on the Apple mailing list unix-porting:

There is no "picps"/"picprm" utility, you are expected to remember what 
you create and clean up afterward, or clean up first thing on 
restart if you crash a lot, there is nothing exposed directly 
in the filesystem namespace, and you are expected to do 
the shm_unlink because it is a rendezvous for potentially a 
lot of unrelated programs.

OTHER TIPS

Hope you figure out your problem. you can use ipcs -a and look under the heading Shared Memory for NATTCH. that value will tell you how many guys are attached to a particular id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top