Chapter 3: Processes
Introduction
Learning Outcomes
Explain the difference between a program and a process, and the memory layout of a process.
Describe process states, transitions and the contents of the PCB.
Trace small programs that use fork(), exec(), wait(), exit() and pipe().
Explain the ready/wait queues and the cost of a context switch.
Compare shared memory, message passing, pipes and sockets.
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 (
vimin 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.
The Process Concept
Process Layout in Memory
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.
Text: the compiled instructions of main; read-only and shareable among processes of the same program.
Data: the initialized global counter; its value is stored in the executable file.
BSS: uninitialized global and static variables; the loader fills them with zeros and they take no space in the file.
Stack: the call frame of main: parameters, local variables and the pointer p itself.
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
- 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
Several tasks make progress over the same time interval; on one core by interleaving.
Several tasks execute at the same instant; this needs multiple cores or processors.
The Process Concept
Example: Chrome's Multiprocess Architecture
Process Creation
Process Creation
Events that create processes
- System initialization (init/systemd, background services: daemons).
- A running process making a process-creation system call.
- A user starting a command or application.
- 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
| System | Call |
|---|---|
| UNIX / Linux | fork() + exec(); also posix_spawn(), and clone() on Linux |
| Windows | CreateProcess(): creates the process and loads the program in one call |
Process Creation
Process Tree
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
The child may share all, some or none of the parent's resources. In UNIX the child inherits the open file descriptors.
Parent and child can run concurrently, or the parent can wait for the child to finish with wait().
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().
getpid() returns the process's own PID, getppid() its parent's.
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 */ }
Start: the parent's page table points to physical pages A, B, C.
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.
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; }
There is one process: the parent (PID 1234), x = 10.
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.)
In the parent pid > 0: both ifs are skipped, and wait(NULL) puts the parent in the waiting state until the child finishes.
In the child pid == 0 is true: it enters the child branch. (Had the child run first, the result would be the same.)
The child changes its own copy: x = 15. The parent's x stays 10.
The child prints.
The child exits with return 0 (exit(0)); it is briefly a zombie, and the kernel wakes the parent.
wait returns 1235; the child's entry is removed from the process table.
The parent prints: its own x is still 10.
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");
wait() (or another form of synchronization).Process Creation
The exec() Family
| Call | Arguments | PATH search | Environment |
|---|---|---|---|
execl / execv | list / array | no | inherited |
execlp / execvp | list / array | yes | inherited |
execle / execve | list / array | no | passed 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?
#! line).If the file is missing or not executable, exec returns −1 and the old program carries on.
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.
Caught signals revert to the default action, since the handler code is no longer in memory; ignored signals stay ignored.
Nothing is copied: the first use of a page causes a page fault and that page is brought in from disk (lazy loading).
The argv and envp strings, the pointer arrays to them and argc are placed at the top of the stack.
_start sets up the C runtime and calls main(argc, argv, envp). The PID never changed.
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; }
fork(): two processes are running the same a.out image. Had pid < 0, line 8 would print the error and exit.
The parent skips the child branch and blocks in waitpid(1235, ...).
The child calls execlp: ls is found on PATH and the address space is replaced by /usr/bin/ls. The PID is still 1235.
The child is now the ls program; lines 11–12 never run (exec succeeded). It writes the listing to the inherited stdout.
ls ends with exit(0); waitpid returns the child's PID (no error) and st carries the exit status.
The child exited normally (WIFEXITED is true): WEXITSTATUS(st) = 0 is printed.
Process Creation
The Shell Loop: fork → exec → wait
The shell shows a prompt and reads the ls command.
fork(): a copy of the shell (PID 2300) is created; the parent starts waiting with waitpid.
The child calls execvp("ls", argv); its image is replaced by ls.
ls runs and writes to the terminal stdout it inherited.
ls exits; the kernel notifies the parent with SIGCHLD and waitpid reaps the child.
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; }
fork(): the child copies the parent's fd table; 0, 1, 2 are the terminal.
The parent waits in waitpid; the next steps change only the child's table.
close(1): entry 1 in the child's table is freed. The parent's stdout is still the terminal.
open always returns the lowest free number: 1. The child's stdout is now the file.
exec replaces code and data but keeps the fd table: unaware, wc writes to fd 1, that is, to the file.
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
| Cause | Kind | Example |
|---|---|---|
| Normal exit | voluntary | return 0, exit(0); the compiler finished its job |
| Error exit | voluntary | file not found → exit(1) |
| Fatal error | involuntary | invalid memory access (SIGSEGV), divide by zero (SIGFPE) |
| Killed by another process | involuntary | kill(pid, SIGTERM), TerminateProcess() on Windows |
exit(status): the C library flushes buffers, runs theatexithandlers, then makes the_exitsystem call.abort(): the calling process sends itself SIGABRT and ends abnormally (a core dump may be produced). To terminate another process, usekill().- 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
| Error | Signal |
|---|---|
| Invalid/unauthorized memory access | SIGSEGV |
| Integer divide by zero | SIGFPE |
| Illegal instruction | SIGILL |
| Alignment / bus error | SIGBUS |
| Writing to a closed pipe | SIGPIPE |
- 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
coredumpctlorgdb a.out core(crash-dump analysis).
Process Termination
Termination by Signal: kill, pkill, killall
| Signal | No | Meaning |
|---|---|---|
| SIGTERM | 15 | finish politely; the process can catch it and clean up (the default for kill) |
| SIGKILL | 9 | force termination; cannot be caught or ignored, no cleanup |
| SIGINT | 2 | Ctrl+C in the terminal |
| SIGTSTP | 20 | Ctrl+Z in the terminal (suspend); fg/bg resume it |
| SIGSTOP / SIGCONT | 19 / 18 | stop (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
Parent (100) and child (101) are running.
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).
The parent calls wait() and collects the exit status; the kernel removes the entry (reaping). No zombie remains.
Scenario 2: the parent (100) terminated before the child. The child (101) is still running: an orphan process.
The kernel reparents the orphan to init/systemd (PID 1) or a designated subreaper process; getppid() now returns 1.
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)
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
The user started a program: the PCB is being created and memory allocated → New.
The kernel admitted the process and put it in the ready queue → Ready.
The scheduler picked this process and its context was loaded → Running.
The process asked to read() from disk; it gives up the CPU until the data arrives → Waiting.
Disk interrupt: the data is ready. The process does not run immediately; it returns to the ready queue → Ready.
Picked again → Running.
Timer interrupt: the time slice expired (preemptive scheduling) → Ready.
Picked once more → Running.
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 STAT | Meaning | In the model |
|---|---|---|
R | running or ready to run | Running + Ready |
S | interruptible sleep (waiting for an event) | Waiting |
D | uninterruptible sleep (usually disk I/O) | Waiting |
T | stopped (SIGSTOP, debugger) | suspended |
Z | zombie | Terminated (not reaped) |
I | idle 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)
- 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 management | Memory management | File 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 alarm | pointers to the text, data and stack segments; today a page-table pointer and a list of memory regions | root directory · working directory · file descriptors · user ID (UID) · group ID (GID) |
Status flags (zero, overflow...), execution mode (user/kernel), interrupt enables.
Processes managed together: a shell pipeline forms one group, and Ctrl+C goes to all of them.
The delivery time of a SIGALRM requested with alarm().
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 ... */ };
- All tasks are on a doubly linked list;
currentpoints to the task running on that core. - The
long state/time_slicefields of older kernels have moved into__stateandsched_entityin current kernels.
Scheduling
Ready and Wait Queues
- 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
A new process is added to the tail of the ready queue and waits its turn.
The scheduler dispatches it: the process runs on the CPU.
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.
Time slice expired: a timer interrupt takes it off the CPU and it goes straight back to the ready queue.
It created a child and called wait(): it waits until the child finishes.
It waits for an interrupt/event (e.g. sleep(), a network packet).
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
Long computation bursts, infrequent I/O: compiling, video encoding, scientific computing.
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
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); }
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.
n = 1: a single process; the probability that the CPU is idle is pⁿ = 0.8.
util = 1 − 0.8 = 20%: on a uniprogrammed system the CPU is idle four-fifths of the time.
n = 2: the probability that all wait at once is 0.82 = 0.64.
util = 36%.
n = 3: the probability that all wait at once is 0.83 = 0.512.
util = 48.8%.
n = 4: the probability that all wait at once is 0.84 = 0.4096.
util = 59.04%.
n = 5: the probability that all wait at once is 0.85 = 0.32768.
util = 67.23%. Each new process adds less than the one before: an increase of 8 points.
Scheduling
Context Switch
P₀ is running (thick line); P₁ waits in the ready queue.
Interrupt or system call: the CPU enters kernel mode and P₀'s program counter and registers are saved in PCB₀.
The scheduler picks P₁; P₁'s context is loaded from PCB₁ (the address space changes too).
P₁ is running; P₀ is idle (ready or waiting).
Another interrupt: P₁'s state is saved in PCB₁.
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);
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₁.
Save: P₀'s pc and sp are written to PCB₀. If they were lost, P₀ could not return to where it was.
The general register is saved too; P₀ is now ready (R).
Load: P₁'s pc and sp go into the CPU. From now on the CPU uses P₁'s code and stack.
r1 = 2 is loaded; P₁ is running (X). No user work was done meanwhile: a context switch is pure overhead.
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.
Another interrupt: the kernel decides to return to P₀.
P₁'s pc and sp are saved in PCB₁.
r1 is saved as well; P₁ is ready.
The values in PCB₀ are restored: pc = 104, sp = 896.
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.
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.
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.
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.
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.
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.
Saving/loading registers, kernel code: typically a few microseconds on current Linux.
Caches and the TLB “go cold”: the new process takes more cache misses at first; this often exceeds the direct cost.
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):
The interrupt request is accepted at an instruction boundary; the CPU enters kernel mode.
The vector is a table holding the handler address for each kind of interrupt.
The registers are written to the current process's PCB (or the kernel stack).
The interrupt handler runs on the kernel stack.
It usually reads/buffers input and makes the waiting process ready.
If the woken process has higher priority, it may be chosen instead of the interrupted one.
What remains is loading the chosen process's context.
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
- How does one process pass information to another?
- How do we keep processes out of each other's way: how do we prevent two bookings for the same seat?
- How do we ensure the right order when there are dependencies? (Don't fire before aiming; don't consume before producing.)
Interprocess Communication
Shared Memory and Message Passing
Shared memory: the kernel creates a region and maps it into both processes' address spaces (system calls only during setup).
A writes directly into the region: the kernel is not involved, so this is very fast.
B reads the same data. But the processes must handle synchronization themselves (who writes when?).
Message passing: A says send(m₃); the kernel copies the message into its own queue.
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_opencreates a named object (under/dev/shmon Linux).ftruncatesets the size,mmapmaps 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.
Interprocess Communication
Message-Passing Design
Two basic operations: send(message) and receive(message). First a communication link must be set up.
| Aspect | Options |
|---|---|
| Physical link | shared memory, hardware bus, network |
| Naming | direct (process name) · indirect (mailbox / port) |
| Synchronous / asynchronous | blocking (synchronous): the sender waits until the message is received, the receiver until one arrives · nonblocking (asynchronous): returns at once |
| Buffering | zero capacity (sender waits: rendezvous) · bounded (waits if the queue is full) · unbounded (never waits) |
mq_send/mq_receive, Mach ports, Windows ALPC.Interprocess Communication
Direct and Indirect Communication
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.
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
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) | |
|---|---|---|
| Access | only the creator and its children | any process that knows the file name |
| Direction | one-way | UNIX: half duplex; Windows: full duplex |
| Lifetime | ends when the processes close it | mkfifo; stays in the file system |
| Network | no | UNIX 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; }
pipe(fd): the kernel opens an empty buffer; read end fd[0]=3, write end fd[1]=4 (0–2 are stdin/stdout/stderr).
fork(): the child inherits the descriptors; both ends are now open in both processes.
The child closes the write end it will not use.
The pipe is empty: read blocks the child.
The parent closes the read end.
The parent writes 7 bytes; writes of ≤ PIPE_BUF are not split (atomic). The waiting reader wakes up.
The parent closes the write end (so the reader can later see EOF) and waits with wait.
read returns 7; the data is copied from the buffer into the child's buf array.
A terminating '\0' is added and the string is printed.
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
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
- 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
- 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,
BGTaskSchedulertasks; everything else is suspended.
- 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.
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
A program in execution: text, data, heap, stack + CPU context + resources. The kernel tracks it with a PCB.
New → Ready ⇄ Running → Waiting → Ready … → Terminated. The exit status is collected with wait.
fork copies (COW), exec replaces the image, wait waits and reaps, exit ends.
Ready/wait queues; a context switch is pure overhead.
Shared memory is fast but needs synchronization; message passing, pipes and sockets go through the kernel.
Because of scarce memory and battery, background processes are suspended and killed if needed.
Summary
Review Questions
- How many processes in total does a program running
fork(); fork();end up with? Andfor (i = 0; i < 3; i++) fork();? - What is the output of
printf("A"); fork(); printf("B\n");when stdout is redirected to a file? Why? - Can a process go directly from Waiting to Running?
- What is the difference between a zombie and an orphan? Which causes more trouble for the system?
- If
execsucceeds, when does the line after it run? - What is the CPU utilization for p = 0.5 and n = 3?
- In the pipe example, what would happen if the parent did not
close(fd[1])and the child read until EOF?
Show answers
- 4 processes (1 → 2 → 4). The loop gives 2³ = 8 processes (7 new).
ABtwice: 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.- No; it first moves to Ready, then the scheduler picks it.
- A zombie has finished but not been reaped; an orphan is running but has no parent (init adopts it). Accumulated zombies use up PIDs.
- Never; the line runs only if exec fails.
- 1 − 0,5³ = 0,875 → 87%,5.
- Since the write end stays open in the parent,
readnever returns 0; the child waits forever, and the parent waits inwait.
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”.