How to do a cleanup after SIGKILL?

后端 未结 3 1523
日久生厌
日久生厌 2020-12-31 16:53

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

相关标签:
3条回答
  • 2020-12-31 17:19

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

    0 讨论(0)
  • 2020-12-31 17:33

    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.

    0 讨论(0)
  • 2020-12-31 17:33

    Combined with shared memory, robust mutexes located in the shared memory segment would be a great tool. If a process dies while holding a lock on a robust mutex, the next process to attempt locking it will get EOWNERDEAD and can perform the cleanup the original owner should have performed.

    0 讨论(0)
提交回复
热议问题