I program that I wrote in C fork()'s off a child process. Neither process will terminate. If I launch the program from the command line and press control-c which process(es) will receive the interrupt signal?
Fork() and how signals are delivered to processes
forkprocesssignals
Related Solutions
Could anybody please tell me the reason to why pending signals are not inherited by the child process?
Signals come in two flavours, synchronous and asynchronous. Synchronous ones begin with the kernel in response to something the process did -- e.g., the ever popular SIGSEGV. Asynchronous signals generally come from other processes, and, as the name implies, arrive at some arbitrary point. A "pending" signal is either one which simply hasn't been delivered yet, or one which has been blocked temporarily by the receiving process. Consider some pending scenarios and why it would be inappropriate for the child to inherit them.
Synchronous, undelivered: Let's take the example of a SIGSEGV that's pending because the parent committed an access violation -- something I'm sure actually doesn't happen or doesn't happen for long enough to really consider it as "pending" from a userspace perspective, but for sake of argument -- then it's the parent that committed the violation, not the child. If the child then proceeds to do something similar (which it likely would), it will get its own signal anyway. If by chance it doesn't, then it shouldn't receive one that was pending for the parent.
Asynchronous, undelivered: In this case, it really cannot be meaningful if the signal was pending for the duration of the fork and then delivered to the parent, since the signal is asynchronous -- it cannot be intended to interrupt the process at a particular point, it's like a phone call; the caller isn't intending to catch you while you are still sitting in your chair because the caller can't know that. The point of the call is just to communicate something to you. Further, signals are addressed to specific process, and the child is a separate process with its own PID. If I send a signal to process 1001, I do not expect it to also go to process 1002.
Blocked (synchronous or asynchronous): In this case, the signal is left pending intentionally by the process, so it had the opportunity to unblock it before the fork (but didn't). Since it is known that such signals will not be inherited by the child, this scenario occurs by design. For example, a server receives a signal and for what ever reason decides to block it temporarily while it answers an incoming request -- pselect()
will accomplish this, I think -- and to handle the request it forks a child. Then it deals with the pending signal.
On the other hand, the child process inherits the signal handlers and signal mask from the parent, why is this done?
That seems integral to the concept of a fork: the child is a copy of the parent. Signal handlers are just regular functions with a special purpose. Again, since this is known it can be exploited by design, or conversely, compensated for. It is easy enough to remove a handler if you no longer want to use it.
Recent versions of the nVidia proprietary drivers (possibly combined with other recent versions of libraries) have a bug which causes them to corrupt the signal mask.
You can look at signal masks like this:
anthony@Zia:~$ ps -eo blocked,pid,cmd | egrep -v '^0+ '
BLOCKED PID CMD
fffffffe7ffbfeff 605 udevd --daemon
0000000000000002 4052 /usr/lib/policykit-1/polkitd --no-debug
0000000000087007 4646 /usr/sbin/mysqld --basedir=/usr […]
0000000000010000 15508 bash
That's about what it should look like. If you run that on a system with the proprietary nVidia drivers, you'll see all kinds of crazy values for BLOCKED
, for many of your programs—including, likely, all the misbehaving ones.
Note that signal masks are passed from parent to child through fork
/exec
, so once a parent process has a corrupt one, all the children it spawns from that point forward will, too.
See also my question After upgrade, X button in titlebar no longer closes xterm and various distro bugs you'll be able to find now, knowing which package to look at. You can modify the code in my answer to that question to reset the signal mask to none blocked (Elide sigaddset
and change SIG_UNBLOCK
to SIG_SETMASK
).
Best Answer
Why don't we try it out and see? Here's a trivial program using
signal(3)
to trapSIGINT
in both the parent and child process and print out a message identifying the process when it arrives.Let's call that
test.c
. Now we can run it:Interrupt signals generated in the terminal are delivered to the active process group, which here includes both parent and child. You can see that both
child_trap
andparent_trap
were executed when I pressed Ctrl-C.There is a lengthy discussion of interactions between
fork
and signals in POSIX. The most material part of it here is that:They also note that some systems may not behave in exactly the correct way, in particular when the signal arrives very close to the time of the
fork()
. Figuring out whether you're on one of those systems is probably going to require reading the code or a lot of luck, because the interactions are vanishingly unlikely in each individual attempt.Other useful points are that:
kill
) will be delivered only to that process, regardless of whether it is the parent or child.SIGINT
code (the default behaviour).