Briefly observing signal handling
Signals in Linux are a powerful and simple way to synchronize processes through software interrupts sent to them, indicating that an important event has occurred. They have a different nature, depending on their roles. Some of them are ignorable, while others are not and cause a process to be blocked, unblocked, or terminated. We discussed those behaviors in the previous chapter, but is there something we could do to gracefully handle them? We will use the anonymous pipe example to trigger a SIGPIPE
signal.
Let’s see the following example:
... void handle_sigpipe(int sig) { // {1} Â Â Â printf("SIGPIPE handled!\n"); } int main() { Â Â Â int an_pipe[2] = {0}; Â Â Â char buff[BUFF_LEN + 1] = {0}; Â Â Â if (pipe(an_pipe) == 0) { Â Â Â Â Â Â int pid = fork(); Â Â Â Â Â Â if (pid == 0) { Â Â Â Â Â Â Â Â ...