Introduction

A program is a passive file of instructions on disk. A process is a program in execution: the code together with a program counter, register values, a stack, a heap, open files and everything else the OS must remember to stop it and later continue it exactly where it left off. Two users running the same editor have one program and two processes.

The process is the unit the OS schedules, protects and accounts for. Scheduling, synchronization, deadlock and memory management all build on the life cycle described here: a process is created, waits for the CPU, runs, waits for I/O, runs again, and finally terminates. The kernel records all of this in one data structure per process, the process control block.

Prerequisites

Core concepts

The process in memory

RegionContentsGrows
TextMachine code (often shared, read-only)Fixed
DataGlobal and static variablesFixed
HeapMemory from malloc and newUpward, toward the stack
StackActivation records: parameters, return addresses, local variablesDownward, toward the heap

The gap between heap and stack is what lets both grow. A stack overflow is the stack running into something it should not.

The process control block (PCB)

The PCB (called a task structure in Linux) holds everything the kernel needs about one process:

  • Identification: process ID, parent's ID, user ID.
  • CPU state: program counter, general registers, stack pointer, status word. Saved here when the process leaves the CPU.
  • Scheduling information: state, priority, pointers into scheduling queues, CPU time used.
  • Memory-management information: base and limit registers or the page-table pointer.
  • I/O and file information: the open-file table, pending I/O requests.
  • Accounting: CPU time, time limits.

The PCB is kernel data. It lives in protected kernel memory, not in user-accessible process memory, so a process cannot change its own priority or page table by writing memory.

The five-state model

StateMeaning
NewBeing created; PCB being set up; not yet admitted
ReadyIn memory, able to run, waiting only for a CPU
RunningExecuting on a CPU
Waiting (blocked)Cannot run until an event happens: I/O completion, a signal, a semaphore
TerminatedFinished; the parent has not yet collected its exit status, or the kernel is freeing its resources
Five-state process transition diagram
Figure 1. The five process states and the six legal transitions: admitted, dispatch, interrupt, I/O or event wait, I/O or event completion, and exit.

Legal transitions.

FromToCause
NewReadyAdmitted by the long-term scheduler
ReadyRunningDispatched by the short-term scheduler
RunningReadyPreempted: timer interrupt or a higher-priority process became ready
RunningWaitingProcess asked for I/O or waits on an event
WaitingReadyThe I/O or event completed
RunningTerminatedexit or a fatal error

Impossible transitions. Waiting to Running directly (a woken process must go through Ready and be chosen), Ready to Waiting (only a running process can issue the request that blocks it), and New to Running. In a non-preemptive system the Running to Ready edge is used only when a process yields voluntarily; it is never forced.

Counting limits on nn CPUs. At most nn processes are Running at any instant; Ready and Waiting have no fixed limit. With one CPU and kk processes in memory, at most one is Running and up to kk can be Ready or Waiting.

Suspended states

When memory is short, the medium-term scheduler swaps a whole process out to disk. That adds two states, giving the seven-state model: Ready-suspended (could run if it were in memory) and Blocked-suspended (waiting for an event and swapped out). A blocked-suspended process whose event occurs becomes ready-suspended; it becomes ready again when it is swapped in.

Schedulers

SchedulerChoosesRunsControls
Long-term (job)Which new jobs enter memoryRarely (seconds or minutes)Degree of multiprogramming, mix of CPU-bound and I/O-bound jobs
Short-term (CPU)Which ready process gets the CPUVery often (milliseconds)Response time; must be fast
Medium-termWhich process to swap out or inOccasionallyMemory pressure, thrashing

The degree of multiprogramming is the number of processes in memory. An I/O-bound process spends most time waiting for I/O and has short CPU bursts; a CPU-bound process has long bursts. A good long-term scheduler mixes both, so that the CPU and the devices are busy at the same time.

Context switch

Switching the CPU from process A to process B means saving A's CPU state into A's PCB and loading B's state from B's PCB, plus switching the address space (page-table base register and, on many machines, flushing or tagging the TLB). The switch does no useful work for either process: it is pure overhead, typically microseconds, and it grows with the amount of hardware state to save.

Context switch timeline between two processes
Figure 2. P0 runs from 0 to 10, the kernel saves P0 into PCB0 and reloads P1 from PCB1 from 10 to 11, P1 runs from 11 to 21, and the switch back takes 21 to 22.

Mode switch versus context switch. System calls and interrupts from user mode enter kernel mode; they may return to the same process or trigger a context switch. A context switch changes the running process/thread and executes kernel scheduling code, but need not itself change privilege mode if the CPU is already in kernel mode.

Termination, zombies and orphans

When a process calls exit, its memory and files are released but a small entry (PID and exit status) stays until the parent calls wait. Until then the dead child is a zombie. If the parent terminates first, the child becomes an orphan; on UNIX systems it is adopted by init (PID 1) or a designated subreaper, which collects it when it dies.

Mathematical explanation

CPU utilization with multiprogramming

Tanenbaum's simple probabilistic model assumes each of nn processes in memory independently spends a fraction pp of its time waiting for I/O. The CPU is idle only when all nn are waiting at once, so

U=1−pn.U = 1 - p^{n}.

This is an approximation: it ignores that processes compete for the one CPU, so it is optimistic for large nn. It still explains why adding processes raises utilization with diminishing returns.

Context-switch efficiency

If each process runs for a quantum qq and every switch costs ss, the fraction of CPU time doing useful work is

E=qq+s,E = \frac{q}{q + s},

assuming every quantum is fully used and then followed by a switch.

Worked examples

Utilization from the multiprogramming model

With p=0.8p = 0.8 (processes wait 80% of the time) and n=4n = 4: U=1−0.84=1−0.4096=0.5904U = 1 - 0.8^{4} = 1 - 0.4096 = 0.5904, about 59%.

How many processes are needed for at least 90% utilization? We need 0.8n≤0.10.8^{n} \le 0.1. Since 0.810≈0.1070.8^{10} \approx 0.107 and 0.811≈0.08590.8^{11} \approx 0.0859, the answer is n=11n = 11.

Counting state transitions

A process runs alone on one CPU with the burst pattern CPU 3, I/O 2, CPU 4, I/O 1, CPU 2, with no preemption. It enters Ready once on admission, then: Ready to Running 3 times (one per CPU burst), Running to Waiting 2 times (one per I/O burst), Waiting to Ready 2 times, Running to Terminated once. Its turnaround time, measured from admission, is 3+2+4+1+2=123 + 2 + 4 + 1 + 2 = 12 units, since nothing else competes.

Switch overhead

With q=10q = 10 ms and s=1s = 1 ms, E=10/11≈0.909E = 10/11 \approx 0.909: about 9.1% of CPU time is lost to switching. Cutting the quantum to q=2q = 2 ms gives E=2/3E = 2/3, so a third of the time is overhead. This is why the quantum must be large compared with the switch time.

Important properties and observations

  • A process is in exactly one state at a time; the PCB records which.
  • Being in memory is not the same as being ready: a waiting process is in memory but cannot run.
  • Only the running process can block itself; only an interrupt or the scheduler moves Running to Ready.
  • Context-switch time is overhead that every scheduling metric pays; when a switch cost is given, add it between consecutive runs.
  • The long-term scheduler controls the degree of multiprogramming; the medium-term scheduler reduces it temporarily by swapping.

Common misconceptions.

  • "A waiting process can be dispatched as soon as its I/O completes." It first becomes Ready.
  • "Swapping is the same as a context switch." Swapping moves a whole process between memory and disk; a context switch only changes which process the CPU runs.
  • "A zombie still holds its memory." Only its process-table entry remains.

GATE examination perspective

  • Legal and illegal transitions. Expect lists of transitions to classify. Waiting to Running and Ready to Waiting are illegal in the standard model.
  • Preemptive or not. An involuntary Running-to-Ready transition identifies preemption; it may follow a timer interrupt or another interrupt that makes a higher-priority process ready.
  • Counting processes in states. On nn CPUs at most nn are Running; the minimum in Ready or Waiting is 0.
  • Overheads. Add the switch time between consecutive runs of different processes; read whether a switch is also charged at time 0 or after the last process (state the convention you use).
  • PCB contents. The PCB stores registers, PC, state, scheduling and memory information; it does not store the process's code or heap.
  • Zombie and orphan. Zombie: child dead, parent has not waited. Orphan: parent dead, child alive.

Summary

Sources

  1. A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley: Chapter 3 (3.1 Process Concept, 3.2 Process Scheduling, 3.3 Operations on Processes).
  2. A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed., Pearson: Section 2.1 (2.1.5 Process States, 2.1.6 Implementation of Processes, 2.1.7 Modeling Multiprogramming).

Next steps