I have multiple apps compiled with g++, running in Ubuntu. I'm using named semaphores to co-ordinate between different processes.
All works fine except in the following situation: If one of the processes calls sem_wait()
or sem_timedwait()
to decrement the semaphore and then crashes or is killed -9 before it gets a chance to call sem_post()
, then from that moment on, the named semaphore is "unusable".
By "unusable", what I mean is the semaphore count is now zero, and the process that should have incremented it back to 1 has died or been killed.
I cannot find a sem_*()
API that might tell me the process that last decremented it has crashed.
Am I missing an API somewhere?
Here is how I open the named semaphore:
sem_t *sem = sem_open( "/testing",
O_CREAT | // create the semaphore if it does not already exist
O_CLOEXEC , // close on execute
S_IRWXU | // permissions: user
S_IRWXG | // permissions: group
S_IRWXO , // permissions: other
1 ); // initial value of the semaphore
Here is how I decrement it:
struct timespec timeout = { 0, 0 };
clock_gettime( CLOCK_REALTIME, &timeout );
timeout.tv_sec += 5;
if ( sem_timedwait( sem, &timeout ) )
{
throw "timeout while waiting for semaphore";
}