Could anybody please tell me the reason to why pending signals are not inherited by the child process? On the other hand, the child process inherits the signal handlers and signal mask from the parent, why is this done? An example would be very helpful, so that i could understand the concept much better.
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.