Linux – How to get PIDs waiting on a semaphore

ipclinuxredhatstrace

If I strace a stuck process and see output of the form:

[gaius@redhat64 bin]$ strace -p 18185
Process 18185 attached - interrupt to quit
[ Process PID=18185 runs in 32 bit mode. ]
semop(458760, 0xffa00af0, 1

How would I find out the PID that last incremented the semaphore/that I am waiting for? I know about the lpid column in ipcs -p but that only works for shared memory segments.

My OS is RHEL 5.4 (Tikanga) on x86_64. Thanks!

Best Answer

Quick and dirty:

#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int pid;
    if (argc != 2) return 1;
    pid = semctl(atoi(argv[1]), 0, GETPID);
    printf("%d\n", pid);
    return 0;
}

(I'm guessing here.)