Basis Lab

What Happens While You Wait

Your Program Is the Small Part of Your Process

Last timeThe Cost of Asking

A process is a pile of things the system holds for you: an address space, a table of open files, an identity, a set of handlers. Splitting one in two reveals exactly which parts are which.

The inventory

Ask what a process is and most answers are some version of a running program. That is the least interesting part of the answer, because the program is a file and the running is a scheduling decision. What makes a process a thing is everything the kernel is holding on its behalf.

The list is worth reading slowly.

An address space, which is the mapping from the addresses your code uses to actual memory. A table of open files. A user identity and a set of group identities, which decide what it is allowed to do. A current working directory, which is how relative names are resolved. A set of signal handlers and a set of signals currently blocked. Any pending signals not yet delivered. Resource limits. A scheduling priority and accumulated processor time. An identifier, a parent, and a list of children. An exit status waiting to be collected once it finishes.

Your code and data are one entry on that list. Everything else is bookkeeping the kernel maintains because your program cannot maintain it itself, which is the whole argument of the first lesson applied to a specific case.

What a file descriptor is

The second item on the list deserves its own treatment, because almost everything confusing about processes turns out to be about it.

An open file in your program is a small integer. Zero, one, two for the standard three, and then whatever comes next. That integer is not a handle in the usual sense, it is an index into a table that belongs to your process. The table entry points at something else: a shared object in the kernel describing one open file, and critically, that object holds the current position.

FIG 1Three layers, and which layer each thing lives on
Follow the two paths into the middle layer and the whole of the behaviour falls out. Shared object means shared position, which is what makes two processes appending to the same log produce an interleaved log rather than a mangled one. Separate objects mean separate positions, which is what makes two programs opening the same file independently clobber each other. The question is never whether they have the same file open, it is whether they arrived at the same middle box.

That middle layer also explains why permissions behave as they do. The check happens at open. After that, the descriptor is the authority, and it can be passed to another process, inherited by a child, or kept across a complete replacement of the program. A descriptor is a capability: holding it is permission, and nobody rechecks how you got it.

The lesson stops here

5 more paragraphs 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 Processyou are here
  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 Chooseopening only
  8. 08Four Seconds of Wall Clock, Half a Second of Work, and Where the Rest Wentopening only

Read alongside