Skip to content
Chethan Yadav_
Operating Systems

OS/Lesson 2 of 9/5 min read

Processes

Process vs program, memory layout, PCB, process states, context switching, fork/exec, zombie and orphan processes, and IPC.


On this page

Process vs Program

A program is a passive file on disk (an executable). A process is a program in execution: it has a program counter, registers, memory and open files. One program can spawn many processes (open three terminal windows, get three shell processes).

Process memory layout

From low to high addresses:

SegmentContents
Text (code)Compiled machine instructions, read-only
DataInitialized global and static variables
BSSUninitialized globals (zeroed at start)
HeapDynamic memory (malloc, new), grows upward
StackFunction frames, local variables, return addresses, grows downward

The gap between heap and stack lets both grow. Stack overflow happens when the stack crosses its limit (deep recursion).

Process Control Block

The Process Control Block (PCB) is the kernel data structure that represents a process. On Linux it is task_struct.

It stores:

  • Process ID (PID), parent PID
  • Process state
  • Program counter and CPU registers (saved during a context switch)
  • CPU scheduling info: priority, queue pointers
  • Memory management info: page table base, limits
  • Accounting info: CPU time used
  • I/O status: open file descriptors, devices

Process States

 new --admitted--> ready --dispatch--> running --exit--> terminated
                     ^  <--preempt/timer--  |
                     |                      | I/O or event wait
                     +---- waiting <--------+
                       I/O done
  • New: being created.
  • Ready: in memory, waiting for the CPU.
  • Running: instructions executing on a CPU. One process per core at a time.
  • Waiting (Blocked): waiting for I/O or an event.
  • Terminated: finished; PCB kept until the parent collects the exit status.

With swapping there are also suspended ready and suspended blocked states, where the process image is moved to disk.

Schedulers

  • Long-term scheduler (job scheduler): picks which jobs enter the ready queue; controls the degree of multiprogramming.
  • Short-term scheduler (CPU scheduler): picks the next ready process to run; runs very often. See CPU scheduling.
  • Medium-term scheduler: swaps processes out and in to reduce memory pressure.

Context Switch

A context switch is saving the state of the running process into its PCB and loading the saved state of another.

Steps: interrupt or system call enters the kernel, save registers and PC into PCB A, update A's state, pick B, load B's page table and registers, return to user mode in B.

Context switch time is pure overhead: no useful work happens. It also pollutes the CPU cache and the TLB, which makes the next few microseconds slower. Switching between threads of the same process is cheaper because the address space stays the same.

Process creation: fork and exec

On Unix, a process is created with fork():

pid_t pid = fork();
if (pid == 0) {
    // child: replace the image with a new program
    execlp("ls", "ls", "-l", NULL);
} else if (pid > 0) {
    wait(NULL);          // parent waits for child
} else {
    perror("fork failed");
}
  • fork() duplicates the calling process. It returns 0 in the child and the child's PID in the parent.
  • Modern kernels use copy-on-write: parent and child share pages until one writes, then only that page is copied.
  • exec() replaces the current process image with a new program (same PID).
  • wait() lets the parent collect the child's exit status.

Classic question: how many processes does this create?

fork(); fork(); fork();

Each fork doubles the count: 2^3 = 8 processes total (7 new ones).

Zombie Process

A zombie process has finished execution but still has an entry in the process table because its parent has not called wait() yet. It uses no CPU or memory except the PCB slot. Too many zombies can exhaust PIDs. Fix: parent calls wait(), or handles SIGCHLD.

Orphan Process

An orphan process is a running process whose parent has terminated. The OS re-parents it to init/systemd (PID 1), which reaps it when it exits. Daemons are intentionally orphaned processes.

Inter-Process Communication

Processes have isolated address spaces, so they need Inter-Process Communication (IPC) to cooperate. Reasons: information sharing, speedup, modularity.

Shared Memory

A region of memory is mapped into both processes' address spaces.

  • Very fast: after setup, no kernel involvement per message.
  • Processes must synchronize access themselves (see synchronization), otherwise race conditions occur.
  • API: shmget/shmat (System V), shm_open + mmap (POSIX).

Message Passing

Processes exchange messages through the kernel with send() and receive().

  • Easier and safer, no shared state.
  • Slower: every message is a system call and a copy.
  • Can be direct (name the process) or indirect (via mailboxes/ports), blocking (synchronous) or non-blocking (asynchronous).

Pipe

A pipe is a unidirectional byte stream between related processes (typically parent and child): ls | grep txt. A named pipe (FIFO) has a name in the file system, so unrelated processes can use it.

Other IPC mechanisms

  • Message queues: kernel-managed lists of typed messages.
  • Sockets: communication across machines (or locally via Unix domain sockets). See transport layer.
  • Signals: asynchronous notifications like SIGINT, SIGKILL, SIGTERM.
  • Memory-mapped files: map a file into memory and share it.

Quick revision

  • Process = program + execution state; PCB stores that state.
  • Five states: new, ready, running, waiting, terminated.
  • Context switch is overhead; threads switch cheaper than processes.
  • fork returns 0 to the child; n forks make 2^n processes.
  • Zombie: dead, not reaped. Orphan: alive, parent dead.
  • Shared memory is fastest IPC; message passing is simplest and safest.