Scroll to turn the page
Operating Systems · Operating Systems
a program, running
A program is a file of instructions sitting on the disk. A process is that program running: loaded into memory, with a CPU’s worth of state and the kernel keeping track of it.
Run the same program twice and you get two processes from one program, each with its own memory and its own place in the code.
A process is made of:
malloc. The stack starts at the top and grows down, one frame per function call. The space between them is free;The heap and the stack grow toward each other. In the figure each grows by 2 KB and back; at rest the free gap is 12 KB. A real 64-bit address space is so large that they essentially never meet; running out of physical memory comes first.
where a process can be
On one CPU core only one process runs at a time. The others are waiting, and it matters what for.
The figure follows one process through its moves:
| from | to | because |
|---|---|---|
| ready | running | scheduled |
| running | ready | time slice up (preempted) |
| ready | running | scheduled |
| running | blocked | asks the disk for data |
| blocked | ready | the disk interrupt: data arrived |
Only the scheduler moves a process from ready to running. Being blocked is not wasted time for the machine: while this process waits for the disk, which takes milliseconds, the scheduler runs a ready one instead. A process never goes straight from blocked to running; when its I/O completes it rejoins the ready processes and waits its turn.
Two more states bracket a process’s life: being created, and having finished but not yet been cleaned up (a zombie, page 2.4).
how a process is paused and resumed
For a process to stop in the middle of anything and later continue as if nothing happened, the kernel must save exactly where it was.
The kernel keeps a process control block (PCB) for every process. In Linux it is called task_struct. It holds:
A context switch from process A to process B happens inside the kernel, after a timer interrupt or a system call that blocks:
In the figure (register values hypothetical), A’s pc, sp, rax, rbx go into its PCB, and B’s come out of its own. B continues at pc = 0x40b318, the instruction where it was stopped.
A switch takes microseconds, but it is pure overhead: no program makes progress during it, and afterward B finds the CPU’s caches full of A’s data. That is one reason time slices are milliseconds long, not microseconds.
fork() is called once and returns twice
On Unix-like systems (Linux, macOS) every new process is made by copying an existing one.
int x = 100; pid_t pid = fork(); if (pid == 0) { x = x + 1; // child printf("child: x = %d\n", x); } else { printf("parent: child is %d\n", pid); } fork() creates a child that is a copy of the parent: the same code, a copy of its memory (the variables, the heap, the stack), the same open files, and the same place in the program. Then both continue from the return of fork(). The only difference is what it returns:
So the if sends each process its own way. The child adds 1 to its copy of x and prints 101; the parent’s x is still 100. After the fork they share no memory.
Which line prints first is up to the scheduler: either order is correct output.
Each fork doubles the processes that run the code after it. Three fork() calls in a row, each made by every process that exists by then, leave 8 processes. (The copy is cheaper than it sounds: the kernel shares the memory and copies a page only when one side writes to it, called copy-on-write.)
how a shell runs a command
fork() makes a copy of the same program. To run a different program, the copy replaces itself.
exec() (on Linux the system call execve, behind library functions such as execvp) loads a new program into the calling process: new code, new data, a fresh heap and stack. The process ID stays the same and open files stay open. If it succeeds, it never returns: the code that called it is gone.
wait() (or waitpid) blocks the parent until a child finishes, and collects its exit status. A shell runs every command this way:
while (1) { read a command: ls pid = fork(); if (pid == 0) execvp("ls", args); // never returns waitpid(pid, &status, 0); } A child that has exited but not yet been waited for is a zombie: its memory is freed, but its PCB stays so the parent can read its exit status. If the parent exits first, the orphaned child is adopted (by init, PID 1, or a designated “subreaper”), which waits for it.
Splitting fork and exec is what lets the shell set up the child between them, for example pointing its standard output at a file (ls > out.txt) or at a pipe.
ls | wc -l
Processes share no memory, so to work together they ask the kernel to carry data between them. The simplest way is a pipe.
A pipe is a buffer inside the kernel with a write end and a read end. The shell runs ls | wc -l by creating a pipe, forking twice, pointing ls’s standard output at the write end and wc’s standard input at the read end, then exec-ing each program. Neither program knows; each just writes or reads as usual.
wc knows to print its count.On Linux a pipe holds 65,536 bytes by default (16 pages of 4,096 bytes). In the figure (8 slots) ls writes faster than wc reads, so the pipe fills and ls blocks; then it drains, and wc waits on the empty pipe.
Blocking does the coordination for free: the faster process is paused, not spinning, and the scheduler runs something else. Chapter 5 builds the same kind of bounded buffer out of locks and condition variables.