In shared memory system, when two processes share a shared memory segment, after one process is killed, how will the other running process be notified of the killed process by the shared memory system ?
-
On which unix variants, and which shared memory system?– Gilles 'SO- stop being evil'Jan 31, 2012 at 23:45
-
There is no "built-in" asynchronous notification if that's what you're looking for. How are processes "killed?" legacy code? new code? I've always used a control struct located in shm, each and any process is responsible for updating their status with a timestamp, other processes, or a controlling "agent" monitor and respond to changes.– bsdFeb 1, 2012 at 13:25
2 Answers
The only way you could handle this would be to have some kind of heartbeat system between the processes. For instance the first process could share its PID in shared memory.
Subsequent processes could announce themselves by sending a signal such as SIGUSR1. Then they could keep sending SIGUSR2 at intervals as a heartbeat. Two missing SIGUSR2 signals in a row would tell you that the process had died. You would need to use sigqueue(2) sigaction(2) to communicate the PID of the signal sender for this to work.
Another way is to arrange for the other sharing processes to be children of the first process to access the shared memory. Then the death of a child results in an automatic SIGCHLD to the parent. Even if you currently have the sharing processes started independently, you could change those to signal the first process that a new process wants to share memory. Then the first process could fork a new child to take over the activity of the process that wants to share memory, thus preserving the parent/child relationship.
Personally, I would first look at a more modern IPC method such as 0MQ (go here and read the guide) before I would try to implement this with UNIX kernel services. I would even give up on shared memory, and use memcache to share the data. In fact I have implemented a system with several processes using 0MQ for heartbeats and IPC and sharing memory via memcache.
It isn't. Processes can attach and detach to the shared memory segment at any time and no notification is given to other attached processes ( if there even are any ).