How to find process holding a semaphore?

Hello All,

The system concerned has multiple processes communicating with each other using shared memory. These processes use semaphores to protect data being used amongst them. The "key" would uniquely identifies the particular semaphore corresponding to a resource for the various processes.

My task is to write a standalone program which would identify the various semaphores being used in the system with their ID, Key etc and also provide information which process is holding the semaphore.

My query is how to find which process is holding a particular semaphore at a particular instant? The best I could find over the internet was that by using semctl with command GETPID I can find out the process ID which performed the last semop operation on a particular instant. However that does not mention what the operation was and whether the process is currently holding the semaphore or not? Best would be to get a mechanism at runtime which could provide this information to provide a snapshot at a particular instant. Please assist.

Thanks.

best regards,
Saptarshi.

What's your OS?

No process "holds" a semaphore. It's not a lock.

Processes block and wait (or not) for another process to post to the semaphore. There's no way to write a program to figure out which process "should" post to the semaphore.

1 Like