lakshmananindia has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I want to get the process ID of the process which sends a signal to my process. I used sigaction, but inside the signal handler, I got only the signal name. I also printed the @_ with Dumper inside the signal handler. It contains only the Signal name.

So please tell me a way, how to get the process ID of a process which sends the signal?

Replies are listed 'Best First'.
Re: sigaction handler
by ysth (Canon) on Dec 08, 2008 at 04:32 UTC
    Set the SA_SIGINFO flag in your sigaction call, and your signal handling routine will get two extra parameters. The first is a hashref to a hash intended to contain the various siginfo fields and the second is the raw siginfo struct. Currently, the hash only has signo and code, since which entries of the siginfo struct are set varies greatly on a number of different things, and no one has taken the time to write the code to handle other fields like errno, status, uid, pid, addr, and band. But you can examine what your system provides in siginfo_t and use a suitable unpack on the raw structure to get the pid issuing the signal.
Re: sigaction handler
by almut (Canon) on Dec 08, 2008 at 10:11 UTC

    Simply re-posting essentially the same question you posted three days ago (Signal Handler) is unlikely to provide any new insights — in particular as there don't seem to be many alternatives...

    As I already said in that thread, use Perl 5.10.0 and extract the respective PID from the raw siginfo dump using unpack. Have you tried that (i.e. with 5.10.0)?  The only alternative I see is writing your own XS code... but then you'd have to make sure it doesn't interfere with Perl's already existing signal handling implementation, which could be non-trivial.

Re: sigaction handler
by kennethk (Abbot) on Dec 08, 2008 at 04:14 UTC

    I don't think you can easily get the process ID of the process that sends the signal. If you can, however, it should be in perlipc. In particular, it says

    For more complex signal handling, you might see the standard POSIX module. Lamentably, this is almost entirely undocumented, but the t/lib/posix.t file from the Perl source distribution has some examples in it.
    So you might check out POSIX and Google as well if you go that route.

    There's also a section in perlipc on implementing interprocess communication, which may be what you are looking for.