Process Concept

A deep dive into process states, Process Control Block (PCB) architecture, and the mechanics of process creation in modern operating systems.

published: reading time: 39 min read author: Geek Workbench updated: November 24, 2026
Quick Summary

A deep dive into process states, Process Control Block (PCB) architecture, and the mechanics of process creation in modern operating systems.

Process Concept

A program is a stored executable and data; a process is an executing instance with state and resources managed by an operating system. Operating systems track runnable and waiting work, but their internal structures, visible states, and process-creation APIs differ.

This article uses the five-state diagram and the Process Control Block (PCB) as textbook models. Linux and POSIX details are labeled separately from Windows process creation, so the model is not mistaken for one system’s exact implementation.

The later sections about fork(), wait(), signals, /proc, and daemons describe POSIX or Linux behavior. They are not Windows process APIs.

When to Use

  • Debugging performance issues — When CPU usage is unexpectedly high or low, understanding process states helps identify bottlenecks.
  • Designing concurrent systems — Knowing how processes are scheduled allows you to write more efficient parallel code.
  • System programming — Creating daemons, forking workers, or managing subprocess hierarchies requires solid process concepts.
  • Capacity planning — Understanding how many processes your system can handle informs infrastructure decisions.

When Not to Use

  • Writing simple scripts — For basic automation, you rarely need to think about process internals.
  • High-level application development — Modern runtimes (JVM, Node.js, .NET) abstract away process management for most use cases.
  • Database query optimization — Process concepts won’t help you tune your SQL indexes.

Process States: Textbook Model

Operating-systems textbooks often use five broad states to explain scheduling. This is a teaching model, not a state list shared by every operating system:

graph TD
    A[New] --> B[Ready]
    B --> C[Running]
    C --> D[Waiting]
    D --> B
    C --> E[Terminated]
    B --> E

New: The process is being created. An OS may perform this work without exposing a separate “new” state to applications.

Ready: The process is runnable and waiting for CPU time. Kernels may combine this with a running state in their user-visible status.

Running: A thread is executing on a CPU. On a multicore system, several threads from the same process can run at once.

Waiting (Blocked): A thread cannot make progress until an event occurs, such as I/O completion or a lock becoming available. Real kernels often split waiting into several states.

Terminated: Execution has ended. POSIX systems may retain a child’s exit status until the parent collects it; other operating systems expose termination through their own process and thread APIs.

Linux states are different

Linux tools report kernel task states rather than the textbook five labels. For example, ps can show R for running or runnable, S for interruptible sleep, D for uninterruptible sleep, T for stopped, and Z for zombie. The exact set and meaning are Linux-specific; see the ps state codes and /proc/<pid>/status documentation.

Process Control Block (PCB)

The PCB is a textbook abstraction for the bookkeeping an OS needs to manage execution. It is not a portable structure with a fixed set of fields. A model PCB may include:

  • Process ID (PID): Unique identifier
  • Parent PID (PPID): Who created this process
  • State: Current execution state
  • Program Counter: Next instruction to execute
  • CPU Registers: Current register values
  • Stack Pointer: Current top of stack
  • Memory Management Info: Page tables, segments
  • I/O Status: References to open files or pending operations

Linux keeps per-task bookkeeping in internal structures such as task_struct. A process can have several tasks (threads), so the textbook “one PCB per process” is only a model. /proc/<pid>/ exposes selected fields through a user-space interface, not the full kernel structures or all saved CPU state. Tools such as ps format a subset of available process information.

Linux process inspection exercise

Start a short-lived child and compare the view from ps with the files Linux exposes for its PID:

sleep 60 &
pid=$!

ps -o pid,ppid,stat,comm -p "$pid"
grep -E '^(Name|State|Tgid|Pid|PPid|Threads|VmRSS):' "/proc/$pid/status"
printf 'Thread IDs: '
printf '%s ' /proc/"$pid"/task/*
printf '\n'

kill "$pid"
wait "$pid" 2>/dev/null || true

ps and /proc provide snapshots of selected fields; state can change between reads. /proc/<pid>/task/ lists the process’s threads. These interfaces help inspect processes, but they are not a dump of the kernel’s internal PCB. See the Linux /proc/<pid>/status and /proc/<pid>/task/ references.

Process Creation by Operating System

The creation API depends on the OS. On POSIX systems, fork() creates a child process and exec() can replace that child’s process image. On Windows, CreateProcessW creates a new process and its primary thread directly.

System Creation behavior
POSIX/Linux fork() creates a child; the exec family replaces the calling process image after a successful call.
Windows CreateProcessW creates a process and its primary thread, and returns process and thread identifiers and handles. There is no required fork()/exec() pair.

POSIX/Linux: fork and exec

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

int main() {
    pid_t pid = fork();

    if (pid < 0) {
        perror("fork failed");
        return 1;
    }

    if (pid == 0) {
        // Child process
        printf("Child: PID = %d, Parent PID = %d\n", getpid(), getppid());

        // Replace with new program
        char *args[] = { "ls", "-la", NULL };
        execvp("ls", args);

        // If execvp fails
        perror("exec failed");
        return 1;
    } else {
        // Parent process
        printf("Parent: PID = %d, Child PID = %d\n", getpid(), pid);

        int status;
        waitpid(pid, &status, 0);  // Wait for child
        printf("Child exited with status: %d\n", WEXITSTATUS(status));
    }

    return 0;
}

fork() creates a child process. Both processes continue after the call: the parent receives the child’s PID, and the child receives 0. A successful execvp() replaces the child’s process image and does not create another process; it returns only if execution fails.

Windows: CreateProcess

Windows applications commonly use CreateProcessW (or a related API) to create a new process and its initial thread. The API returns handles and identifiers for both objects. This is a different interface from POSIX’s separate fork() and exec() calls.

Textbook Process-Management Diagram

graph TB
    subgraph Kernel_Space
        PCB1[PCB: PID 1001]
        PCB2[PCB: PID 1002]
        PCB3[PCB: PID 1003]
        Scheduler[Scheduler]
    end

    subgraph User_Space
        Process1[Process 1001]
        Process2[Process 1002]
        Process3[Process 1003]
    end

    Process1 --> PCB1
    Process2 --> PCB2
    Process3 --> PCB3

    Scheduler -->|schedules| PCB1
    Scheduler -->|schedules| PCB2
    Scheduler -->|schedules| PCB3

    ReadyQ[Ready Queue] --> Scheduler
    WaitQ[Waiting Queue] --> Scheduler

This diagram illustrates the textbook model. Real schedulers use OS-specific internal structures, and a ready queue is not a required implementation detail. The next article in the sequence covers how schedulers choose runnable work.


Core Concepts

Process vs Thread

The distinction between processes and threads comes down to memory independence versus memory sharing. Each process gets its own virtual address space — when Process A writes to address 0x1000, Process B’s 0x1000 holds completely different data. Threads within a process sidestep this isolation: all threads in a process share the same address space, meaning Thread 1 can read a variable that Thread 2 modified milliseconds ago.

This memory model shapes everything about performance and safety. Spawning a process means the kernel must duplicate page tables, allocate a new stack and heap, and set up separate memory mappings. Spawning a thread only needs a new stack and thread-local storage — the heap, code, and static data are shared. The result: thread creation costs roughly 10-50x less than process creation on typical Linux systems.

The tradeoff is fault isolation. A segfault in a process crashes only that process; a segfault in a thread kills the entire process, because all threads share the same address space and signal handlers. For workloads that need to run untrusted code, processes remain the safer choice even though they cost more to spin up.

Operation Process Thread
fork() time (empty process) ~100-200 microseconds N/A
pthread_create() time N/A ~5-10 microseconds
Memory footprint (empty) ~8MB (page tables) ~8KB (stack)
IPC mechanism pipes, sockets, queues shared memory, mutexes

When you fork a worker in a web server, you’re usually better off with threads if the worker just handles requests in isolation. When you’re spawning a sandboxed execution environment, processes give you the isolation you need at the cost of higher overhead.

POSIX/Linux parent-child hierarchy

On POSIX/Linux, processes have parent relationships. getpid() returns the caller’s PID and getppid() returns its parent’s. If a parent exits first, an orphan is reparented according to the system’s subreaper and init rules. PID 1 is common, but it is not always the adopter in containers or systems with a configured subreaper.

Inspect the tree with pstree or ps -ejH. The PPID column in ps -ef shows the lineage directly. Process group IDs (PGID) are related but separate: a process group lets you send a signal to an entire pipeline at once, like when you press Ctrl+C to interrupt grep foo file | sort. The group leader’s PID equals the PGID, and every member of the group receives the signal simultaneously.

POSIX/Linux zombie and orphan processes

A zombie is a process that has terminated but whose parent hasn’t yet called wait() to retrieve the exit status. Zombies remain in the process table until the parent reads their status.

An orphan is a process whose parent has died. The init process adopts orphans and eventually reaps them via wait.

On POSIX/Linux, the kernel keeps a small process-table entry with a child’s exit status until the parent collects it with wait(). This zombie entry is not a full PCB snapshot. If the parent exits, the child is reparented; a new parent or subreaper must reap it.

In production, you detect zombies with ps aux | grep ' Z '. A few zombies transiently are normal during heavy fork/wait activity. A growing zombie count is a bug — the parent is not reaping children. The usual suspects: request handlers that skip waitpid() in error paths, worker processes that crash before calling wait(), or applications that fork and exec without proper cleanup. In containers where PID 1 is your application (not systemd or init), you must handle SIGCHLD yourself or explicitly adopt children with prctl(PR_SET_CHILD_SUBREAPER).

Daemon processes on Unix-like systems

A daemon is a background process that runs without a controlling terminal. Created by forking, then calling setsid() to detach from the terminal, then often changing the working directory to / and closing stdin/stdout/stderr.

The fork-then-setsid sequence is deliberate. The first fork makes the child a descendant of the calling process, which matters if the caller is already a session leader. The child then calls setsid(), which creates a new session and makes the child the session leader, but only if it is not already a session leader. A session leader can acquire a terminal by opening one, so the second fork prevents this. Only the child of the second fork becomes session leader, which means no subsequent setsid() call can attach a terminal. The order matters, not just the presence of the calls.

After detaching from the terminal, daemons change their working directory to / using chdir(). This prevents the daemon from holding a mount point open. If the current directory lives on a filesystem that gets unmounted, the kernel keeps that mount alive as long as the daemon holds it. Moving to / solves this.

File descriptor handling follows the same logic. Closing stdin, stdout, and stderr cuts the daemon’s ties to the old terminal and prevents accidental writes from appearing to users. Redirecting them to /dev/null or a log file keeps library code that writes to stdout or stderr from failing. The descriptor numbers matter here: fd 0, 1, and 2 are what you are actually closing. If a daemon opens a new file without O_CLOEXEC before those descriptors are redirected, the kernel can fill them with real file descriptors instead, pushing stdout somewhere unexpected.

The Python daemon in the Implementation Snippets section follows the double-fork approach. The first fork lets the parent exit so the shell that launched the daemon sees it complete immediately. The second fork prevents the daemon from accidentally acquiring a new controlling terminal later. The double-fork pattern is also embedded in systemd’s service abstraction: when you define an ExecStart directive, systemd calls fork() internally and handles the session management. For daemons that must run without systemd, the double-fork pattern remains the right approach.


Production Failure Scenarios

Fork Bombs

Problem: A process rapidly forks children that also fork, exhausting process table slots and PID resources.

Symptoms: “fork: Cannot allocate memory” errors, system unresponsiveness, inability to create new processes even for essential services.

Mitigation:

  • Set appropriate ulimit -u (max processes per user)
  • Use systemd’s DefaultLimitNPROC in /etc/security/limits.conf
  • Implement exponential backoff in application code that forks
# Check current process limits
ulimit -a
# See process count per user
ps aux | awk '{print $1}' | sort | uniq -c | sort -rn | head

Zombie Accumulation

Problem: Application fails to call wait() on terminated children, causing zombie processes to accumulate.

Symptoms: ps shows processes with ‘Z’ state, process table fills up, new processes cannot be created.

Mitigation:

  • Always call wait()/waitpid() in parent
  • Use signal handler for SIGCHLD that reaps children
  • For long-running servers, implement a child reaper thread
#include <signal.h>

void sigchld_handler(int sig) {
    int saved_errno = errno;  // Save errno
    while (waitpid(-1, NULL, WNOHANG) > 0);
    errno = saved_errno;
}

struct sigaction sa;
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
sigaction(SIGCHLD, &sa, NULL);

Resource Leakage in Child Processes

Problem: Child processes inherit open file descriptors but never close them, leading to resource exhaustion.

Symptoms: “Too many open files” errors, file descriptor leaks visible in /proc/<pid>/fd/.

Mitigation:

  • Set O_CLOEXEC flag when opening files
  • Close unnecessary FDs before exec()
  • Use FD_CLOEXEC with fcntl() on inherited FDs

Trade-off Table

Aspect Process Thread
Creation Speed Slower (memory duplication) Faster (shared address space)
Memory Overhead Higher (separate page tables) Lower (shares parent’s memory)
Communication Complex (IPC required) Simple (shared memory)
Fault Isolation Strong (separate address space) Weak (crash can affect others)
Synchronization Simpler (no shared state) Complex (must synchronize shared data)
Scenario Best Choice Reason
Isolated execution Process Crash doesn’t affect parent
High-frequency spawning Thread Lower overhead
CPU-bound parallel work Thread Shares memory, low latency
I/O-bound concurrent tasks Either Depends on isolation needs
Scheduler Design Advantages Disadvantages
Short-term (CPU) Minimizes turnaround time May starve processes
Long-term (Job) Controls degree of multiprogramming Slower response to load changes
Medium-term Balances memory and CPU usage Adds complexity

Implementation Snippets

Creating a Daemon

#!/usr/bin/env python3
import os
import sys

def become_daemon():
    """Fork and detach from controlling terminal."""
    # First fork
    if os.fork() > 0:
        sys.exit(0)  # Parent exits

    # Detach from controlling terminal
    os.setsid()

    # Second fork (prevents acquiring new controlling terminal)
    if os.fork() > 0:
        sys.exit(0)

    # Change to root directory (prevents holding mount point)
    os.chdir("/")

    # Close stdin, stdout, stderr
    sys.stdout.flush()
    sys.stderr.flush()
    with open(os.devnull, 'r') as devnull:
        os.dup2(devnull.fileno(), sys.stdin.fileno())
    with open(os.devnull, 'a+') as devnull:
        os.dup2(devnull.fileno(), sys.stdout.fileno())
        os.dup2(devnull.fileno(), sys.stderr.fileno())

    # Write PID file
    with open('/var/run/mydaemon.pid', 'w') as f:
        f.write(str(os.getpid()))

become_daemon()
# Now running as daemon
import time
while True:
    time.sleep(60)

Checking Process State on Linux

#!/bin/bash
# Monitor process state transitions

PID=$1
if [ -z "$PID" ]; then
    echo "Usage: $0 <pid>"
    exit 1
fi

echo "Monitoring process $PID..."
echo "Press Ctrl+C to stop"

while true; do
    if [ -d "/proc/$PID" ]; then
        STATE=$(cat /proc/$PID/status | grep "^State:" | awk '{print $2}')
        CMD=$(cat /proc/$PID/cmdline 2>/dev/null | tr '\0' ' ' | cut -c1-60)
        THREADS=$(cat /proc/$PID/status | grep "^Threads:" | awk '{print $2}')
        echo "$(date '+%H:%M:%S') State=$STATE Threads=$THREADS CMD=$CMD"
    else
        echo "Process $PID no longer exists"
        break
    fi
    sleep 1
done

Process Resource Monitoring in C

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

void print_rlimits(const char *name) {
    struct rlimit limit;
    printf("%s limits for PID %d:\n", name, getpid());

    if (getrlimit(RLIMIT_CPU, &limit) == 0) {
        printf("  CPU time: %lu (soft) / %lu (hard)\n",
               (unsigned long)limit.rlim_cur,
               (unsigned long)limit.rlim_max);
    }
    if (getrlimit(RLIMIT_NPROC, &limit) == 0) {
        printf("  Max processes: %lu (soft) / %lu (hard)\n",
               (unsigned long)limit.rlim_cur,
               (unsigned long)limit.rlim_max);
    }
    if (getrlimit(RLIMIT_NOFILE, &limit) == 0) {
        printf("  Open files: %lu (soft) / %lu (hard)\n",
               (unsigned long)limit.rlim_cur,
               (unsigned long)limit.rlim_max);
    }
}

int main() {
    print_rlimits("Current process");
    return 0;
}

Observability Checklist

Metrics to Monitor

  • Process count per user: ps aux | awk '{print $1}' | sort | uniq -c | sort -rn | head
  • Zombie count: ps aux | grep ' Z ' | wc -l
  • Runnable processes: vmstat 1 | tail -1 (look at ‘r’ column)
  • Process state distribution: ps -eo state,pid,cmd | sort | uniq -c | sort -rn

Logs to Watch

  • /var/log/syslog or /var/log/messages — system process creation/termination
  • dmesg | grep -i "process" — kernel messages about process events
  • journalctl -u <service> — for systemd-managed services

Alerts to Configure

  • Zombie process count > 0 for more than 5 minutes
  • Total process count approaching kernel.pid_max (default 32768 on Linux)
  • Process count per user exceeding 80% of their ulimit
  • High rate of fork() failures in application logs

Trace Commands

# Trace process creation
sudo strace -f -e trace=fork,vfork,clone,execve

# Trace signal delivery
sudo strace -p <pid> -e trace=signal

# Monitor process state changes with perf
sudo perf sched record -a -g sleep 10
sudo perf sched latency

Common Pitfalls / Anti-Patterns

Ignoring SIGCHLD

Not handling SIGCHLD causes zombies. Always either:

By default, when a child terminates, the kernel sends SIGCHLD to the parent. The default disposition is SIG_IGN on some init systems, but historically it was SIG_DFL, which does nothing — the kernel just keeps the zombie around. This design predates careful thinking about signal semantics: SIGCHLD with SIG_DFL does not actually cause the kernel to auto-reap the child.

The three approaches that actually work:

Reap in a dedicated thread. The parent blocks on waitpid(-1, NULL, 0) in a separate thread. This is the cleanest approach for servers — one thread handles all child reaping without polluting the main thread’s signal context.

Signal handler with SA_NOCLDSTOP. Loop on waitpid(-1, NULL, WNOHANG) inside the handler. SA_NOCLDSTOP keeps the handler from firing when a child stops due to SIGSTOP, which would cause unnecessary reaping attempts. Use this when you need to know about child exits from a signal context.

Set SA_NOCLDWAIT. This Linux-specific flag tells the kernel to discard child exit information automatically, so no zombies form. The tradeoff: you cannot call wait() to retrieve exit status. This works for daemons that fork workers purely for isolation and do not care about exit codes.

The common mistake is assuming children are automatically reaped if the parent does not register a handler. They are not. The kernel discards the exit information but leaves the zombie entry until the parent explicitly calls wait(). If you do not care about exit status, loop on waitpid(pid, NULL, WNOHANG) or set prctl(PR_SET_CHILD_SUBREAPER) in a supervisor process.

Fork without exec

The fork-exec pattern exists for a reason: fork() duplicates the parent’s address space, and exec() replaces it with a new program. If you fork and never exec, you’re paying the cost of memory duplication for a child that runs the same code as the parent — which is often pointless.

The classic valid use case for fork-without-exec is spawning a worker that continues running the parent’s code, like a web server that forks children to handle requests. But if you fork purely to launch a different program, skipping exec() means the child drags along the parent’s entire memory footprint for no reason.

The alternatives depend on what you’re actually trying to achieve:

  • vfork() was designed for the pre-copy-on-write era where fork() was genuinely expensive. It shares the parent’s address space with the child until exec() or exit(), blocking the parent in the meantime. On modern Linux, vfork() is just fork() with COW disabled; it does not save you anything meaningful and is deprecated.
  • clone() with CLONE_VM creates a child that shares the parent’s memory space (like a thread) but runs as a separate process with its own PID. Useful for implementing custom process models.
  • posix_spawn() is the portable way to fork-plus-replace atomically. It handles the details correctly and is what the standard library calls under the hood on macOS and other systems where fork() alone is problematic.

If you’re forking to run a different program, just use exec() in the child. If you’re forking to run the same program in a worker model, consider whether threads would be simpler. If you genuinely need separate process state without exec(), clone() with explicit flags gives you fine-grained control over what gets shared.

Not checking fork() return value

Fork failures are rare in casual development but common enough in production that ignoring the return value will eventually bite you. The kernel returns -1 when it can’t create a new process, and this happens most often when the process table is full, when the system’s memory or address space is exhausted, or when the calling process has hit its RLIMIT_NPROC ceiling.

When fork() returns -1, errno is set to indicate the reason. EAGAIN means the system is low on memory or swap space — try reducing the workload or waiting before retrying. ENOMEM means the kernel can’t allocate the internal structures needed for a new process, which on Linux typically means either memory exhaustion or hitting the pid_max limit. ENOSPC means you’ve hit the maximum number of processes allowed system-wide.

The correct pattern is to check the return value immediately and handle failure before doing anything else in the child branch:

pid_t pid = fork();
if (pid < 0) {
    // Log and handle — do not continue
    syslog(LOG_ERR, "fork failed: %m");
    // Decide: abort, retry with backoff, or degrade gracefully
    return; // or exit(1), or longjmp to safety
}
if (pid == 0) {
    // Child: can proceed knowing fork() succeeded
}

Failing to check means the child continues executing as if it were successfully forked, which it was not. Any code that assumes the child exists and proceeds to, say, call exec() or communicate with the parent via a pipe, will behave unpredictably. The parent meanwhile continues as if it has a child, leading to confusing state when waitpid() returns -1.

Zombie accumulation in production

Zombies accumulate when the parent process never calls wait() or waitpid() to retrieve a child’s exit status. In development, this rarely matters — your process runs, exits, and the parent reaps it immediately. In production servers that fork many workers over time, a single missed wait() call per fork creates one zombie, and zombies stick around until the parent exits or gets killed.

The production failure mode looks like this: a long-running web server forks a child to handle a request. The child crashes or times out, but the request handler code has a bug that skips the waitpid() call in the error path. Each failed request leaves one zombie. After a few hundred failed requests, the process table starts filling up. New processes can’t be created, essential services can’t spawn workers, and the system grinds to a halt.

The standard fix is a SIGCHLD handler that reaps all terminated children non-blocking:

void sigchld_handler(int sig) {
    int saved_errno = errno;
    while (waitpid(-1, NULL, WNOHANG) > 0);
    errno = saved_errno;
}

Setting SA_NOCLDSTOP is important — it prevents SIGCHLD from firing when a child stops (due to SIGSTOP, etc.), which would cause unnecessary reaping attempts. For servers that need to know the child’s exit status rather than just reaping it, use waitpid() with the WEXITED flag in a dedicated reaper thread instead of a signal handler.

If your application uses a worker pool model where children outlive individual requests, implement a communication channel (pipe, socketpair) that the child writes its exit status through before terminating. The parent reads this channel and calls waitpid() with the exact PID, getting both reaping and status reporting.

Unintended forking loops

The most dangerous fork bugs aren’t the ones where you forget to check the return value — they’re the ones where fork() ends up inside a loop you didn’t design to handle it. A forking loop that works correctly during development can behave very differently under load, with signals, or with concurrent connections.

Consider a request handler that forks a child per connection. Under normal load, each request forks, the child handles it, and exits. Under heavy load with many short-lived connections, the fork-rate becomes high enough that the system can’t keep up. If the handler is also inside a retry loop, say a database connection that reconnects on failure, and the retry logic calls the connection handler which forks, you now have a loop inside a loop, each with their own fork(). This is the classic pattern that produces fork bombs.

The safeguard is to move process creation outside of retry loops, and to make the number of child processes a controlled resource. A simple semaphore or token bucket that limits concurrent children prevents the exponential growth case:

static atomic_int active_children = 0;
static const int MAX_CHILDREN = 50;

if (active_children >= MAX_CHILDREN) {
    // Wait or decline new requests — don't fork
    return;
}
pid_t pid = fork();
if (pid < 0) {
    // System is out of resources — back off and retry later
    sleep(1);
    return;
}
if (pid == 0) {
    // child: handle request, then exit
    active_children.fetch_sub(1);
    _exit(0);
} else {
    // parent: increment, waitpid in separate thread
    active_children.fetch_add(1);
}

When designing any code that forks, draw the state machine before you write the loop. Identify every path into the fork() call and every path out of it. If you can’t account for all paths, the fork belongs in a function with a clear single entry and exit, not scattered across conditional branches.

Resource Limits

Configure /etc/security/limits.conf to prevent fork bombs and resource exhaustion:

* soft nproc 1024
* hard nproc 4096
* soft nofile 1024
* hard nofile 4096

Privilege Separation

Run services with minimal privileges. Use setuid and setgid binaries cautiously — these are common privilege escalation vectors.

Privilege separation limits the blast radius of a compromise. If a network service runs as root and an attacker exploits a buffer overflow, the attacker gets root immediately. If that same service runs as a dedicated unprivileged user, the attacker gets only that user’s permissions. The principle is straightforward, but production deployments routinely stumble on the implementation.

The setuid and setgid bits are where most privilege escalation mistakes happen. A binary with the setuid bit runs with the permissions of its file owner, not the user who executed it. A vulnerable setuid root binary is a direct path to root. Setgid binaries carry similar risks, though the damage is usually limited to group-level compromise.

The audit command finds all setuid binaries on the system:

# Check for setuid binaries (audit regularly)
find / -perm -4000 -type f 2>/dev/null | head -20

Compare output against a known-good baseline to catch new additions. An attacker who already has a foothold often plants a setuid binary to maintain access. Pay attention to binaries in /tmp, /var/tmp, home directories, and any recently installed packages.

Modern Linux offers safer options: systemd’s User= directive, CAP_NET_BIND_SERVICE for binding privileged ports as non-root, and explicit privilege-dropping via setuid(getuid()) after initial setup. These give you exactly the privileges you need without the attack surface of a setuid binary.


Process Isolation

Process isolation is the kernel’s guarantee that Process A cannot read or modify Process B’s memory, open files, or kernel state without explicit sharing mechanisms. This guarantee is enforced through a combination of hardware features and OS-level policy.

The hardware layer uses memory management unit (MMU) features — specifically the page table structure — to enforce separate address spaces. Each process has its own page table, and the MMU walks this table on every memory access. When a process tries to access an address outside its mapped range, the hardware triggers a page fault, and the kernel kills the process with SIGSEGV. On x86-64, the page tables are hierarchical (PML4 → PDPT → PD → PT), and each process gets its own PML4 root.

Address Space Layout Randomization (ASLR) randomizes where the kernel places the stack, heap, shared libraries, and mmap regions in a process’s address space. ASLR breaks exploit techniques that rely on knowing absolute memory addresses — without it, an attacker who controls one memory location can reliably jump to a known function. Linux enables ASLR by default for user-space processes; you can verify with cat /proc/sys/kernel/randomize_va_space (0=off, 2=full).

Hardware virtualization extensions (Intel VT-x, AMD-V) take isolation further by letting the kernel run guest address spaces in a virtual machine monitor. A hypervisor can map guest physical memory to arbitrary host physical memory, and enforce that guest processes can’t access memory belonging to other guests. This is the foundation of containers and VMs alike.

For most applications, the OS-level isolation between processes is sufficient — you don’t need hardware virtualization to get memory protection. But when running untrusted or semi-trusted workloads (like a plugin sandbox, a multi-tenant web server, or a container), combining process isolation with seccomp filters, capability dropping, and namespace isolation gives you defense in depth.

Capability Model

Before capabilities, Unix distinguished only two states: root (UID 0, all privileges) and non-root (everything denied). This coarse model meant a program that needed to bind to a privileged port (< 1024) or read a system file had to run as root entirely, gaining all root privileges even though it only needed one. The capability model splits these root privileges into around 40 discrete units, so a process can hold exactly the privileges it needs and nothing more.

Each capability is a per-process attribute stored in the kernel. The key ones you’ll encounter:

  • CAP_NET_BIND_SERVICE — bind to a port below 1024. Without this, bind() to port 80 or 443 fails with EACCES. Web servers that want to drop root privilege after binding their port use this.
  • CAP_DAC_OVERRIDE — bypass file permission checks. Without this, a process respects normal UNIX permission bits.
  • CAP_SYS_ADMIN — a broad capability covering many administrative operations: mounting filesystems, changing hostname, setting domain name, many sysctl writes. This is the closest thing to “partial root.”
  • CAP_NET_RAW — raw socket access, needed for tools like ping and packet sniffers.
  • CAP_SYS_PTRACE — attach to other processes via ptrace(), read their memory, intercept syscalls.

At exec() time, the kernel checks capabilities and marks the permitted and effective capability sets in the process’s credentials. A daemon that needs CAP_NET_BIND_SERVICE drops everything else:

struct cred *new = prepare_creds();
cap_from_text("cap_net_bind_service=ep", &new->cap_permitted);
commit_creds(new);

You can inspect a running process’s capabilities with getpcaps <pid> or cat /proc/<pid>/status | grep Cap. The three sets to understand are: Permitted (capabilities the process may use), Effective (capabilities currently active), and Inheritable (capabilities preserved across exec()).

Audit Requirements

Process accounting records when processes are created and destroyed. Compliance frameworks like PCI-DSS and SOC2 require an audit trail of process lifecycle events. The acct package on Debian/Ubuntu (or psacct on RHEL-based systems) enables this via the accton daemon, which writes to /var/account/pacct — a binary file you query with lastcomm, sa, and ac commands.

What you log depends on your compliance posture. Track process creation (fork), program execution (exec), process termination (exit), and privilege changes (setuid/setgid binaries). PCI-DSS specifically requires knowing which user ran which binary and when. The acct records include the command name, user ID, terminal, elapsed time, and CPU usage — enough to reconstruct what happened during an incident.

# Enable process accounting (Debian/Ubuntu)
sudo apt install acct
sudo systemctl enable acct

# Query who ran a specific command
lastcomm --user www-data --command apache2

# Summarize process accounting statistics
sa -m

# Show login accounting (who logged in/out)
last

If acct isn’t enough, look at the Linux Audit Daemon (auditd). It tracks syscalls directly: execve, fork, exit, and setuid are all configurable events in /etc/audit/rules.d/. auditd logs to the kernel audit buffer and can capture command-line arguments and return codes. acct gives you post-hoc statistics; auditd gives you forensic detail.

For CLOUD-AUDIT or FedRAMP environments, forward these logs to a centralized SIEM. Each event needs timestamp, PID, PPID, UID, gid, command executed, and session ID. rsyslog or a dedicated log forwarder handles this.


Quick Recap Checklist

  • A process is a program in execution with its own address space and state
  • The PCB is a textbook model; each OS stores and exposes process information differently
  • The five textbook states explain scheduling; Linux reports its own task-state codes
  • In POSIX, fork() creates a child and exec() replaces the current process image
  • Linux commonly uses copy-on-write for fork(); this is an implementation detail
  • On POSIX/Linux, a zombie remains until its exit status is collected; orphan adoption follows OS rules
  • Daemons are background processes detached from controlling terminals
  • ulimits prevent resource exhaustion; monitor process counts in production
  • Always handle SIGCHLD to prevent zombie accumulation

Interview Questions

1. What is the difference between a process and a program?

A program is a passive entity — a file containing executable code and data stored on disk. A process is an active entity — a program that has been loaded into memory and is currently executing with its own execution context, including registers, stack, heap, and program counter.

In technical terms, a process is the unit of execution in an operating system, while a program is the static description of that execution.

2. Explain the different process states and transitions between them.

The five labels below are a textbook model, not a portable list of OS state names:

  • New: Creation is in progress.
  • Ready: Runnable, but waiting for CPU time.
  • Running: A thread is executing on a CPU.
  • Waiting: A thread is blocked until an event occurs.
  • Terminated: Execution has ended; POSIX parents may still need to collect a child's exit status.

Linux reports states such as R (running or runnable), S (interruptible sleep), D (uninterruptible sleep), T (stopped), and Z (zombie). Use the Linux state codes for Linux observations; do not map them one-to-one onto the five textbook labels.

3. What is a zombie process and how does it form?

A zombie is a process that has terminated but whose entry still exists in the process table because the parent hasn't read its exit status via wait() or waitpid().

On POSIX/Linux, the kernel retains a small process-table entry with the exit status until the parent collects it. This zombie entry is not a full PCB snapshot. The process is already terminated, so sending it a signal cannot make it run or reap itself.

If its parent exits first, the child is reparented according to the system's init and subreaper rules; the new parent can collect the status.

4. How does copy-on-write (COW) optimize the fork() system call?

Copy-on-write is an implementation technique, not a POSIX requirement. Linux commonly lets the parent and child initially share physical pages, then copies a page when one process writes to it. The kernel still creates process bookkeeping and page-table state, so fork() is not cost-free or guaranteed to be instantaneous.

This is useful when a child soon calls exec(), because it can avoid copying most of the parent's data pages before replacing the process image.

5. What is the purpose of the Process Control Block (PCB) and what information does it contain?

PCB is a textbook name for the OS bookkeeping associated with execution. The internal data structures vary by OS, and user-space tools show only selected fields. A conceptual PCB may include:

  • Identification: PID, parent PID, user ID, group ID
  • State: Current process state (running, waiting, etc.)
  • Scheduling: Priority, cumulative CPU time, scheduling policy
  • Memory: Pointer to memory descriptor (page tables, segments)
  • CPU context: Register values, program counter, stack pointer
  • I/O status: Open files, file descriptor table, current directory
  • Accounting: Start time, total CPU time used
  • Signals: Pending signals, signal handler table

On Linux, /proc and ps expose a partial view, not the full kernel task structure or all saved CPU state.

6. What is the difference between an orphan process and a zombie process?

In POSIX/Linux, an orphan is still running after its parent exits; the system reparents it, often to PID 1 or a configured subreaper. A zombie has already exited, but its parent has not collected the exit status.

The new parent can reap an orphan when it later exits. A zombie's original parent or its reaper must collect the stored status; the zombie itself cannot run or be made to exit again.

7. How does a daemon process differ from a regular background process?

A daemon is a background process that has detached from its controlling terminal — it has no terminal associated with it, which means it won't receive terminal-generated signals like SIGINT (Ctrl+C) or SIGHUP (terminal hangup).

Creation steps: fork() → setsid() (detach from terminal) → optionally fork again (prevent acquiring a new terminal) → chdir("/") (prevent holding a mount point) → close stdin/stdout/stderr → write PID file.

Regular background processes started with & are still attached to the terminal and will receive SIGHUP when the terminal closes. Daemons are immune to terminal closure.

8. What happens during a context switch at the hardware level?

In a simplified model, the kernel saves enough execution context to suspend one thread and resume another. The exact steps depend on the architecture and kernel:

  • Timer interrupt fires, transferring control to kernel mode
  • Kernel saves current process's registers, program counter, and stack pointer onto the kernel stack
  • Kernel switches to the process's kernel stack (for that process's kernel-mode execution)
  • The scheduler selects another runnable thread and restores its saved context from kernel-managed state
  • Stack pointer is switched to the new process's kernel stack
  • Control returns to user mode with the new process's instruction pointer

The PCB diagram is a teaching aid; register storage and context-switch details are implementation-specific.

9. What is the role of the init process (PID 1) in Unix systems?

On Linux, PID 1 is the first user-space process in a PID namespace. Orphaned processes may be reparented to PID 1 or to a configured child subreaper; PID 1 is not necessarily the immediate adopter in every case.

Key responsibilities:

  • Zombie reaping: an adopting parent must collect exit statuses from its children
  • System initialization: runs startup scripts (/etc/rc.d/, systemd units) to bring up services
  • Adoption: system-specific reparenting keeps orphaned processes under a parent

Systemd and other init systems commonly serve as PID 1 on Linux, but the exact setup varies by distribution and container.

10. What is the relationship between PID, TGID, and PGID?

PID (Process ID) is the unique identifier for a process. TGID (Thread Group ID) is the PID of the thread that started a thread group — all threads in a multi-threaded process share the same TGID. PGID (Process Group ID) groups related processes for signal delivery (e.g., a pipeline of processes).

For a single-threaded process, PID == TGID. For the main thread of a multi-threaded process, PID == TGID. Additional threads get unique PIDs but share the TGID.

11. What is the purpose of the nice value and how does it affect scheduling?

The nice value (-20 to +19, default 0) adjusts a process's scheduling priority on Unix systems. Higher nice values mean lower priority — the process is "nicer" to other processes by yielding CPU more readily.

Negative nice values (requires root) increase priority above normal. Positive nice values decrease priority. The kernel converts nice to a static priority value that influences CPU time distribution.

On Linux, CFS ignores nice values for normal scheduling (uses vruntime), but nice values affect the latency target. Batch and idle-class schedulers use nice directly for priority.

12. How does the kernel handle process limits (ulimit)?

The kernel enforces resource limits via the rlimit mechanism. Each process has a soft limit (what the process can change) and a hard limit (root-only ceiling). Limits include max processes (RLIMIT_NPROC), max open files (RLIMIT_NOFILE), max file size (RLIMIT_FSIZE), and CPU time (RLIMIT_CPU).

When a process hits a limit, the kernel returns an error — fork() returns EAGAIN, open() returns EMFILE, etc.

13. What is the difference between vfork() and fork()?

vfork() was introduced as an optimization before copy-on-write existed. It creates a child process without copying the parent's page tables — the child runs in the parent's address space until it calls exec() or exit(). The parent is blocked while the child runs.

vfork() is essentially obsolete now because COW makes fork() efficient. On modern Linux, vfork() is implemented as fork() with COW disabled for comparison purposes.

fork() with COW is faster in practice because many processes only read memory, requiring zero actual copying.

14. What happens when a process calls the exit() system call?

On POSIX/Linux, process termination releases most resources and records an exit status for the parent. A child can remain visible as a zombie until its parent collects that status with wait() or waitpid(). Exact cleanup and inspection APIs vary by OS.

In the Linux/POSIX case:

  • Closes all open file descriptors
  • Releases memory (page tables, file mappings)
  • Notifies the parent via SIGCHLD (if not ignored)
  • Retains a small process-table entry while the parent has not collected the status
  • Stores the exit code passed to exit() or returned from main()
15. How does process forking interact with threads in a multi-threaded program?

Forking a multi-threaded program creates a child with only the calling thread — all other threads disappear. This is a critical and often misunderstood behavior.

Only the thread that called fork() continues in the child. Other threads are not replicated. This leads to subtle bugs: a thread holding a mutex during fork() can leave that mutex locked permanently in the child.

Best practice: fork() followed immediately by exec() in the child, so the parent's address space is replaced entirely.

16. What can Linux process tools show about the kernel's process bookkeeping?

Linux keeps internal task structures, but /proc is a user-space interface that exposes selected process and thread information. It is not a complete dump of a PCB or of every kernel field.

For example, /proc/<pid>/ includes:

  • /proc/<pid>/status — selected state, IDs, thread count, and memory fields
  • /proc/<pid>/maps — process memory mappings
  • /proc/<pid>/fd — file descriptor links
17. What is a process group and when is it useful?

A process group is a collection of processes that share a PGID (process group ID). The group leader has a PGID equal to its PID. All members of a group receive signals together — pressing Ctrl+C sends SIGINT to all processes in the foreground process group.

Shells use process groups to manage pipelines: grep foo file | sort creates a process group containing grep and sort.

18. How does the kernel track CPU time per process?

Linux reports user and system CPU-time counters in fields 14 and 15 of /proc/<pid>/stat. They are an OS interface, not proof that each context switch writes a textbook PCB field. Use sysconf(_SC_CLK_TCK) to interpret the clock-tick units.

19. What is the difference between wait(), waitpid(), and wait3()/wait4()?

wait() suspends the caller until any child terminates, returning its PID and exit status. It blocks if no child has exited yet.

waitpid(pid, status, options) waits for a specific child (pid=-1 for any child). WNOHANG option makes it non-blocking — returns 0 if no child has exited.

wait3() and wait4() are older Unix variants that additionally fill a rusage struct with the child's resource usage. WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG macros extract information from the status integer.

20. How does containerization (Docker) relate to process concepts?

Docker containers are processes with isolated namespaces. Each container sees its own PID 1, its own network stack, its own filesystem mount, and its own process table — all created by namespace syscalls (clone with CLONE_NEW* flags).

Key namespaces: PID (isolated process numbering), NET (separate network stack), NS (mount points), USER (separate UID/GID mapping), IPC (separate SysV IPC objects).

cgroups (control groups) limit CPU, memory, and I/O per container. Namespaces + cgroups + capabilities = containers.

Further Reading


Conclusion

The five-state diagram and PCB are useful teaching models, while Linux and Windows expose different process states and creation APIs. To continue, read Process Scheduling to see how runnable work is chosen, then Threads & Lightweight Processes to compare execution within a shared process.

Category

Related Posts

ASLR & Stack Protection

Explore ASLR, PIE, stack canaries, and NX on Linux with GCC, then inspect a harmless C program to see what each mitigation does and where it falls short.

#operating-systems #aslr-stack-protection #computer-science

Assembly Language Basics: Writing Code the CPU Understands

Learn x86-64 Linux assembly with the System V ABI: track registers, assemble and run a small program, debug it in GDB, then compare AArch64.

#operating-systems #assembly-language-basics #computer-science

Boolean Logic & Gates

Understanding AND, OR, NOT gates and how they combine into arithmetic logic units — the building blocks of every processor.

#operating-systems #boolean-logic-gates #computer-science