Skip to content
BIASED

Processes and threads

PCB and address space, fork/exec/wait and zombies, threads in one AS, scheduling metrics and context-switch cost.

Why many programs

A job that runs for 10 ms and then waits 40 ms for disk keeps the CPU busy only 20% of that stretch. Early batch systems already noticed: keep several jobs in memory (multiprogramming) so the processor can switch to another when one blocks on I/O. Interactive machines add time sharing: a periodic timer interrupt forces entry to the kernel so the scheduler can run someone else, giving each user the illusion of a dedicated machine. Without the timer, a tight loop in U-mode would never yield.

The process and PCB

A process is the OS abstraction of a running program: a private virtual address space, a PCB holding saved registers and scheduling state (task_struct on Linux), a table of open file descriptors, and credentials (UIDs/GIDs). Isolation is structural — VA 0x400000 in process A is not the same frame as in process B. Performance vocabulary: turnaround Ti=ci−aiT_i=c_i-a_i, waiting Wi=Ti−biW_i=T_i-b_i, response Ri=si−aiR_i=s_i-a_i.

States: new, ready, running, blocked/waiting, zombie (and variations). Transitions to blocked happen on sleeps for I/O or locks; ready→running is the scheduler’s choice; only traps and interrupts move running→kernel.

fork, exec, wait

Unix process surgery uses three calls. fork clones the caller; it returns the child’s PID to the parent and 0 to the child. Modern kernels use copy-on-write pages instead of eagerly duplicating all memory. exec overlays a new program image on the same PID, keeping open fds by default (close-on-exec flags opt out). wait (or waitpid) reaps an exited child’s status; until then the child is a zombie — mostly gone, PCB kept for the exit code. Children whose parents die become orphans adopted by PID 1 or a declared subreaper.

Threads and the scheduler

Threads (kernel threads / tasks) share the VAS, heap and fd table but own a stack and registers. Creating a thread is cheaper than a process; switching is cheaper; accidentally sharing a counter without locks is also cheaper — and wrong. Classic schedulers: FCFS (convoy effects), SJF/STCF (need burst knowledge), round-robin with quantum qq (smaller qq → better interactive response, more switch overhead), multilevel feedback queues that promote interactive jobs. Context-switch cost (save regs, switch page-table root, cold caches) is why thread-per-connection architectures eventually yield to event loops at very large NN. Next — Synchronization: the locks and atomics shared memory demands.

This topic’s checks ask for concrete numbers from the prose — page-sized PIPE_BUF and mode 0640 as 416, syscall number 64, microkernel crossings 8, busy fraction 20% — so that the abstractions stay pinned to the machine.

clone on Linux is the underlying call: flags choose whether to share virtual memory (CLONE_VM), file table (CLONE_FILES), signal handlers and so on. fork is clone with a separate address space; pthread_create is clone with shared VM. Seeing both as flag sets demystifies containers’ use of CLONE_NEWNS, CLONE_NEWPID and friends — namespaces are clone flags too.

Scheduler latency versus throughput is a dial. Tiny quanta favour terminals and GUIs; large quanta favour batch throughput and fewer switches. Real-time policies (SCHED_FIFO) invert fairness for deadlines. None of this helps if your threads spend their quantum fighting a contended lock — the synchronization topic is the missing half of “why is my multi-threaded server idle yet slow.”

Worked habit: for every new abstraction, write down (1) the kernel object, (2) the creating syscall, (3) what is shared after fork, (4) how EOF or termination is signalled, and (5) one concrete number (size, timeout, default limit). That checklist turns this course’s vocabulary into a debugging script you can run under strace.

Observability: ps, /proc/<pid>/status and /proc/<pid>/sched expose state, voluntary/involuntary switches and the current policy. perf can attribute cycles to your code versus kernel time during a switch-heavy workload. If CPU% is low but latency is high, look for blocking IPC or lock contention; if CPU% is high with little progress, look for schedulable spin or thundering herds after a broadcast wake. The abstractions are simple; the emergent behaviour under load is where systems engineering spends its time.

Re-read the block markers as a study plan: each block’s checks are capped so that finishing the prose and the questions in one sitting stays realistic for Pulse. If a number in a check surprises you, jump back to the sentence that introduced it rather than memorizing in isolation.

Putting numbers on switches: a same-core thread switch may cost about a microsecond of direct work plus cache and TLB warm-up that shows up later; a cross-core migration costs more. That is why pinning busy threads (taskset, cpusets) and avoiding needless wakeups are practical skills once the abstractions are clear. Measure with perf sched before redesigning the whole server.

The fork-exec-wait triad is also how shells implement job control and how container entrypoints wrap your app as PID 1: someone must wait, handle signals and forward exits. If your process becomes PID 1 inside a container without a proper init, unreaped zombies accumulate — an operational echo of this topic’s zombie rule.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • fork/COW vs threadsoptional

    For 4 tasks choose process or thread; mark address space and COW after fork.

    ~20 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.