Linux C: upon receiving a signal, is it possible to know the PID of the sender?

Yes. Register your signal handler using sigaction with the SA_SIGINFO flag, filling in the sa_sigaction field. Now your handler function takes a siginfo_t* parameter, which includes a field si_pid.

Note that si_pid is only set under some circumstances. In your case, you'll want to check that check that si_code is set to SI_USER or SI_QUEUE. Read man 2 sigaction for more.


Yes, if you use the sigaction() call to set up your signal handler instead of signal. Doing so will let you set up a signal handler that takes three parameters:

  • An int, for the signal number (just like signal)
  • A siginfo_t *, which is a structure containing all sorts of information about the source of the signal, including the pid of the sender if applicable. (It also includes some information about the cause of the signal for automatic signals like SIGSEGV.)
  • A ucontext_t *, which has to do with which thread got the signal. Mostly ignorable.