tags:

views:

96

answers:

2

Hi.

I'm working on a program which uses shared memory. Multiple instances of said program will either connect to an existing one or create it anew, and give it back to OS when there are no other processes or just detach it and terminate. I thought of using a simple counter to keep track of how many processes use it.

I'm using atexit() function to do the cleanup, however, afaik, upon receiving SIGKILL signal, processes won't do any cleanup, so if any of those processes don't terminate normally, I might never be able to clean the memory.

Is there a way to specify what to do even after a SIGKILL signal ? I'm probably going to write some mechanism similiar to a timer to check if processes are still alive, but I'd really like to avoid it if there is another way.

Thanks.

+8  A: 

No, SIGKILL cannot be caught in any way by your application - if it could, the application could ignore it, which would defeat its purpose.

anon
+4  A: 

You can't catch SIGKILL.

However: you can still do cleanup, provided that cleanup is done by another process. There's lots of strategies you can go with here to let your housekeeping process see your other processes appear and disappear.

For example: you could have a Unix domain socket in a known location, which the housekeeper listens to; each slave process opens the socket to indicate it's using the shared memory segment. When a slave exits, for whatever reason, the socket will get closed. The housekeeper can see this happen and can do the cleanup.

David Given
+1 for explaining a concept
Robert