Skip to content
Advertisement

How to find which process owns the named posix semaphore lock? [closed]

I used named semaphore to synchronize multiple processes in my system. One of the process acquired the lock and exits without releasing the lock.Now none of the process able to acquire the semaphore lock.

named semaphore file present in /dev/shm/ directory (i.e) /dev/shm/sem.XXXX.

I am trying to find the culprit process by adding debug logs in the code. Is there any other way we can use to find the process id associated with the named semaphore?

Advertisement

Answer

You can just use the command lsof /dev/shm/sem.XXXX (lsof = list of open files) to find which process has the file. fuser is also an equivalent for what you are trying to achieve. You can just call those commands from your c program via system() call or fork()/exec(). Then you have to analyse the output of the command to take the proper actions.

For more details on portability issues, have a look at https://unix.stackexchange.com/questions/18614/alternatives-for-lsof-command

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement