How to clean after SIGKILL?

I am working on a program that uses shared memory. Several instances of the specified program will either connect to the existing one, or create it again, and return it to the OS when there are no other processes, or simply disconnect it and complete it. I thought of using a simple counter to keep track of how many processes are using it.

I use the atexit() function to clear, but afaik, when I receive a SIGKILL signal, the processes will not clear, so if any of these processes does not finish normally, I may never be able to clear the memory.

Is there a way to indicate what to do even after a SIGKILL signal? I'm probably going to write some kind of timer-like mechanism to check if all processes are still alive, but I really would like to avoid this if there is another way.

+7
c linux signals
source share
3 answers

No, SIGKILL cannot be caught in any way by your application - if possible, the application can ignore it, which can lead to its purpose.

+16
source share

You cannot catch SIGKILL.

However: you can still clean, provided that the cleaning is performed by another process. There are many strategies you can go here for your housekeeping process to see your other processes appear and disappear.

For example: you may have a Unix domain socket in a known place that the servant is listening to; each slave process opens a socket to indicate it using a shared memory segment. When the slave exits, for some reason the socket will be closed. The housekeeper can see it and do the cleaning.

+13
source share

In combination with shared memory, reliable mutexes located in the shared memory segment will be a great tool. If the process freezes when a trusted mutex is locked, the next lock attempt process will receive EOWNERDEAD and can perform the cleanup that the original owner should have done.

+4
source share

All Articles