Your Handler Runs Between Two Instructions You Did Not Choose
Last timeAsking Without Waiting
A signal arrives in the middle of whatever your program was doing, on the same thread, with every lock it was holding still held. That is the whole reason the rules are so strict.
What arrives without being asked
Two different things interrupt a machine, and they are worth separating
because only one of them reaches your program.
Hardware interrupts come from devices. A disk finishes a transfer, a network
card has a packet, a timer expires. The processor stops whatever it was doing,
switches to the stronger state, and runs a kernel routine. Your program is not
involved, is not notified, and cannot tell this happened, except that it
briefly stopped running. Everything in the fourth and fifth lessons rests on
these.
Signals are the process-level version. They arrive at a process rather than at
a processor, and they come from four places. A terminal, when somebody presses
the interrupt key. Another process, deliberately. A timer you set. And the
kernel, telling you that your own code did something impossible: touched an
address that is not mapped, divided by zero, executed an illegal instruction.
That last category is the uncomfortable one, because it means the mechanism is
used both for a polite request to shut down and for the report of a fault in
the middle of the instruction that caused it.
Where the handler actually runs
The delivery mechanism explains every restriction that follows, so it is worth
getting exactly right.
Why the rules are so strict
Now the problem states itself. Your thread was in the middle of allocating
memory. The allocator had taken its lock and was halfway through rearranging a
free list. The signal arrives. Your handler runs, on this thread, and calls a
function that formats a message, which allocates memory, which tries to take
the allocator's lock.
The lock is held. It is held by this thread, by the function sitting below your
handler on the stack. That function cannot continue until your handler returns.
Your handler cannot continue until the lock is released. The program is stopped
forever, and it will happen perhaps one time in ten thousand, in production,
never in testing.
The lesson stops here
1 more paragraph to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents