Chapter 3: Processes

Operating Systems
The process conceptMemory layoutCreation: fork / exec / wait Termination, zombies, orphansProcess statesPCB Scheduling queuesContext switchShared memory Message passingPipes and socketsMultitasking on mobile
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Process model

Explain the difference between a program and a process, and the memory layout of a process.

Life cycle

Describe process states, transitions and the contents of the PCB.

UNIX API

Trace small programs that use fork(), exec(), wait(), exit() and pipe().

Scheduling machinery

Explain the ready/wait queues and the cost of a context switch.

IPC

Compare shared memory, message passing, pipes and sockets.

Mobile multitasking

Explain why and how iOS and Android restrict background processes.

The Process Concept

What Is a Process?

  • Process: an instance of a program in execution.
  • A program is passive (an executable file on disk); a process is active (program counter, registers, memory, open files). A program becomes a process when it is loaded into memory.
  • One program can give rise to several processes (vim in two terminals); each has its own address space and resources, though the text section can be shared.
  • A single-threaded process executes its instructions sequentially; a multithreaded process can run in parallel on many cores.
a.outprogram (on disk)passive · filerunrunprocess · PID 4021textdataheapstackactive · in memory · own PCBprocess · PID 4022textdataheapstackactive · in memory · own PCBtwo separate processesfrom one program:separate address spaces,separate state and resources

The Process Concept

Process Layout in Memory

kernel spacemapped into every process; inaccessible in user modestacklocal variables, parameters, return addressesfree spaceheapmemory allocated at run time with malloc() / newBSSuninitialized global / static (zeroed)datainitialized global / static variablestextthe program's machine code (read-only, shareable)growsgrowshigh addressnear address 0
The stack grows down from high addresses, the heap grows up. The program counter is not in memory but is a register in the processor; it marks the process's “current position”. In a real layout ASLR shifts the addresses randomly on every run.

The Process Concept

Where Do Variables Live?

#include <stdlib.h>
int counter = 5;  /* data */
int table[1000];  /* bss */

int main(int argc, char *argv[]) {
    int local = 3;
    static int count;
    int *p = malloc(4 * sizeof *p);
    p[0] = local + counter;
    free(p);
    return 0;
}

On Linux, size a.out shows the text/data/BSS sizes and cat /proc/PID/maps the regions of a running process.

kernel spacestack↓ free ↑heapBSSdatatextmain()malloc(), free()(machine code)

Text: the compiled instructions of main; read-only and shareable among processes of the same program.

kernel spacestack↓ free ↑heapBSSdatatextcounter = 5

Data: the initialized global counter; its value is stored in the executable file.

kernel spacestack↓ free ↑heapBSSdatatexttable[1000]count (static)

BSS: uninitialized global and static variables; the loader fills them with zeros and they take no space in the file.

kernel spacestack↓ free ↑heapBSSdatatextargc, argvlocal = 3p (the pointer itself)return address

Stack: the call frame of main: parameters, local variables and the pointer p itself.

kernel spacestack↓ free ↑heapBSSdatatext*p → 16 bytes(from malloc)

Heap: the 16 bytes allocated by malloc; p lives on the stack, but the area it points to is on the heap. It is returned with free.

The Process Concept

Pseudoparallelism and Multiprogramming

(a) one program counterABCDprocessswitch(b) four independent processesABCDeach has a logical PC(c) only one runs at a timetimeDCBA
  • On a single core only one process executes at any instant; the scheduler switches rapidly among processes, creating pseudoparallelism.
  • Switches happen with time slices of a few milliseconds (EEVDF on Linux uses a base slice of ~3 ms; the old CFS had a target latency of 6–24 ms); to the user all processes seem to run at once.
  • Good for the user, but hard to manage: when a process will be interrupted is unpredictable, so no timing assumptions should be made.

The Process Concept

Concurrency and Parallelism

Concurrency(1 core)ABCABCABParallelism(2 cores)AAABBBAACCCCAACCcore 0core 1time
Concurrency

Several tasks make progress over the same time interval; on one core by interleaving.

Parallelism

Several tasks execute at the same instant; this needs multiple cores or processors.

As of 2026 even phones have 6–10 cores; still, the number of running processes (hundreds) far exceeds the core count, so multiprogramming and switching exist on every system.

The Process Concept

Example: Chrome's Multiprocess Architecture

Browser processUI, tabs, permissions, diskRenderersite ARenderersite BGPU processdrawingNetwork serviceHTTP, cachePlug-in / utilityextensions, audio...IPC (Mojo)renderers run in a sandbox;if one tab crashes, the browser and other tabs live on
BrowserManages the UI, tabs, disk and permissions; the one privileged process.
RendererHandles HTML, CSS, JavaScript; with site isolation, one process per site.
GPU / network / utilityDrawing, networking and extensions in separate processes. Old plug-in support was removed: NPAPI in 2015, Flash at the end of 2020.

Process Creation

Process Creation

Events that create processes

  1. System initialization (init/systemd, background services: daemons).
  2. A running process making a process-creation system call.
  3. A user starting a command or application.
  4. Starting a batch job.

Technically the same thing happens every time: an existing process executes a system call that creates a new process.

System calls

SystemCall
UNIX / Linuxfork() + exec(); also posix_spawn(), and clone() on Linux
WindowsCreateProcess(): creates the process and loads the program in one call
A parent process creates child processes; the children create their own children, forming a process tree. Each process carries a unique PID.

Process Creation

Process Tree

systemdPID 1kthreaddPID 2journaldPID 312sshdPID 890gdmPID 1003kworker/0:1PID 57sshd (session)PID 2101gnome-shellPID 1520bashPID 2105firefoxPID 2310vimPID 2230psPID 2241kernelthreads$ pstree -p$ ps -ef --forest

At boot Linux runs systemd (or another init) as PID 1; PID 2, kthreadd, is the parent of kernel threads. The PID values are examples.

Process Creation

Parent and Child

Resource sharing

The child may share all, some or none of the parent's resources. In UNIX the child inherits the open file descriptors.

Execution

Parent and child can run concurrently, or the parent can wait for the child to finish with wait().

Address space

UNIX fork(): the child gets a copy of the parent's address space (it does not share it). It can then load a new program with exec().

Identifiers

getpid() returns the process's own PID, getppid() its parent's.

After fork() the child's address space is separate from the parent's; for efficiency the physical pages are shared only until one is written (copy-on-write).

Process Creation

fork(): One Call, Two Returns

pid_t pid = fork();
if (pid < 0)        { perror("fork"); exit(1); }
else if (pid == 0)  { /* child: fork returned 0 */ }
else                { /* parent: pid = child's PID */ }
parent (PID 1234)page tablephysical memory (frames)page Apage Bpage C

Start: the parent's page table points to physical pages A, B, C.

parent (PID 1234)page tablechild (PID 1235)page tablephysical memory (frames)page Apage Bpage Cshared,write-protected(read-only)

fork(): the kernel copies the PCB and the page table; the pages are not copied but mapped read-only into both. The parent gets the child's PID, the child gets 0; on error no child is created and only the parent gets −1.

parent (PID 1234)page tablechild (PID 1235)page tablephysical memory (frames)page Apage Bpage Cpage C′copyshared,write-protected(read-only)child wrote to C →page fault →kernel copied it

When the child writes to page C a page fault occurs; the kernel copies only that page (C′). This is copy-on-write (COW).

Process Creation · Step through the code

Code Trace: fork() + wait()

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void) {
    int x = 10;
    pid_t pid = fork();
    if (pid < 0) { perror("fork"); exit(1); }
    if (pid == 0) {
        x = x + 5;
        printf("child: x=%d\n", x);
        return 0;
    }
    wait(NULL);
    printf("parent: x=%d\n", x);
    return 0;
}
parent · 1234running

There is one process: the parent (PID 1234), x = 10.

parent · 1234fork() → 1235child · 1235fork() → 0

fork() creates child 1235. In the parent pid = 1235, in the child pid = 0; there are two separate xs. (A failed fork returns −1: line 9 prints the error and exits.)

parent · 1234wait() → waitingchild · 1235ready

In the parent pid > 0: both ifs are skipped, and wait(NULL) puts the parent in the waiting state until the child finishes.

parent · 1234waitingchild · 1235running

In the child pid == 0 is true: it enters the child branch. (Had the child run first, the result would be the same.)

parent · 1234waitingchild · 1235running

The child changes its own copy: x = 15. The parent's x stays 10.

parent · 1234waitingchild · 1235running

The child prints.

parent · 1234woken (SIGCHLD)child · 1235zombie

The child exits with return 0 (exit(0)); it is briefly a zombie, and the kernel wakes the parent.

parent · 1234runningchild · 1235reaped

wait returns 1235; the child's entry is removed from the process table.

parent · 1234runningchild · 1235reaped

The parent prints: its own x is still 10.

parent · 1234terminatedchild · 1235reaped

The parent terminates too. Thanks to wait(), the output order is the same on every run.

Process Creation

Without wait() the Output Order Is Undefined

pid_t pid = fork();
if (pid == 0) printf("child\n");
else          printf("parent\n");
Schedule 1fork()parent: printfchild: printfparentchildSchedule 2fork()child: printfparent: printfchildparenttime (one core)
The scheduler decides which process runs first after fork; on multiple cores both may even run at once. If the program's correctness depends on this order, use wait() (or another form of synchronization).

Process Creation

The exec() Family

before execafter execlp("ls", ...)PID 1235 · PPID 1234text: a.out codedata / BSSheapstackopen files 0,1,2cwd, credentialsPID 1235 · PPID 1234text: ls codenew data / BSSnew (empty) heapnew stack: argvopen files 0,1,2cwd, credentialsthe whole address spaceis reloadedkept: PID, PPID,open file descriptors
CallArgumentsPATH searchEnvironment
execl / execvlist / arraynoinherited
execlp / execvplist / arrayyesinherited
execle / execvelist / arraynopassed explicitly

They all end up in the execve() system call. A successful exec never returns; if it returns, there was an error.

Process Creation

What Does exec Do in the Kernel?

1. Open the file; check permission and format (ELF header or #! line).

If the file is missing or not executable, exec returns −1 and the old program carries on.

2. Release the old address space.

The code, data, heap and stack mappings are removed; other threads in the process are terminated. From here on there is no program to return to.

3. Close close-on-exec fds, reset signal handlers.

Caught signals revert to the default action, since the handler code is no longer in memory; ignored signals stay ignored.

4. Map the code and data sections from the file.

Nothing is copied: the first use of a page causes a page fault and that page is brought in from disk (lazy loading).

5. Allocate zeroed memory for bss, heap and stack.

The argv and envp strings, the pointer arrays to them and argc are placed at the top of the stack.

6. PC = entry point; return to user mode.

_start sets up the C runtime and calls main(argc, argv, envp). The PID never changed.

old code, data, heap,stack: removedkept:PID and parentopen fds (except cloexec)working directory, usersignal maskprog (ELF)header.text.datastackargc, argv, envpfreeheap (empty)bss (zeroed)datatext (code)PC = entry (_start)mapping is lazy: a page is read on first access

A program on disk is a passive file; exec turns it into the address space of a running process. On Linux the whole exec family ends up in execve().

Process Creation · Step through the code

Code Trace: fork + exec + waitpid

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void) {
  pid_t pid = fork();
  if (pid < 0) { perror("fork"); exit(1); }
  if (pid == 0) {
    execlp("ls", "ls", "-l", NULL);
    perror("execlp");  /* error */
    exit(1);
  }
  int st;
  if (waitpid(pid, &st, 0) < 0) { perror("waitpid"); exit(1); }
  if (WIFEXITED(st)) printf("done: %d\n", WEXITSTATUS(st));
  return 0;
}
parent · 1234fork() → 1235child · 1235fork() → 0

fork(): two processes are running the same a.out image. Had pid < 0, line 8 would print the error and exit.

parent · 1234waitpid → waitingchild · 1235ready

The parent skips the child branch and blocks in waitpid(1235, ...).

parent · 1234waitingchild · 1235exec: ls

The child calls execlp: ls is found on PATH and the address space is replaced by /usr/bin/ls. The PID is still 1235.

parent · 1234waitingchild · 1235ls running

The child is now the ls program; lines 11–12 never run (exec succeeded). It writes the listing to the inherited stdout.

parent · 1234wokenchild · 1235output: 0

ls ends with exit(0); waitpid returns the child's PID (no error) and st carries the exit status.

parent · 1234runningchild · 1235reaped

The child exited normally (WIFEXITED is true): WEXITSTATUS(st) = 0 is printed.

Process Creation

The Shell Loop: fork → exec → wait

bashPID 2105$ ls → reads the command

The shell shows a prompt and reads the ls command.

bashPID 2105bash (copy)PID 2300fork()waitpid(2300)→ waiting

fork(): a copy of the shell (PID 2300) is created; the parent starts waiting with waitpid.

bashPID 2105lsPID 2300fork()execvp("ls")image replaced by lswaitpid(2300)→ waiting

The child calls execvp("ls", argv); its image is replaced by ls.

bashPID 2105lsPID 2300fork()execvp("ls")image replaced by lswaitpid(2300)→ waitinglsrunswrites to stdout

ls runs and writes to the terminal stdout it inherited.

bashPID 2105lsPID 2300fork()execvp("ls")image replaced by lswaitpid(2300)→ waitinglsrunswrites to stdoutexit(0) → SIGCHLDchild is reaped

ls exits; the kernel notifies the parent with SIGCHLD and waitpid reaps the child.

bashPID 2105lsPID 2300fork()execvp("ls")image replaced by lswaitpid(2300)→ waitinglsrunswrites to stdoutexit(0) → SIGCHLDchild is reaped$ new prompt

The shell shows a new prompt. If the command ends with &, the shell continues without waiting (a background job).

Process Creation · Step through the code

Why Are fork and exec Separate? Output Redirection

int main(void) {
  pid_t pid = fork();
  if (pid == 0) {    /* child */
    close(1);        /* stdout freed */
    int fd = open("count.txt",
             O_WRONLY | O_CREAT | O_TRUNC, 0644);
    /* lowest free number: fd == 1 */
    execlp("wc", "wc", "-l", "notes.txt", NULL);
    perror("execlp");
    exit(1);
  }
  waitpid(pid, NULL, 0);
  printf("done\n");
  return 0;
}
child's fd table (copy of the parent's)0 → tty1 → tty2 → ttyparent: 0, 1, 2 → terminal (unchanged)

fork(): the child copies the parent's fd table; 0, 1, 2 are the terminal.

child's fd table0 → tty1 → tty2 → ttyparent: 0, 1, 2 → terminal (unchanged)

The parent waits in waitpid; the next steps change only the child's table.

child's fd table0 → tty1 → (free)2 → ttyparent: 0, 1, 2 → terminal (unchanged)

close(1): entry 1 in the child's table is freed. The parent's stdout is still the terminal.

child's fd table0 → tty1 → count.txt2 → ttyparent: 0, 1, 2 → terminal (unchanged)

open always returns the lowest free number: 1. The child's stdout is now the file.

child = wc · fd table kept0 → tty1 → count.txt2 → ttyparent: 0, 1, 2 → terminal (unchanged)

exec replaces code and data but keeps the fd table: unaware, wc writes to fd 1, that is, to the file.

child done; file holds: 42 notes.txt0 → tty1 → count.txt2 → tty$ cat count.txt → 42 notes.txt

wc finishes and waitpid returns; the parent writes to the terminal: only done appears on screen.

This is how the shell does > count.txt: between fork and exec the child sets up its environment (fds, directory, priority). Windows's CreateProcess takes these as parameters.

Process Termination

Process Termination

CauseKindExample
Normal exitvoluntaryreturn 0, exit(0); the compiler finished its job
Error exitvoluntaryfile not found → exit(1)
Fatal errorinvoluntaryinvalid memory access (SIGSEGV), divide by zero (SIGFPE)
Killed by another processinvoluntarykill(pid, SIGTERM), TerminateProcess() on Windows
  • exit(status): the C library flushes buffers, runs the atexit handlers, then makes the _exit system call.
  • abort(): the calling process sends itself SIGABRT and ends abnormally (a core dump may be produced). To terminate another process, use kill().
  • The kernel reclaims the process's memory, open files and other resources; the exit status is kept until the parent calls wait().
  • Some systems terminate the children when the parent terminates (cascading termination); in UNIX the children live on.

Process Termination

Fatal Errors

ErrorSignal
Invalid/unauthorized memory accessSIGSEGV
Integer divide by zeroSIGFPE
Illegal instructionSIGILL
Alignment / bus errorSIGBUS
Writing to a closed pipeSIGPIPE
  • The OS catches the hardware exception and sends the process the corresponding signal; if unhandled, the process terminates.
  • The process is cleared from memory and its resources are released.
  • A core dump can be taken: inspect the cause with coredumpctl or gdb a.out core (crash-dump analysis).
Leaking memory or not releasing resources is not fatal in itself; the process keeps running. If memory runs out, the Linux OOM killer may terminate a process with SIGKILL.
p = NULL; *p = 5MMU: page faultkernel: invalid addressSIGSEGV is sentterminate + coreif no handler

Process Termination

Termination by Signal: kill, pkill, killall

SignalNoMeaning
SIGTERM15finish politely; the process can catch it and clean up (the default for kill)
SIGKILL9force termination; cannot be caught or ignored, no cleanup
SIGINT2Ctrl+C in the terminal
SIGTSTP20Ctrl+Z in the terminal (suspend); fg/bg resume it
SIGSTOP / SIGCONT19 / 18stop (cannot be caught) / continue

Numbers are for x86/ARM Linux; portable code uses the names, not the numbers.

$ ps -e | grep writer
 1234 ?  00:00:03 writer
$ kill 1234       # SIGTERM
$ kill -9 1234    # SIGKILL
$ pkill writer    # by name
$ killall writer
  • SIGTERM first; SIGKILL if there is no response.
  • Sending a signal requires being the same user or privileged (root).

Process Termination

Zombie and Orphan Processes

Scenario 1: the child finishes firstparent · PID 100runningchild · PID 101running

Parent (100) and child (101) are running.

Scenario 1: the child finishes firstparent · PID 100runningchild · PID 101ZOMBIE (Z) · exit code kept

The child finished with exit(), but the parent has not called wait() yet: the child is a zombie. Its memory is freed; its PID and exit status wait in the process table (Z / <defunct> in ps).

Scenario 1: the child finishes firstparent · PID 100calls wait()PID 101 removed from the process tablegot status=0

The parent calls wait() and collects the exit status; the kernel removes the entry (reaping). No zombie remains.

Scenario 2: the parent finishes firstsystemd · PID 1init / subreaperparent (100) terminatedchild · PID 101ORPHAN · PPID gone

Scenario 2: the parent (100) terminated before the child. The child (101) is still running: an orphan process.

Scenario 2: the parent finishes firstsystemd · PID 1init / subreaperchild · PID 101adopted · PPID = 1

The kernel reparents the orphan to init/systemd (PID 1) or a designated subreaper process; getppid() now returns 1.

Scenario 2: the parent finishes firstsystemd · PID 1init / subreaperchild · PID 101done → systemd reaps with wait()

When the orphan finishes, its new parent reaps it with wait(), so zombies do not pile up.

A zombie uses no resources but holds a PID; a long-lived server that never calls wait can pile up thousands of zombies and exhaust the PIDs. Fix: wait/waitpid or a SIGCHLD handler.

States and the PCB

Process States (the Five-State Model)

Newbeing created
Readywaiting for the CPU
Runninginstructions are executing
Waiting(blocked) waiting for an event
Terminatedfinished execution
admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

Only one process can be running on a core at any instant; many are ready or waiting.

States and the PCB

State Transitions Step by Step

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

The user started a program: the PCB is being created and memory allocated → New.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

The kernel admitted the process and put it in the ready queue → Ready.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

The scheduler picked this process and its context was loaded → Running.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

The process asked to read() from disk; it gives up the CPU until the data arrives → Waiting.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

Disk interrupt: the data is ready. The process does not run immediately; it returns to the ready queue → Ready.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

Picked again → Running.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

Timer interrupt: the time slice expired (preemptive scheduling) → Ready.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

Picked once more → Running.

admittedscheduler dispatchinterrupt / time slice expiredI/O or event waitI/O or event completionexitNewnewReadyreadyRunningrunningWaitingwaitingTerminatedterminated

It called exit() → Terminated; a zombie until the parent calls wait().

There is no Waiting → Running transition: a process whose event completes first enters the ready queue.

States and the PCB

Process States on Linux

ps STATMeaningIn the model
Rrunning or ready to runRunning + Ready
Sinterruptible sleep (waiting for an event)Waiting
Duninterruptible sleep (usually disk I/O)Waiting
Tstopped (SIGSTOP, debugger)suspended
ZzombieTerminated (not reaped)
Iidle kernel thread—
$ ps -eo pid,stat,comm
  PID STAT COMMAND
    1 Ss   systemd
  890 Ss   sshd
 2105 Ss   bash
 2241 R+   ps
 2307 Z    writer <defunct>

Extra letters: s session leader, + foreground group. Linux does not distinguish ready from running; both are R.

States and the PCB

The Process Control Block (PCB)

process stateprocess ID (PID)program counterregistersscheduling informationmemory-management informationlist of open filesaccounting information (CPU time)• • •context(context)
  • The kernel keeps a PCB (process control block) for each process; everything that must be known about the process is here.
  • The structure formed by all the PCBs is the process table (Tanenbaum's table lists the fields of these entries).
  • When a process leaves the CPU, its program counter and registers are saved in the PCB; when it returns, they are loaded from there. This information is the context.
  • PCBs live in kernel memory; user processes cannot access them directly.

States and the PCB

PCB Fields (after Tanenbaum)

Process managementMemory managementFile management
registers · program counter · program status word (PSW) · stack pointer · process state · priority · scheduling parameters · PID · parent process · process group · signals · start time · CPU time used · children's CPU time · time of next alarmpointers to the text, data and stack segments; today a page-table pointer and a list of memory regionsroot directory · working directory · file descriptors · user ID (UID) · group ID (GID)
PSW

Status flags (zero, overflow...), execution mode (user/kernel), interrupt enables.

Process group

Processes managed together: a shell pipeline forms one group, and Ctrl+C goes to all of them.

Next alarm

The delivery time of a SIGALRM requested with alarm().

process table141210331034…array or list indexed by PIDprocess: state, registers, prioritymemory: page-table pointerfile: open files, working directoryPCB (PID 1033)

States and the PCB

The PCB on Linux: task_struct

struct task_struct {          /* <linux/sched.h> */
    pid_t pid;                     /* process ID */
    unsigned int __state;          /* process state */
    int prio;                      /* priority */
    struct sched_entity se;        /* CFS/EEVDF info */
    struct task_struct *parent;    /* parent process */
    struct list_head children;     /* children */
    struct files_struct *files;    /* open files */
    struct mm_struct *mm;          /* address space */
    /* ... hundreds more fields ... */
};
task_structPID 1task_structPID 2105task_structPID 2241currenttasks: doubly linked list
  • All tasks are on a doubly linked list; current points to the task running on that core.
  • The long state / time_slice fields of older kernels have moved into __state and sched_entity in current kernels.

Scheduling

Ready and Wait Queues

ready queuehead · tailPCB 7PCB 2PCB 9disk I/O waithead · tailPCB 3PCB 14network / keyboard waithead · tailPCB 6CPUPCB 5 runningqueues are kept as linked lists of PCBs
  • The scheduler picks which ready process gets the CPU next; the goal is to keep the CPU busy and respond quickly to users.
  • Ready queue: processes in memory waiting to execute. Wait queues: processes waiting for a specific event (I/O completion, a child terminating...); there is a separate queue for each kind of event.
  • Linux gives each core its own runqueue; CFS/EEVDF keeps it in a red-black tree.

Scheduling

Queueing Diagram: A Process's Journey

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

A new process is added to the tail of the ready queue and waits its turn.

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

The scheduler dispatches it: the process runs on the CPU.

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

It made an I/O request: it moves to the I/O wait queue and returns to the ready queue when the I/O completes.

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

Time slice expired: a timer interrupt takes it off the CPU and it goes straight back to the ready queue.

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

It created a child and called wait(): it waits until the child finishes.

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

It waits for an interrupt/event (e.g. sleep(), a network packet).

ready queueCPUterminate (exit)I/O wait queueI/O requesttime slice expiredwait for child to finishcreate child (fork)interrupt wait queuewait for interrupt

Its work is done: it leaves the system with exit(); its PCB is removed when the parent calls wait.

Scheduling

CPU-Bound and I/O-Bound Processes

(a) CPU-bound(b) I/O-boundCPU burstI/O waittime
CPU-bound

Long computation bursts, infrequent I/O: compiling, video encoding, scientific computing.

I/O-bound

Short CPU bursts, frequent waits: editors, web servers, database clients.

  • Because CPUs improve much faster than disks, processes are becoming more and more I/O-bound.
  • An I/O-bound process that becomes ready should get the CPU at once (fast context switch); a good mix of both kinds is needed.

Scheduling

Modeling Multiprogramming

204060801000246810processes in memory (degree of multiprogramming) nCPU %20% I/O wait50% I/O wait80% I/O wait

Suppose a process spends a fraction p of its time waiting for I/O. With n independent processes in memory, the probability that all are waiting at once is pⁿ:

CPU utilization = 1 − pⁿ

E.g. p = 0.8: n = 1 → 20%, n = 4 → 59%, n = 10 → 89%.

A rough model: it assumes the processes are independent.

Scheduling · Step through the code

Code Trace: CPU Utilization 1 − pⁿ

double p = 0.8;      /* I/O fraction */
double pn = 1.0;
for (int n = 1; n <= 5; n++) {
  pn = pn * p;       /* all waiting */
  double util = 1 - pn;
  printf("n=%d %.0f%%\n", n, util * 100);
}
0%50%100%n=1n=2n=3n=4n=5idle probability pⁿ = —

Each process spends 80% of its time waiting for I/O. The CPU is idle only when all processes in memory are waiting at once.

0%50%100%n=1n=2n=3n=4n=5idle probability pⁿ = 0.8

n = 1: a single process; the probability that the CPU is idle is pⁿ = 0.8.

0%50%100%n=120%freen=2n=3n=4n=5idle probability pⁿ = 0.8

util = 1 − 0.8 = 20%: on a uniprogrammed system the CPU is idle four-fifths of the time.

0%50%100%n=120%n=2n=3n=4n=5idle probability pⁿ = 0.64

n = 2: the probability that all wait at once is 0.82 = 0.64.

0%50%100%n=120%n=236%freen=3n=4n=5idle probability pⁿ = 0.64

util = 36%.

0%50%100%n=120%n=236%n=3n=4n=5idle probability pⁿ = 0.512

n = 3: the probability that all wait at once is 0.83 = 0.512.

0%50%100%n=120%n=236%n=349%freen=4n=5idle probability pⁿ = 0.512

util = 48.8%.

0%50%100%n=120%n=236%n=349%n=4n=5idle probability pⁿ = 0.4096

n = 4: the probability that all wait at once is 0.84 = 0.4096.

0%50%100%n=120%n=236%n=349%n=459%freen=5idle probability pⁿ = 0.4096

util = 59.04%.

0%50%100%n=120%n=236%n=349%n=459%n=5idle probability pⁿ = 0.3277

n = 5: the probability that all wait at once is 0.85 = 0.32768.

0%50%100%n=120%n=236%n=349%n=459%n=567%freeidle probability pⁿ = 0.3277

util = 67.23%. Each new process adds less than the one before: an increase of 8 points.

Scheduling

Context Switch

process P₀operating systemprocess P₁time

P₀ is running (thick line); P₁ waits in the ready queue.

process P₀operating systemprocess P₁interrupt or system callsave state into PCB₀time

Interrupt or system call: the CPU enters kernel mode and P₀'s program counter and registers are saved in PCB₀.

process P₀operating systemprocess P₁interrupt or system callsave state into PCB₀reload state from PCB₁time

The scheduler picks P₁; P₁'s context is loaded from PCB₁ (the address space changes too).

process P₀operating systemprocess P₁interrupt or system callsave state into PCB₀reload state from PCB₁idletime

P₁ is running; P₀ is idle (ready or waiting).

process P₀operating systemprocess P₁interrupt or system callsave state into PCB₀reload state from PCB₁save state into PCB₁time

Another interrupt: P₁'s state is saved in PCB₁.

process P₀operating systemprocess P₁interrupt or system callsave state into PCB₀reload state from PCB₁save state into PCB₁reload state from PCB₀time

Reloaded from PCB₀; P₀ resumes where it left off. No user work is done in the gray parts in between: overhead.

Scheduling · Step through the code

Code Trace: Context Switch

struct pcb { int pc, sp, r1; char state; };
struct pcb P[2];   /* process table */
int pc, sp, r1;    /* CPU registers */
void context_switch(int from, int to) {
  P[from].pc = pc;  P[from].sp = sp;  /* save */
  P[from].r1 = r1;  P[from].state = 'R';
  pc = P[to].pc;  sp = P[to].sp;  /* load */
  r1 = P[to].r1;  P[to].state = 'X';
}
/* interrupt */  context_switch(0, 1);
/* ... P1 runs ... */
/* interrupt */  context_switch(1, 0);
CPU (P₀)pc104sp896r18PCB₀ runningpc—sp—r1—PCB₁ readypc340sp500r12

P₀ is running: the registers hold P₀'s values. P₁'s last state is saved in PCB₁. A timer interrupt arrives; the kernel decides to switch to P₁.

CPU (P₀)pc104sp896r18PCB₀ runningpc104sp896r1—PCB₁ readypc340sp500r12save

Save: P₀'s pc and sp are written to PCB₀. If they were lost, P₀ could not return to where it was.

CPU (P₀)pc104sp896r18PCB₀ readypc104sp896r18PCB₁ readypc340sp500r12save

The general register is saved too; P₀ is now ready (R).

CPU (P₁)pc340sp500r18PCB₀ readypc104sp896r18PCB₁ readypc340sp500r12load

Load: P₁'s pc and sp go into the CPU. From now on the CPU uses P₁'s code and stack.

CPU (P₁)pc340sp500r12PCB₀ readypc104sp896r18PCB₁ runningpc340sp500r12load

r1 = 2 is loaded; P₁ is running (X). No user work was done meanwhile: a context switch is pure overhead.

CPU (P₁)pc352sp492r15PCB₀ readypc104sp896r18PCB₁ runningpc340sp500r12

P₁ ran a few instructions: pc 340 → 352, r1 = 5, sp = 492. PCB₁ is out of date now; the current values exist only in the CPU.

CPU (P₁)pc352sp492r15PCB₀ readypc104sp896r18PCB₁ runningpc340sp500r12

Another interrupt: the kernel decides to return to P₀.

CPU (P₁)pc352sp492r15PCB₀ readypc104sp896r18PCB₁ runningpc352sp492r12save

P₁'s pc and sp are saved in PCB₁.

CPU (P₁)pc352sp492r15PCB₀ readypc104sp896r18PCB₁ readypc352sp492r15save

r1 is saved as well; P₁ is ready.

CPU (P₀)pc104sp896r15PCB₀ readypc104sp896r18PCB₁ readypc352sp492r15

The values in PCB₀ are restored: pc = 104, sp = 896.

CPU (P₀)pc104sp896r18PCB₀ runningpc104sp896r18PCB₁ readypc352sp492r15

r1 = 8: P₀ continues exactly where it was interrupted, at pc = 104, without noticing. Real CPUs have dozens of registers, and the address space changes too.

Scheduling

Limited Direct Execution

The program runs directly on the CPU; privileged work happens in the kernel, and the OS takes the CPU back by force.

OS (kernel mode)HardwareProgram (user mode)bootrunset up trap tableremember handler addressesstart the timerinterrupt every few ms

At boot, in kernel mode: the OS tells the hardware which code runs on which event (the trap/interrupt table) and sets up the timer. Both are privileged instructions.

OS (kernel mode)HardwareProgram (user mode)bootrunset up trap tableremember handler addressesstart the timerinterrupt every few msA running (directly on the CPU)

A runs directly on the CPU in user mode: no interpreter in between, full speed. For privileged work it must trap with a system call.

OS (kernel mode)HardwareProgram (user mode)bootrunset up trap tableremember handler addressesstart the timerinterrupt every few msA running (directly on the CPU)timer interruptregs(A) → kernel stack

Even if A is in an infinite loop, the interrupt comes. The hardware saves A's registers, enters kernel mode and jumps to the OS handler: the OS is in control.

OS (kernel mode)HardwareProgram (user mode)bootrunset up trap tableremember handler addressesstart the timerinterrupt every few msA running (directly on the CPU)timer interruptregs(A) → kernel stackscheduler picks Bswitch(): A → PCB(A), load B

A's slice is up, so the OS decides to switch to B. switch() writes A's context to PCB(A), loads B's and moves to B's kernel stack.

OS (kernel mode)HardwareProgram (user mode)bootrunset up trap tableremember handler addressesstart the timerinterrupt every few msA running (directly on the CPU)timer interruptregs(A) → kernel stackscheduler picks Bswitch(): A → PCB(A), load Breturn-from-trapregs(B), user modeB running

Return-from-trap loads B's registers as if B were returning from where it was interrupted, and enters user mode: now B runs.

Without the interrupt (cooperative multitasking, Windows 3.x), an infinite loop would hang the machine.

Scheduling

The Cost of a Context Switch

  • When the CPU switches to another process it must save the old process's state and load the new one's.
  • This time is pure overhead: the system does no useful work meanwhile.
  • The more complex the PCB and the OS, the longer a context switch takes.
Direct cost

Saving/loading registers, kernel code: typically a few microseconds on current Linux.

Indirect cost

Caches and the TLB “go cold”: the new process takes more cache misses at first; this often exceeds the direct cost.

Hardware support

Multiple register sets; address-space tags in the TLB (ASID / PCID) to avoid flushing.

Scheduling

Handling an Interrupt

What happens at the lowest level of the OS when an I/O interrupt arrives (Tanenbaum):

1. Hardware stacks the program counter, PSW, etc.

The interrupt request is accepted at an instruction boundary; the CPU enters kernel mode.

2. Hardware loads a new program counter from the interrupt vector.

The vector is a table holding the handler address for each kind of interrupt.

3. An assembly routine saves the registers.

The registers are written to the current process's PCB (or the kernel stack).

4. An assembly routine sets up a new stack.

The interrupt handler runs on the kernel stack.

5. The C interrupt service routine runs.

It usually reads/buffers input and makes the waiting process ready.

6. The scheduler picks the next process.

If the woken process has higher priority, it may be chosen instead of the interrupted one.

7. The C routine returns to the assembly code.

What remains is loading the chosen process's context.

8. The assembly routine starts the new process.

Registers and the memory map are loaded and control returns to user mode.

Interprocess Communication

Interprocess Communication

  • Processes can be independent (unaffected by others) or cooperating (sharing data).
  • Reasons to cooperate: information sharing, speedup (splitting the work), modularity, security and isolation (Chrome).
  • Cooperating processes need an IPC (interprocess communication) mechanism.

Three basic questions

  1. How does one process pass information to another?
  2. How do we keep processes out of each other's way: how do we prevent two bookings for the same seat?
  3. How do we ensure the right order when there are dependencies? (Don't fire before aiming; don't consume before producing.)
Two basic models: shared memory and message passing. The last two questions call for synchronization: uncontrolled access to shared data makes the result depend on execution order (a race condition). The same questions apply to threads.

Interprocess Communication

Shared Memory and Message Passing

(a) shared memory(b) message passingprocess Ashared regionmapped into both address spacesprocess Bkernelshm_open + mmapprocess Aprocess Bkernel: message queuem₀m₁m₂

Shared memory: the kernel creates a region and maps it into both processes' address spaces (system calls only during setup).

(a) shared memory(b) message passingprocess Ashared regionmapped into both address spacesprocess Bkernelwriteprocess Aprocess Bkernel: message queuem₀m₁m₂

A writes directly into the region: the kernel is not involved, so this is very fast.

(a) shared memory(b) message passingprocess Ashared regionmapped into both address spacesprocess Bkernelreadprocess Aprocess Bkernel: message queuem₀m₁m₂

B reads the same data. But the processes must handle synchronization themselves (who writes when?).

(a) shared memory(b) message passingprocess Ashared regionmapped into both address spacesprocess Bkernelprocess Aprocess Bkernel: message queuem₀m₁m₂m₃send(m₃)

Message passing: A says send(m₃); the kernel copies the message into its own queue.

(a) shared memory(b) message passingprocess Ashared regionmapped into both address spacesprocess Bkernelprocess Aprocess Bkernel: message queuem₁m₂m₃receive() → m₀

B takes the next message with receive(). Each message costs a system call and a copy, but the kernel provides synchronization, and it works over a network too.

Interprocess Communication

POSIX Shared Memory

/* producer */
int fd = shm_open("/os03", O_CREAT|O_RDWR, 0600);
ftruncate(fd, 4096);
char *p = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
               MAP_SHARED, fd, 0);
strcpy(p, "hello");     /* direct write */

/* consumer (another process) */
int fd = shm_open("/os03", O_RDONLY, 0);
char *p = mmap(NULL, 4096, PROT_READ,
               MAP_SHARED, fd, 0);
printf("%s\n", p);
shm_unlink("/os03");    /* remove */
  • shm_open creates a named object (under /dev/shm on Linux).
  • ftruncate sets the size, mmap maps it into the address space.
  • After that it is ordinary memory access.
  • If the consumer reads before the producer writes, it sees nothing: real code needs synchronization such as a semaphore.
  • The older System V API: shmget/shmat.
producer processcodedatap = 0x7f3a…stackconsumer processcodedatap = 0x7f91…stack/os03 (4096 B)"hello"physical memory (/dev/shm)mmapmmap

Interprocess Communication

Message-Passing Design

Two basic operations: send(message) and receive(message). First a communication link must be set up.

AspectOptions
Physical linkshared memory, hardware bus, network
Namingdirect (process name) · indirect (mailbox / port)
Synchronous / asynchronousblocking (synchronous): the sender waits until the message is received, the receiver until one arrives · nonblocking (asynchronous): returns at once
Bufferingzero capacity (sender waits: rendezvous) · bounded (waits if the queue is full) · unbounded (never waits)
Blocking send + blocking receive = rendezvous: the producer–consumer problem solves itself. Examples: POSIX mq_send/mq_receive, Mach ports, Windows ALPC.

Interprocess Communication

Direct and Indirect Communication

direct: send(Q, m)PQone link · one pair of processesindirect: send(A, m)mailbox Aport / queuePQRmany processes · many links
Direct
  • send(P, m): send to P; receive(Q, m): receive from Q.
  • The link is set up automatically and belongs to exactly one pair of processes.
  • One link between each pair; usually bidirectional.
Indirect (mailbox)
  • send(A, m) / receive(A, m): through mailbox A.
  • Each mailbox has a unique ID; processes sharing a mailbox can communicate.
  • A link may involve many processes, and a pair may have many links; uni- or bidirectional.

Interprocess Communication

Pipes

parentfd[0] closed · fd[1] writeschildfd[0] reads · fd[1] closedpipe: FIFO buffer in the kernelwrite(fd[1])read(fd[0])write end → read end (one-way)

Design questions

  • One-way (simplex) or two-way?
  • If two-way, half or full duplex?
  • Must the processes be parent and child?
  • Can it be used over a network?
Ordinary (anonymous)Named (FIFO)
Accessonly the creator and its childrenany process that knows the file name
Directionone-wayUNIX: half duplex; Windows: full duplex
Lifetimeends when the processes close itmkfifo; stays in the file system
NetworknoUNIX no; Windows yes

Interprocess Communication · Step through the code

Code Trace: pipe() + fork()

int main(void) {
  int fd[2];
  char buf[32];
  pipe(fd);
  pid_t pid = fork();
  if (pid == 0) {   /* child: reads */
    close(fd[1]);
    ssize_t n = read(fd[0], buf, sizeof buf - 1);
    buf[n] = '\0';
    printf("child got: %s\n", buf);
    close(fd[0]);
    return 0;
  }
  close(fd[0]);     /* parent: writes */
  write(fd[1], "welcome", 7);
  close(fd[1]);
  wait(NULL);
  return 0;
}
parent 1234running(empty)4 (w)3 (r)

pipe(fd): the kernel opens an empty buffer; read end fd[0]=3, write end fd[1]=4 (0–2 are stdin/stdout/stderr).

parent 1234runningchild 1235running(empty)4 (w)3 (r)4 (w)3 (r)

fork(): the child inherits the descriptors; both ends are now open in both processes.

parent 1234runningchild 1235running(empty)4 (w)3 (r)3 (r)

The child closes the write end it will not use.

parent 1234runningchild 1235read → waiting(empty)4 (w)3 (r)3 (r)

The pipe is empty: read blocks the child.

parent 1234runningchild 1235waiting(empty)4 (w)3 (r)

The parent closes the read end.

parent 1234runningchild 1235wokenwelcome4 (w)3 (r)

The parent writes 7 bytes; writes of ≤ PIPE_BUF are not split (atomic). The waiting reader wakes up.

parent 1234wait → waitingchild 1235readywelcome3 (r)

The parent closes the write end (so the reader can later see EOF) and waits with wait.

parent 1234waitingchild 1235running(empty)3 (r)

read returns 7; the data is copied from the buffer into the child's buf array.

parent 1234waitingchild 1235running(empty)3 (r)

A terminating '\0' is added and the string is printed.

parent 1234runningchild 1235done(empty)

The child closes the read end and exits; the parent returns from wait and finishes. Since only the child prints, the output is deterministic.

Interprocess Communication

A Shell Pipeline: ls | wc -l

bashpipe(), 2 × fork(), waitlsdup2(fd[1], 1) → stdoutwc -ldup2(fd[0], 0) → stdinpipe (64 KiB buffer)both processes must close the ends they don't use, or wc never sees EOF
int fd[2]; pipe(fd);
if (fork() == 0) { dup2(fd[1], 1); close(fd[0]); close(fd[1]);
                   execlp("ls", "ls", NULL); }
if (fork() == 0) { dup2(fd[0], 0); close(fd[0]); close(fd[1]);
                   execlp("wc", "wc", "-l", NULL); }
close(fd[0]); close(fd[1]); wait(NULL); wait(NULL);

Error checks omitted for brevity. dup2(old, new) makes the new descriptor a copy of the old one; programs work with stdin/stdout, so they never notice the pipe.

Interprocess Communication

Sockets

client 146.86.5.20socket146.86.5.20:1625web server 161.25.19.8socket161.25.19.8:80TCP connectionephemeral port ↔ well-known port
  • Socket: an endpoint for communication, identified by IP address + port (161.25.19.8:80).
  • Communication takes place between a pair of sockets; the port identifies the service on the machine.
  • Ports below 1024 are reserved for well-known services (SSH 22, HTTP 80, HTTPS 443); binding them as a server requires privilege.
  • 127.0.0.1 (IPv6: ::1) is the loopback address: processes on the same machine.
  • TCP is a connection-oriented, ordered, reliable byte stream; UDP is connectionless datagrams.
  • On the same machine, UNIX domain sockets (AF_UNIX) are faster; Docker, systemd and X/Wayland use them.

Mobile Systems

Multitasking on Mobile Systems

iOS / iPadOS
  • Early versions (2007) allowed only one user app at a time; iOS 4 (2010) brought limited multitasking.
  • Only permitted work runs in the background: audio playback, location, VoIP, BGTaskScheduler tasks; everything else is suspended.
Android
  • Allows background processes; long work continues in a service component even while the UI is suspended.
  • Background limits since Android 8: a foreground service with a notification, or WorkManager, for long work.
foregroundthe activity the user is interacting withvisibleon screen but not in focusservicee.g. background synccachedbackground + empty processeswhen memory runs low, the lowest are killed first (lmkd)

The limits exist because of scarce memory and battery. The “background” and “empty” processes of older docs are now grouped as cached; when memory runs low, lmkd kills from the bottom up.

Summary

Summary

Process

A program in execution: text, data, heap, stack + CPU context + resources. The kernel tracks it with a PCB.

Life cycle

New → Ready ⇄ Running → Waiting → Ready … → Terminated. The exit status is collected with wait.

UNIX API

fork copies (COW), exec replaces the image, wait waits and reaps, exit ends.

Scheduling machinery

Ready/wait queues; a context switch is pure overhead.

IPC

Shared memory is fast but needs synchronization; message passing, pipes and sockets go through the kernel.

Mobile multitasking

Because of scarce memory and battery, background processes are suspended and killed if needed.

Summary

Review Questions

  1. How many processes in total does a program running fork(); fork(); end up with? And for (i = 0; i < 3; i++) fork();?
  2. What is the output of printf("A"); fork(); printf("B\n"); when stdout is redirected to a file? Why?
  3. Can a process go directly from Waiting to Running?
  4. What is the difference between a zombie and an orphan? Which causes more trouble for the system?
  5. If exec succeeds, when does the line after it run?
  6. What is the CPU utilization for p = 0.5 and n = 3?
  7. In the pipe example, what would happen if the parent did not close(fd[1]) and the child read until EOF?
Show answers
  1. 4 processes (1 → 2 → 4). The loop gives 2³ = 8 processes (7 new).
  2. AB twice: writing to a file, stdout is fully buffered; "A" is still in the buffer at fork time, so it is copied into the child too.
  3. No; it first moves to Ready, then the scheduler picks it.
  4. A zombie has finished but not been reaped; an orphan is running but has no parent (init adopts it). Accumulated zombies use up PIDs.
  5. Never; the line runs only if exec fails.
  6. 1 − 0,5³ = 0,875 → 87%,5.
  7. Since the write end stays open in the parent, read never returns 0; the child waits forever, and the parent waits in wait.

Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 — Chapter 3: Processes.
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (5th ed. 2023), Pearson — Chapter 2: Processes and Threads.
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), pages.cs.wisc.edu/~remzi/OSTEP — Processes, Process API.
  • Linux man pages: fork(2), execve(2), wait(2), pipe(7), signal(7), shm_overview(7).
  • The Chromium Projects, “Multi-process Architecture” and “Site Isolation”; Android Developers, “Processes and app lifecycle”.

The End

Chapter 3: Processes
Previous: Chapter 2: Operating-System Structures · Next: Chapter 4: Threads
1 / 1