Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
Thanks! Easy when you know how :-) –  Gaius Nov 5 '10 at 13:22
    
semctl(2) man page is at 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 8 '10 at 3:12
1  
ipcs -s -i semid may also show the same information. semid is the same number given in ipcs -l. –  zerolagtime Nov 8 '10 at 3:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.