Appearance
2.2.1 — What a Process Is
Start our photo-upload service twice, in two terminals. Both copies run the same file, upload-service.ts. Yet they are completely separate. Each one has its own memory. Each one can crash without touching the other. The kernel even gives them different numbers.
ts
// whoami.ts — what the kernel knows about this running copy of the upload service
const mb = (n: number): string => (n / 1024 / 1024).toFixed(1) + " MB";
console.log("process id (PID):", process.pid); // (1)
console.log("parent's id (PPID):", process.ppid); // (2)
const before = process.memoryUsage(); // (3)
const photos: Buffer[] = [];
for (let i = 0; i < 20; i++) photos.push(Buffer.alloc(5 * 1024 * 1024, 1)); // 20 photos, 100 MB
const after = process.memoryUsage();
console.log("resident memory before:", mb(before.rss)); // (4)
console.log("resident memory after: ", mb(after.rss));
console.log("held outside the JS heap:", mb(after.arrayBuffers)); // (5)
// One run printed: A second run printed:
// process id (PID): 16036 process id (PID): 20940
// parent's id (PPID): 24236 parent's id (PPID): 9620
// resident memory before: 54.9 MB
// resident memory after: 153.2 MB
// held outside the JS heap: 100.0 MB(1)The kernel gave this running copy a number, its process ID or PID. The second run got a different number, because it is a different running copy.(2)The PPID is the PID of the program that started this one. Here that is the terminal's shell. Every running program was started by some other running program, and page 2.2.2 shows how.(3)process.memoryUsage()asks how much memory this copy is using right now.(4)RSS stands for resident set size: how much of this program's memory is actually sitting in RAM. It grew by about 100 MB when we made twenty 5 MB photo buffers.(5)Node keeps those buffers outside the area where it stores ordinary JavaScript objects, and reports them separately. That is a Node detail. The kernel just sees memory.
The file is the same. The running copies are different. The word for a running copy is process, and it is one of the two or three most important ideas in the whole operating system. This page defines it exactly: what the kernel keeps about each process, what a process's memory looks like inside, and how the kernel pauses one process and resumes another many times a second.
1. A program is a recipe, a process is the cooking
A program is a file. node is a program. upload-service.ts is a program. /usr/bin/ls is a program. A program is passive. It sits on the disk and does nothing, like a recipe in a closed book.
A process is a program in motion. It is one running instance of a program, with a life of its own. If the program is the recipe, the process is a cook actually making the dish, right now, with a particular bowl, with the onions half chopped, at step 4.
That last part is the key. A process is not just the code. It is the code plus all the live state of running it. The kernel gives every process four things:
- Its own private memory. No other process can see it or change it. 2.5.1 explains how the kernel makes that possible.
- The CPU's state at this moment. That is the values in the CPU's registers, including the program counter, the register that holds the address of the next instruction to run (1.5). This is the "at step 4 right now" part.
- Its open resources. The files, network connections and other things it has open, each known by a file descriptor (2.1.2).
- Its identity. Its PID, its parent's PID, which user it runs as, and its priority.
Run the same program twice and you get two processes. They share nothing. Two cooks following the same recipe each have their own bowl. That is exactly what our two copies of the upload service showed.
2. What a process's memory looks like
A process's private memory is not one shapeless lump. It is laid out in regions, always in the same order. Knowing the layout explains several common bugs. Here it is from the lowest addresses at the top of the picture to the highest at the bottom.
Walk through the regions from top to bottom.
- The text segment holds the program's machine-code instructions. It is marked read-only, so a bug cannot accidentally overwrite the program's own code. (The name "text" is old Unix slang for code.)
- The data segment holds global variables and constants: things that exist for the whole life of the program.
- The heap holds everything the program creates while it runs. In C that means memory from
malloc. In TypeScript it means every object, array and string. Our twenty photo buffers live here. The heap grows as the program makes more things. - The gap in the middle is enormous. On a 64-bit Linux PC, one process can address 128 terabytes of user memory, and almost all of it is unused. Unused address space costs nothing, because the kernel gives a region real RAM only when the program actually touches it (2.5.3). The gap is where the kernel places shared libraries, which are code files that many programs use, such as the C standard library. It also holds files mapped into memory (2.5.4).
- The stack holds the chain of function calls. Each time a function is called, a block called a stack frame is added. It holds that function's local variables and the address to return to when it finishes. Each time a function returns, its frame is removed. The stack grows towards the heap.
Why infinite recursion crashes
A common explanation says infinite recursion crashes because "the stack grows until it hits the heap". On a modern 64-bit system that is not quite right. The gap is far too big to fill. The real reason is that the stack has a size limit. On Linux the main thread's stack is usually limited to 8 MB. Right past the end of the allowed stack, the kernel leaves an unmapped region called a guard page. When runaway recursion keeps adding frames, it eventually touches the guard page. The CPU raises an exception, the kernel sees the stack has overflowed, and the process is ended. That is a stack overflow.
In TypeScript you rarely see the kernel's version. Node checks its own call depth first and throws a RangeError: Maximum call stack size exceeded before the real stack runs out. The idea is the same: the stack is small and fixed, and the heap is large and growable. That is why big data belongs on the heap.
3. The process control block: the kernel's file on each process
The kernel runs hundreds of processes at once. For each one it keeps a record, a data structure called the process control block, or PCB. In Linux it is called task_struct, and it is a large structure with hundreds of fields. Think of it as the process's complete file in the kernel's filing cabinet. It holds everything the kernel needs to manage the process, and above all to pause it and resume it later.
The important fields are:
- the PID, and the parent's PID;
- the user the process runs as, and its priority;
- a pointer to the description of its memory, so the kernel knows which addresses belong to it;
- a pointer to its table of open file descriptors;
- its current state (section 4);
- and a saved copy of its CPU registers, including the program counter, for the times when it is not running.
That last field is what makes sharing the CPU possible. Section 5 shows why.
Where PIDs come from
The kernel hands out PIDs by counting upwards, and wraps around to small numbers again when it reaches a limit. On Linux the default limit is 32,768, and many modern systems raise it to about four million. A PID is reused only after the old process with that number has completely gone. That is why you should never save a PID and trust it days later: by then it might belong to a different program.
4. The states a process moves through
At every moment, each process is in one state. The kernel moves it between states as things happen.
- New. The process is being created.
- Ready: it could run right now, and is waiting only for a free CPU core.
- Running: it is on a CPU core this instant. At most one process runs on each core at a time.
- Blocked, also called waiting or sleeping: it cannot continue until something happens. Usually that is input or output: "I asked the disk for data, wake me when it arrives". Our upload service is blocked almost all the time, waiting for the next photo to arrive. A blocked process uses no CPU at all. This state is the whole reason a computer feels fast despite slow disks and networks: while one process waits, the CPU runs others.
- Terminated: it has finished, and is waiting to be cleaned up.
Notice one detail in the figure. When the wait is over, a blocked process goes back to ready, not straight to running. It has to take its turn for a CPU like everyone else.
The real names on Linux
The ps command on Linux shows each process's state as a letter. These letters are worth knowing, because you will see them in every monitoring tool.
| Letter | Name | Meaning |
|---|---|---|
R | running | running, or ready to run |
S | sleeping | blocked, and can be woken by a signal |
D | disk sleep | blocked, and cannot be woken by a signal |
T | stopped | paused, for example by Ctrl+Z |
Z | zombie | finished, but not yet collected by its parent |
Linux uses R for both ready and running. The difference between them changes every few milliseconds, so it is not worth showing. The Z state belongs to page 2.2.2, and T to 2.8.2.
The D state deserves a closer look, because it causes real confusion in production. S and D are both "blocked", but they differ in one way. A process in S can be interrupted. If you send it a signal, even the strongest one, kill -9, it wakes up and handles it. A process in D is in the middle of an operation the kernel will not abandon halfway, usually a disk or network-storage operation. Not even kill -9 can end it until that operation finishes. If the disk or the network storage server has stopped responding, a process can sit in D for minutes. People then find a process they "cannot kill". The fix is never in the process. It is in whatever device it is waiting on.
5. The context switch: how one CPU runs many processes
A laptop might have 8 CPU cores and 400 processes. Yet they all seem to run at once. The trick is speed. The kernel runs one process on a core for a few milliseconds, then switches to another, then another. It does this so fast that, to a human, everything seems to happen at once, the way 24 still pictures a second look like a moving film. Each switch is a context switch.
Here is exactly what the kernel does to switch a core from process A to process B. Each step uses something from earlier on this page.
- Save A's registers, including the program counter, into A's process control block. A is now frozen at precisely the instruction it was about to run.
- Update A's state to ready, or to blocked if it was waiting for something.
- Choose B. This is the scheduler's job, the subject of 2.3.2.
- Switch to B's memory. Tell the CPU to use B's memory map from now on, so that B sees its own memory and nothing else (2.5.2).
- Load B's registers from B's control block. B's program counter is now in the CPU.
- Return to user mode. B continues exactly where it was frozen, as if it had never stopped.
The saved set of registers is the "context". The control block is where it lives. Now you can see why the control block must hold the registers: without a frozen snapshot to restore, a paused process could never continue.
Why switching is not free
A context switch does no useful work for anyone. It is pure bookkeeping. The direct cost, saving and loading registers and running the scheduler, is a few microseconds on a typical machine (the exact figure depends on the CPU).
The hidden cost is often bigger. Process B's data is probably not in the CPU's caches, because A was using them (1.6). Switching memory maps in step 4 also throws away many of the CPU's saved address translations (2.5.2). So for a while after a switch, B runs slowly while the caches fill up again with its data.
This cost is why "start 10,000 processes, one per task" is usually a bad plan. If the CPU spends most of its time switching between them and refilling caches, little real work gets done. It is also a big reason Node.js uses one process with an event loop to serve thousands of connections (3.8.1): it avoids paying for thousands of switches.
Two kinds of switch
The kernel counts the switches each process has had, in two groups. On Linux you can read the counts from the file /proc/<pid>/status (2.8.3 explains these files).
- A voluntary switch happens when the process blocks: it asked for something slow, so it gave up the CPU by itself.
- An involuntary switch happens when the kernel takes the CPU away because the process's time slice ran out.
text
voluntary_ctxt_switches: 48211
nonvoluntary_ctxt_switches: 97These illustrative numbers are what a healthy upload service looks like. It switches voluntarily all the time, because it keeps waiting for the network and the disk. It is almost never forced off the CPU, because it rarely uses its whole time slice. If the second number were huge, it would mean the service was doing heavy computing and competing for the CPU. That is a clue 2.8.5 teaches you to use.
6. The process is the unit of isolation and the unit of failure
Because each process has its own private memory, a crash stays inside one process (2.1.2). That is not only a safety net. Engineers use it on purpose as a design tool.
- Browsers put each site in its own process. If one page hangs, the others keep working. And a malicious page cannot read the memory of the tab where your bank is open, because it is a different process.
- Servers isolate work in separate worker processes. If one worker crashes, the others keep serving, and a supervisor starts a fresh one.
- "Just restart it" is a real reliability strategy. The programming language Erlang, built for telephone switches that must never go down, is famous for a "let it crash" approach. Each small task runs in its own isolated process, and a supervisor restarts any that fail. Part 10 returns to this idea at the scale of whole machines.
The price is the memory and switching cost of many processes. That tension is exactly why threads exist: several strands of work inside one process, sharing its memory. 2.3.1 builds them.
What the interviewer will push on
"What is the difference between a program and a process?" They are checking whether you can say that a process is a program plus its live state. A memorised answer says "a process is a running program". A good answer lists what the kernel gives each process: its own memory, its saved registers and program counter, its open files, and its identity. The tell is the two-copies example: one file, two independent processes.
"What happens during a context switch?" They want the steps: save the registers to the control block, change the state, choose the next process, switch memory maps, load its registers, return to user mode. The strong answer adds the hidden cost of cold caches and lost address translations. The common wrong answer mixes it up with a mode switch. A system call is a mode switch within one process. A context switch changes which process runs.
"Why can't I kill this process, even with kill -9?" This separates people who have run servers from people who have not. The answer is the D state: the process is inside an operation the kernel will not abandon, usually waiting on a disk or network storage that has stopped answering. The fix is the device, not the process.
"Why does infinite recursion crash?" The good answer says the stack has a fixed limit, often 8 MB, with a guard page past the end. The common wrong answer is "it hits the heap". In Node you get a RangeError before the real limit, because the runtime checks first.
Something to volunteer that nobody asks for: voluntary and involuntary context-switch counts tell you, without a profiler, whether a process is mostly waiting or mostly computing. A process with huge involuntary counts is fighting for CPU, and adding more of them will make things worse, not better.
Recall
- A program is a passive file. A process is one running instance of it: its own private memory, its saved CPU state including the program counter, its open file descriptors, and its identity (PID, parent PID, user, priority).
- A process's memory is laid out as the read-only text segment, the data segment, the growing heap, a huge gap holding shared libraries, and the size-limited stack. Runaway recursion hits the stack's guard page.
- The kernel keeps a process control block (
task_structin Linux) per process, including a saved copy of its registers. - A process moves between ready, running and blocked, ending terminated. A blocked process uses no CPU. On Linux, state
Dcannot be interrupted, even bykill -9. - A context switch saves one process's registers, switches memory maps and loads another's. Its direct cost is microseconds, and its hidden cost is cold caches, which is why thousands of processes or threads can be slower than one event loop.
Self-test: What four things does the kernel give every process? Why is "the stack hits the heap" a poor explanation of a stack overflow? What does the process control block store, and why must it hold the registers? What is the difference between the S and D states? What are the direct and hidden costs of a context switch?
Quiz Bank
FoundationalWhat is the difference between a program and a process?
A program is a passive file on disk: machine code and data, doing nothing on its own. A process is one running instance of a program, with its own live state: its own private memory, its saved registers and program counter, its open files, and a unique PID.
Running the same program twice creates two separate processes that share nothing and know nothing about each other. The program is the recipe. The process is a cook actually making it.
FoundationalWhat are the main regions of a process's memory?
From low addresses to high:
- The text segment holds the machine code and is read-only.
- The data segment holds global and static variables.
- The heap holds everything created while the program runs, and grows as it allocates.
- A huge gap holds shared libraries and memory-mapped files. Unused address space costs no RAM.
- The stack holds one frame per active function call, with local variables and return addresses. It has a fixed limit, usually 8 MB for the main thread on Linux.
AppliedWhat is a context switch, and what does it cost?
A context switch is the kernel moving a CPU core from one process to another. In order, the kernel:
- saves the current process's registers and program counter into its process control block;
- marks it ready or blocked;
- asks the scheduler for the next process;
- switches to that process's memory map;
- loads its saved registers;
- returns to user mode, so it continues where it stopped.
The direct cost is a few microseconds of pure bookkeeping. The hidden cost is usually larger: the new process's data is not in the CPU caches and many saved address translations are gone, so it runs slowly for a while. Too many runnable processes or threads means the CPU spends much of its time switching instead of working.
InterviewA process will not die, even with kill -9. What is going on?
The process is almost certainly in the D state, uninterruptible sleep. It is in the middle of an operation the kernel will not abandon halfway, usually waiting for a disk or a network storage server. In that state the kernel does not deliver signals, so even SIGKILL waits until the operation finishes.
If the device or the storage server has stopped responding, the process can stay in D for a long time. The fix is not in the process. Find what it is waiting on, such as a failing disk or an unreachable network share, and fix or detach that. Many D processes at once also push up the load average, which 2.8.5 explains.
InterviewWhat is the difference between a mode switch and a context switch?
A mode switch happens inside one process. A system call moves the CPU from user mode to kernel mode and back, and the same process continues afterwards. It costs hundreds of nanoseconds to a couple of microseconds.
A context switch changes which process is running. The kernel saves one process's registers, switches the memory map and loads another's. It always includes a mode switch, because only the kernel can do it, but it costs more: the scheduler runs, and the new process starts with cold caches. A system call that blocks, such as a read with no data yet, often leads to a context switch, because the kernel runs someone else while it waits.
StaffYour service runs one process per incoming connection and falls over at a few thousand connections, although CPU use per connection is tiny. Explain what is happening using this page, and what you would change.
Each process has its own memory, its own control block and its own descriptor table, so thousands of processes cost a lot of memory even when idle. Worse, whenever connections become active, the kernel must context switch between thousands of processes. Each switch has a direct cost and leaves the next process with cold caches. Past a point, the machine spends its time switching and refilling caches instead of serving requests.
The fix is to stop paying per connection. One option is a small pool of worker processes, roughly one per CPU core, each handling many connections. Another is an event loop that handles thousands of connections in one process, using the readiness tools of 2.7.3. Node, Nginx and Redis all work this way. You keep process isolation where it pays, between a few workers, without paying for one process per connection.
Scenario Drill
DrillA teammate runs two copies of the photo-upload service on one machine and is surprised that a counter of uploads in one copy does not include the uploads handled by the other. Explain what is happening, and what they would need to share a count.
The two copies are two processes. Each process has its own private memory, so each copy has its own counter variable, at what may even be the same address, but in a different private address space. Uploads handled by copy 1 change copy 1's counter. Copy 2 never sees it. This is exactly the isolation the kernel promises.
To share a count, the two processes must use something outside their private memory:
- a shared store both can reach, such as a database or Redis (Part 7), which is the usual answer for a real service;
- a file on disk, carefully locked;
- or an inter-process channel, which page 2.2.4 covers.
If the copies only need to share work inside one program, a different tool fits: threads, which share one process's memory on purpose (2.3.1). That sharing brings its own dangers, which 2.4.1 explains.
Next: every process has a parent, and our upload service's parent was the shell. So how does one process create another? Unix's answer is strange the first time you meet it: the parent makes a near-perfect copy of itself, and the copy then turns itself into a different program. Page 2.2.2 explains fork, exec and wait, and the zombies and orphans they leave behind.