问题
I am working on a university assignment, based largely around IPC and shared memory. The problem is, as a complete noob to C, I've been happily testing my app (which uses shmget
and shmat
obviously) for hours. As you can probably guess, I've not been cleaning up after myself, and now I can't run my app, because (I assume) shmget
cant allocate anymore resources.
My question is: how can I get this resource back without restarting OSX, and is there a GUI tool or something I can use to monitor/manage this shared memory I am creating?
回答1:
Call shmdt ("shared memory detach") on the shared memory segment in each process that holds a reference to it. Unix shared memory sections are reference counted, so when the last process detaches from them, they can be destroyed with shmctl(id, IPC_RMID, NULL).
From outside your application, the only option I can think of right now to clear your shared memory segments is:
for (int id=0; id < INT_MAX; id++)
shmctl(id, IPC_RMID, NULL);
but this is a horribly inefficient kludge. (I'm also not sure if it works; it doesn't on Linux, but Linux violates the Unix standard while MacOS X is certified against it.)
回答2:
Perhaps a bit late, but there are cmdline tools available to do exactly this. ipcs and ipcrm
take a look at their man pages.
来源:https://stackoverflow.com/questions/4278391/managing-shared-memory-in-c-on-osx