Part 5 of 6 · 3 min read
Alarms
Timers and interrupted calls
In plain words: The call alarm(n) asks the kernel to send SIGALRM after n seconds. A process that is waiting inside a system call is woken by a signal. The call sleep returns early and reports how many seconds were left. A test sets an alarm for 1 second and sleeps for 5:
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
static void h(int s) { const char m[] = "caught SIGALRM\n"; write(1, m, sizeof m - 1); (void)s; }
int main(void) {
struct sigaction sa; memset(&sa, 0, sizeof sa);
sa.sa_handler = h; sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
alarm(1);
unsigned left = sleep(5);
printf("sleep returned early, %u s left\n", left);
return 0;
}
caught SIGALRM
sleep returned early, 3 s left
The sleep ended after about one second, and the value is the remaining time in whole seconds, so 3 or 4 is expected.
Look at the handler. It uses write, and not printf. A handler can run at any moment, even in the middle of printf in the main program. If both used printf at once, they could corrupt its internal buffer. POSIX lists a short set of async-signal-safe functions, such as write and _exit, that are safe to call from a handler. Most library functions, including printf and malloc, are not on the list. The rule of thumb is to do almost nothing in a handler: set a flag of type sig_atomic_t and let the main program act on it.
Waiting calls, such as read on a slow device, can also be cut short by a signal. They then return -1 with the error EINTR ("interrupted"), and a robust program retries. Level 4 returns to the problem of code that can be interrupted.
Read it as
A handler is an interrupt handler for the process: it should be short, and its only safe job is usually to set a flag.
Interview angle
Answer in your head first, then open it.
What is a signal?
A short asynchronous notification sent to a process by the kernel or another process.
Which signals cannot be caught?
SIGKILL and SIGSTOP.
What is the difference between SIGTERM and SIGKILL?
SIGTERM can be caught, so a program can clean up. SIGKILL cannot be caught and ends the process at once.
What should a signal handler do?
Very little: set a flag or call only async-signal-safe functions.
What does Ctrl+C do?
It makes the terminal driver send SIGINT to the foreground process.
Check yourself:
1. Which function sends SIGALRM after a delay?
2. Why is printf unsafe in a handler?
3. What does EINTR mean?
Words you will meet
| Word | Meaning |
|---|---|
| Signal | A notification delivered to a process |
| Handler | A function that runs when a signal arrives |
| Default action | What happens if the process does nothing special |
| Catch, ignore | To run a handler, or to discard the signal |
| Async-signal-safe | Safe to call from inside a handler |
| Core file | A copy of the memory of a crashed process |
Quick notes
- A signal is an asynchronous notification, identified by a small integer, that the kernel or another process sends to a process to report an event.
- A signal handler is a function that a process registers, usually with
sigaction, so that the kernel runs it when a particular signal is delivered to that process.