This is a signal that is sent when alarm is used. An alarm call specifies the number of seconds to elapse before a SIGALRM signal is sent to the requesting process. The return value of alarm is the number of remaining seconds of the previous alarm call. In other words, by itself, alarm is useful for timing purposes (but the resolution is low).
The default disposition of SIGALRM is term, which means the signaled process is terminated. The following program verifies this:
As the program waits for the user to input a number, the kernel keeps track of the elapsed time. If nothing is entered, the scanf call continues to block. After 10 seconds, the OS sends the signal. Because the default behavior is to terminate the process, the process gets terminated. The process does terminate with an interesting message.
In order to handle the signal, let us use sigaction.
This time, the process still exits after 10 seconds, except this time it prints the custom message. Furthermore, the process does not terminate anymore. This is because when a handler is specified, the default disposition no longer applies. In this case, it means that the default disposition of terminating the process is no longer applicable.
Instead, when a signal is received, the handler is “called”. At the end of the handler, it returns to the place of the process where the signal interrupted. The process then continues execution after the handler returns.
At this point, the alarm is a one-shot deal. In order to make the message appears every 10 seconds, we can invoke alarm in the alarm handler.