ContentsThe library

What Happens While You Wait

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.

FIG 1From the event to your handler and back
Follow the path and notice what never appears in it: a new thread, a separate stack by default, or any wait for your code to reach a convenient point. The handler is spliced into the middle of an ordinary thread of execution, and the function it interrupted has not returned and has not released anything. Everything in the next section is a consequence of the fourth node.

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 contents

This 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

The rest of this course

  1. 01Your Program Cannot Read a File, and Never Could
  2. 02Two Hundred Times the Price, for a Line That Looks the Sameopening only
  3. 03Your Program Is the Small Part of Your Processopening only
  4. 04The Program That Waits Gets Served First, and It Is Not Being Rewardedopening only
  5. 05Not Running Is Two Different Problems With One Symptomopening only
  6. 06Ten Thousand Connections, One Thread, and One Thing It Still Cannot Doopening only
  7. 07Your Handler Runs Between Two Instructions You Did Not Chooseyou are here
  8. 08Four Seconds of Wall Clock, Half a Second of Work, and Where the Rest Wentopening only

Read alongside