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) { ...