0

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!

Gaius
  • 1,461
  • 1
  • 12
  • 19

1 Answers1

2

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.)

Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
  • semctl(2) man page is at http://www.manpagez.com/man/2/semctl/. Arguments to the above program assume that you are passing in the ID of the semaphore obtained as "semid" in `ipcs -l`. The GETPID is "the pid of the last process to perform an operation on semaphore number semnum. The "0" argument is to pick the first semaphore under that `semid` although if there has been more than one created, you'll have to choose a different number. – zerolagtime Nov 08 '10 at 03:12
  • 1
    `ipcs -s -i semid` may also show the same information. `semid` is the same number given in `ipcs -l`. – zerolagtime Nov 08 '10 at 03:15