Semaphores

Dear Reader,

I'm in a multiprocess environment working with shared mem and semaphores as mutex.. The problem is -- If one of the process hooked up with the semaphore and accessing the shared mem, terminates abruptly ( or got killed ), other process which are in want of the semaphore are waiting indefnitely...

Is there any way to come out this state....

Thanks in advance..

When programming such IPC's, you should consider 2 things...

  1. Handle all signals so that your exiting process cleans up.
  2. Use non-blocking techniques to assure no dead-lock.

The semaphore documentation states...

There are two control flags that can be used with semop():

IPC_NOWAIT
-- Can be set for any operations in the array. Makes the function return without changing any semaphore value if any operation for which IPC_NOWAIT is set cannot be performed. The function fails if it tries to decrement a semaphore more than its current value, or tests a nonzero semaphore to be equal to zero.
SEM_UNDO
-- Allows individual operations in the array to be undone when the process exits.

Any process with read permission can test whether a semaphore has a zero value. To increment or decrement a semaphore requires write permission. When an operation fails, none of the semaphores is altered.

The process blocks (unless the IPC_NOWAIT flag is set), and remains blocked until:

the semaphore operations can all finish, so the call succeeds,
the process receives a signal, or
the semaphore set is removed.