Processes and Threads

Process memory layout, process states, fork and exec, threads versus processes, context switches, and IPC.

Est. 40 min Updated Suggest a fix
// Part 01 — Program versus process

A Process Is a Running Instance

A program is passive code and data stored somewhere; a process is an executing instance with an address space and OS-managed resources. Two processes can run the same executable while keeping separate virtual memory, open-file state, credentials, and execution context. Shared resources such as files or shared-memory regions are explicit relationships, not evidence that the processes are one.

A simplified process address space contains executable code, global data, a heap for dynamic allocations, and one or more stacks. The OS maps virtual addresses to physical memory as needed. Layout details vary with the architecture, loader, and runtime, and modern systems may randomize addresses.

RegionTypical contentsLifetime intuition
Text / codeInstructions and read-only constantsLoaded for the process image
DataInitialized and zero-initialized globalsProcess lifetime
HeapDynamic allocationsManaged by allocator / application
StackCall frames and local variables per threadGrows and shrinks with calls
// Part 02 — State and metadata

The Kernel Tracks More Than a PID

A process commonly moves among new, ready/runnable, running, blocked/waiting, and terminated states. A timer interrupt can move a running task back to runnable; requesting disk input can block it; completion can make it runnable again. A process blocked on I/O is not using a CPU, even if it still exists.

The textbook process control block (PCB) is a useful abstraction for kernel-maintained metadata: process identifier, state, saved execution context, scheduling information, memory mappings, open resources, and accounting/security data. Real kernels divide these fields among structures rather than using one universal PCB object. A thread has its own execution context and stack while sharing selected process resources.

bash
# Inspect process IDs, states, and parent-child relationships
ps -eo pid,ppid,stat,comm --sort=pid | head -n 15

# Interactive views; press q to leave
top
💡 Note
A PID is an identifier, not the process itself. It can be reused after a process exits, so robust monitoring tools also consider lifetime and other identity information.
// Part 03 — Create and replace

fork Creates; exec Replaces the Program Image

On POSIX systems, fork() creates a child process that begins as a near-copy of the parent process state. Implementations commonly use copy-on-write mappings: parent and child initially share physical pages until a write requires separation. The child can then call an exec family function to replace its current program image with another executable. fork and exec are distinct operations.

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

int main(void) {
    pid_t pid = fork();
    if (pid < 0) { perror("fork"); return 1; }
    if (pid == 0) {
        execlp("echo", "echo", "child program", (char *)NULL);
        perror("execlp");
        _exit(127);
    }
    int status;
    if (waitpid(pid, &status, 0) < 0) { perror("waitpid"); return 1; }
    printf("child finished; exited=%d
", WIFEXITED(status));
    return 0;
}

After a successful exec, the child no longer runs the old code. The parent waits and collects the child's status, avoiding an unreaped terminated child. Error paths matter: exec returns only when it fails.

⚠️ Important
After fork in a multithreaded process, only the calling thread exists in the child. Until exec, use care with operations that may rely on locks held by vanished threads.
// Part 04 — Threads

Threads Share a Process, Not Every Bit of State

Threads in a process usually share its address space and process resources such as open files, which makes communication cheap. Each thread has its own program counter, registers, scheduling state, and stack. Shared memory makes coordination fast but also creates data races if threads update shared state without synchronization.

These are common OS abstractions; implementation details vary.
PropertySeparate processesThreads in one process
Address spaceNormally isolatedShared
Stack and registersPrivate execution contextPrivate per thread
CommunicationIPC mechanisms or explicit shared mappingShared variables plus synchronization
Failure isolationStronger boundary, not absoluteA bad pointer can damage the process

Concurrency means multiple tasks make progress during overlapping periods; parallelism means they execute simultaneously on multiple CPUs. A single CPU can interleave threads without running them at the same instant.

// Part 05 — Switching and IPC

Context Switching Has a Cost

A context switch saves enough state for one task to resume later and restores another task's state. The kernel may switch on a timer, block, wakeup, or scheduling decision. Switching is useful overhead, not useful application work; caches and translation lookaside buffers can also be affected. A process switch may require different address-space mappings, while switching between threads of one process may preserve them.

Processes communicate using pipes, sockets, signals, message queues, or shared memory. A pipe is a byte stream, not a message protocol; the application must define framing when needed. Shared memory avoids copying some data but needs synchronization and lifecycle management.

pipe.c
c
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>

int main(void) {
    int fd[2];
    if (pipe(fd) < 0) { perror("pipe"); return 1; }
    pid_t pid = fork();
    if (pid < 0) { perror("fork"); return 1; }
    if (pid == 0) {
        close(fd[1]);
        char message[32];
        ssize_t n = read(fd[0], message, sizeof message);
        if (n > 0) write(STDOUT_FILENO, message, (size_t)n);
        close(fd[0]);
        _exit(0);
    }
    close(fd[0]);
    const char message[] = "hello child
";
    write(fd[1], message, strlen(message));
    close(fd[1]);
    waitpid(pid, NULL, 0);
    return 0;
}

Closing unused pipe ends is essential: the reader sees end-of-file only when all write descriptors are closed. For production, handle partial reads and writes, interruptions, and every error path.

Choose IPC based on the communication pattern. A pipe or socket is convenient for streams and preserves process address-space separation. A message queue keeps message boundaries. Shared memory can reduce copying for large data, but the application must design synchronization, ownership, and crash cleanup. Signals are better for small notifications than bulk payloads.

🎯 Pro Tip
Do not use a process merely because it sounds safer or a thread merely because it sounds faster. Include isolation needs, data sharing, failure containment, and measured overhead in the decision.
bash
# Observe a process tree and voluntary / involuntary context switches
ps -eLf | head -n 20
/usr/bin/time -v sh -c 'printf "demo
"' 2>&1 | grep -i context

Timing output and field names vary by platform; tiny commands are dominated by noise. Measure a representative workload and avoid concluding that a context switch alone caused a performance problem.

// Part 06 — Python and Linux

A Small Process Observer

Python's standard library exposes process creation and waiting on POSIX systems. The following parent starts a child command safely as an argument list. A production application should still set timeouts, validate inputs, and avoid constructing shell command strings from untrusted data.

run_child.py
python
import subprocess

result = subprocess.run(
    ["python3", "-c", "print('child says hello')"],
    check=True,
    capture_output=True,
    text=True,
    timeout=10,
)
print("child output:", result.stdout.strip())
print("return code:", result.returncode)

For a hands-on trace, run strace -f around a short program to follow child processes when supported. Compare what the parent requests with what appears in the process list. Do not treat a momentary listing as a complete history—short-lived tasks can exit between samples.

  1. Sketch a parent and child, marking which process image changes after exec.
  2. Mark the state of a process waiting for a child and the event that wakes it.
  3. Identify one shared resource between threads and write down the protection it needs.
  4. Compare process isolation, IPC, and context-switch costs for a CPU-heavy and an I/O-heavy workload.
// Part 07 — Pitfalls

Answer with Precision

Giving a memorized slogan without assumptions

Why: A scheduling or replacement answer changes with the workload and model.

Fix: State arrivals, frame count, preemption, and tie-breaking before calculating.

Confusing two neighboring concepts

Why: Similar terms describe different layers or guarantees.

Fix: Define each concept and provide one contrast before adding detail.

Treating an observation as a universal guarantee

Why: A command samples one system at one moment.

Fix: Describe the observation limits and what additional evidence is needed.

Ignoring trade-offs and failure modes

Why: An idealized definition omits the engineering constraints.

Fix: Name one cost, edge case, or recovery path for every mechanism.

// Part 08 — Practice

Apply It to a Small System

Practice

Trace, calculate, and explain

  1. Draw the state, resource, or address transitions for a realistic workload, including a wait or failure path.
  2. Run a relevant Linux observation command from this lesson and explain what it can and cannot prove.
  3. Implement the small C or Python exercise, change one input, and predict the result before executing it.
  4. Explain one design choice with a precise invariant, a concrete example, and a trade-off.

Hint: Write assumptions first, then use a trace or calculation to support your explanation.

🎯 Key Takeaways

  • ✓A process is an executing instance with an address space and OS-managed resources; a program is passive code.
  • ✓The PCB abstraction captures state and metadata needed to manage and resume execution.
  • ✓fork creates a child and exec replaces its program image; waiting collects child status.
  • ✓Threads share selected process resources but retain separate stacks and execution contexts.
  • ✓Context switches enable multiplexing but add overhead; IPC choice changes isolation and coordination costs.

Next

What comes next

How processes talk: pipes, signals, message queues, shared memory and sockets, with working examples and their trade-offs.

Lesson 03 · Inter-Process Communication — Pipes, Signals and Shared Memory →

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...