
On Tue, Jul 29, 2008 at 02:53:01PM +0530, Sachin Garg wrote:
When using semaphores to synchronize separate processes, everything works fine when each process exits nicely (closing its semaphores before exit). But things get really messy when a process might crash. I am unable to figure out how to recover from such a crash which leaves semaphores in inconsistent state.
If a semaphore is not in-use (open) by any process, in this case (in my application) I can safely 'remove' it and start afresh. Is there some way to find out if any process is using a semaphore at a time so that I can call 'remove'?
When I just add a 'remove' on process start this works great on windows (as remove just fails if another process has the semaphore open), but on linux sem_unlink is used which has the behavior of deleting it even if its in use.
What is the general practice when it comes to cleaning up semaphores after process crashes? Maybe some way to ensure that 'post' and 'close' are always called even when application has otherwise crashed? Is there some way to use boost's windows style semaphores on linux instead of native posix style?
I tried looking and many have asked this question (in context of recovering from posix semaphores, which are used by boost on linux), but I couldn't find any answers. Lars had asked this here also, almost an year ago but no answers in that thread either. This seems like a basic issue but am totally lost on how to even approach it.
Sachin Garg
Hi Sachin ipcs -s -p will show a list of semaphores and the associated pids of the process which created them. Using the pids obtained from the above, you can check the process table to check whether the process is still alive? e.g. (N.B. I use the -m, rather than the -s option to ipcs for illustration, since I have no semaphores, but do have shared memory used). bob@spain:~$ ipcs -m -p ------ Shared Memory Creator/Last-op -------- shmid owner cpid lpid 327680 bob 7192 7238 360449 bob 7233 7284 393218 bob 7273 7187 425987 bob 7235 7187 458756 bob 7279 7187 491525 bob 7235 7187 524294 bob 7284 29961 557063 bob 7227 7187 589832 bob 7227 7187 622601 bob 7287 7187 655370 bob 7309 7187 688139 bob 7347 7187 720908 bob 16117 16124 753677 bob 16117 16124 bob@spain:~$ ps ax | grep 7192 7192 tty2 Sl 0:04 /usr/bin/gnome-session 19670 pts/3 R+ 0:00 grep --colour=auto 7192 bob@spain:~$ A little perl script could be written to do this. Bob -- To make tax forms true they should read "Income Owed Us" and "Incommode You".