Semaphore help

Semaphore help

Post by Pino Venturell » Thu, 08 Oct 1998 04:00:00

I have a piece of code similar to yours but there is something not very
clear to me... who will remove the semaphore?

I added a function like:

void removeSem ()
  semctl (v_semid, 0, IPC_RMID, 0);


and in the main I added:

signal (SIGINT,  removeSem);
signal (SIGHUP,  removeSem);
signal (SIGQUIT, removeSem);
signal (SIGTERM, removeSem);
signal (SIGKILL, removeSem);
signal (SIGSTOP, removeSem);

In such a way the ipc semaphore is removed in almost all the cases but
if I run a kill -9 to kill the program this signal is not intercepted,
the program stops BUT if I run a ipcs -s I see that the just created
semaphore is present... and if I run this several time I can have lot
of  not needed ipc semaphores (and this can be dangerous!!)

Do you have any suggestion in order to solve this?
Is it possibe to create an 'exit routine' which is called every time I
quit my program?

Ciao and thanks for any help, Pino