Lesson 2 of 26 · 12 min read
The process abstraction and its states
Understand how the kernel turns a passive binary on disk into a live process tracked through Ready, Running, and Blocked state transitions.

Course syllabus · lesson 2 of 26
In this lesson
OSTEP Chapter 4 crux — How to provide the illusion of many CPUs? A machine has only a few physical CPUs. How can its operating system give many programs the experience of having a CPU they can use and resume independently?
The kernel needs a durable record of each running program's state in memory. A binary contains instructions, constants, and information about how to load them. A process adds an identity, an address space, the current state of computation, and references to resources such as open files. Two launches of the same executable can reach different instructions, read different inputs, and exit at different times. The operating system treats each launch as a separate process.
Follow a process through its states
Step 1 of 6
Ready to run
The process is eligible for CPU time and waits on the ready queue.
What must survive a pause
Start with the machine state a program can observe. The program counter identifies where execution will continue. The stack pointer locates the current top of its stack, and a frame pointer, when used, helps locate a particular call frame. General-purpose registers can hold operands, addresses, or values the program has yet to store. Flags can record the result of a comparison whose conditional branch has yet to execute. An architecture may also have floating-point and vector registers. Restoring just the program counter would resume the right instruction with the wrong inputs.
Registers address the process's memory. The executable's instructions and initialized data occupy mapped ranges. The stack holds call frames and often local variables. The heap and other dynamic mappings supply additional storage. The kernel tracks the mappings so an address in a saved register continues to refer to the right bytes after the process resumes. It also tracks references outside that address space, such as descriptor entries and the current directory. A resumed call to read(3, buf, n) needs both a valid address in buf and the correct object behind descriptor 3.
Live registers belong to the hardware thread that is currently executing. A process that loses the CPU retains the saved representation of its state. Its memory and file references continue to exist. Time sharing exchanges the live execution state on the core while maintaining each process's independent view of those resources. Sharing still reduces available CPU time. Four continuously ready equal-share jobs on one core may each receive roughly a quarter of the compute capacity, before switching overhead.
From executable bytes to the first instruction
A loader reads executable metadata to learn where segments should appear, which bytes to initialize from the file, and what protections to use. A writable data segment can include a zero-filled tail. A loader may populate pages immediately or establish file-backed mappings and let the fault handler fetch them on first access. An executable's file offset and its mapped virtual address describe different locations.
The kernel provides an initial user stack and arranges arguments in the format required by the platform. It sets a starting instruction address and stack pointer for the first return to user mode. On many systems that return goes to a program entry point or an interpreter such as a dynamic linker. Startup code eventually calls C's main. Setting the instruction pointer to main directly would skip initialization on programs that depend on that startup path.
An allocated heap region need not contain resident physical memory throughout. A user allocator can reuse previously obtained storage or request more mappings. Standard input, output, and error are usually inherited from the launcher, including any redirections already applied. Loading a new program image does not require opening the terminal again. Creation and image replacement are separate operations in the UNIX API covered in Lesson 3.
The process state machine
The useful distinction for the scheduler is whether a process is executing and whether it could execute if given a core.
- Running: The process is executing on a core, in user code or in the kernel on its own behalf. The scheduler must prevent the same execution context from running on another core simultaneously.
- Ready: The process could run immediately if selected. xv6 spells this state
RUNNABLE. It may be tracked by a scheduler queue, or discovered by scanning a process table. - Blocked: A condition required for progress is currently false. xv6 calls this
SLEEPING. Examples include waiting for a device completion, for a byte to arrive on a pipe, or for a child to exit.
Dispatch takes a ready process to running. Preemption returns it to ready, since its need for the CPU persists. Blocking takes it out of contention until some event makes it ready again. An interrupt handler can wake the process without dispatching it. A scheduler may then choose it immediately or let another process run first. Even if the wakeup and the dispatch occur at the same displayed timestamp, they are separate state changes.
A process table typically also tracks allocation and retirement. An UNUSED slot is available for reuse. In the historical xv6 example, EMBRYO marks a slot being initialized. Publishing the slot as RUNNABLE too early could let another core restore an uninitialized stack. ZOMBIE marks a process whose execution has ended and whose parent can still collect its termination. A zombie consumes a table entry but no CPU time.
Reading the process control block
OSTEP's xv6 example uses an early x86 layout. The following field grouping follows that example. Later xv6 layouts can represent memory and register saves differently while retaining the same responsibilities.
- Address space fields.
memidentifies the start of the process memory in this early representation.szgives its size in bytes. A page-table-based version can retain a page table reference to select mappings as part of a switch. - Identity and lifetime.
pididentifies the process to callers. A table slot can be reused after reaping, so its array index alone is an unsuitable external identifier.parentlinks to the parent for child collection and related lifecycle operations. - Scheduling.
staterecords whether this slot can run.chanidentifies the condition or object associated with a sleep. Multiple processes can sleep on the same channel and be woken together.killedrecords a pending termination request that the kernel can honor at an appropriate point. - File state.
ofileis the per-process array of open-file references indexed by descriptor.cwdis a reference to the directory used as the starting point for relative path lookup. - Kernel stack.
kstackidentifies the process's kernel stack allocation. If a system call blocks, the kernel's saved call frames, arguments, and local execution state must survive for that process to resume. - Kernel continuation.
contextrepresents the register state for resuming the kernel's switch routine. The early layout recordseipfor the instruction pointer,espfor the stack pointer,ebpas a saved general register often used for the frame base, andebx,ecx,edx,esiandedias saved general-purpose values. - User continuation.
tfpoints to the current trap frame. That frame stores the state needed to return from a trap, including the interrupted instruction address, flags, general registers, and user stack information for an entry from user mode. Lesson 4 follows each save and restore separately.
A zombie keeps enough information for the parent to collect its termination. What remains allocated until collection depends on the kernel. In the teaching xv6 design, parent-side reaping performs final cleanup of resources such as the child kernel stack. Reclaiming that stack while the exiting process is still using it would invalidate the very call frames needed to finish the switch.
Blocking without missing a wakeup
A sleep operation pairs the state change with a condition check. Imagine that a consumer sees an empty buffer, a producer fills it and issues a wakeup, and only afterward the consumer marks itself sleeping. The wakeup finds no sleeper, and the consumer can wait forever on a buffer that already contains data. Checking under a lock alone does not help if a gap opens between releasing that lock and registering the sleep.
Here is an xv6-style fragment of a buffer consumer. Assume producers protect the same buffer with lock and signal the channel after updating the buffer. The teaching primitive sleep(channel, lock) coordinates releasing the lock with marking the caller sleeping and returns with the lock reacquired.
acquire(&lock);
while (buffer_empty(&buf))
sleep(&buf, &lock);
int byte = buffer_take(&buf);
release(&lock);
The first line serializes the predicate check with producer updates. The loop checks the predicate again after each wakeup. A second consumer could have taken the available byte before this process was dispatched, and other events on the same channel can wake more than one waiter. Using if would proceed without checking that the byte is still there. Taking the byte before releasing the lock keeps the check and removal consistent. Moving release ahead of the removal would allow two consumers to act on the same observation.
A process may spend most of its lifetime waiting for an event and very little time in either running or ready state. For diagnosis, record the point where it becomes runnable separately from the point where it gets CPU time. A slow disk and a crowded CPU queue can cause the same total delay but require different fixes.
Worked trace
Run A and B on one core. A is a compute loop. B enters a file read that requests one uncached block. Assume the policy rotates at the next timer tick after a wakeup and that the returned bytes are ready to copy by the time B runs again.
- At time 0, B is
RUNNINGand A isRUNNABLE. B enters the kernel, which records B's return-to-user state in B's trap frame. The read submits the request and checks the block's readiness under the relevant lock. - At time 1, the read finds the block pending, records its wait channel, and transitions B to
SLEEPING. The switch retains B's kernel continuation inside the read. The scheduler dispatches A. Disk work can proceed while A computes. - At time 4, the completion handler marks the buffer ready and wakes waiters on that channel. B becomes
RUNNABLE. Its stack still describes a system call in progress, even though the device transfer has completed. - At time 5, a timer tick sends A back to
RUNNABLE. B resumes its kernel continuation, reacquires the buffer lock, and checks readiness again. It can now copy the bytes and return through its own trap frame. The read incurred 3 time units blocked and 1 unit ready after the wakeup.
Change one input
Suppose the requested block is already cached and uncontended at time 0. B can copy the bytes and return during the same dispatch. It never sets a wait channel and has no device completion to await. A later timer tick can still send B from RUNNING to RUNNABLE. The distinction between a syscall entry, a sleep, and a scheduling switch explains both traces.
Next, inspect the UNIX process API.
Before you move on
Try it in your workspace
Draw Ready -> Running -> Blocked -> Ready from memory. Trace a blocking read from system call entry to I/O completion, and explain which PCB fields save and restore across a switch.
Keep a short note of what you tried, what passed, and what you still need to check.
0 of 3 checked.
Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.