Processes and Threads for GATE CS – States, PCB, Context Switching | EngineeringHulk


Processes and Threads for GATE CS

Process states, PCB, context switching, threads vs processes, multithreading models — complete GATE CS coverage with worked examples.

Last updated: April 2026  |  GATE CS 2024–2026 syllabus

Key Takeaways for GATE

  • A process = program in execution. Has its own address space (code, data, heap, stack).
  • 5 process states: New → Ready → Running → Waiting → Terminated.
  • PCB stores all info OS needs to manage a process — PID, state, PC, registers, memory info, open files.
  • Context switch: save PCB of current process, load PCB of next. Pure overhead — no useful work done during switch.
  • Thread vs Process: Threads share code, data, heap. Each thread has own stack, PC, registers.
  • User-level threads (ULT): fast switching, but one blocking call blocks all threads in process.
  • Kernel-level threads (KLT): OS-managed, true parallelism on multicore, slower switching.

1. Process Concept

A process is a program in execution — an active entity, as opposed to a program which is a passive file on disk. The same program can be run as multiple simultaneous processes (e.g., two instances of a text editor).

Process memory layout (low to high address):
Text segment (code, read-only)  •  Data segment (global/static variables)
Heap (dynamic allocation, grows upward)  •  Stack (local vars, grows downward)

Process vs Program:
Program: passive — instructions stored on disk.
Process: active — program loaded into memory, executing with its own state.

2. Process States & Transitions

StateDescription
NewProcess is being created (PCB allocated, not yet admitted)
ReadyLoaded in memory, waiting in ready queue for CPU time
RunningInstructions being executed on CPU (only 1 per CPU core)
Waiting / BlockedWaiting for an event (I/O completion, signal, resource)
TerminatedFinished execution; PCB still exists briefly for parent to read exit status
State transitions:
New → Ready: admitted by long-term scheduler
Ready → Running: dispatched by short-term (CPU) scheduler
Running → Ready: preempted (time quantum expired or higher priority process arrives)
Running → Waiting: process requests I/O or waits for event
Waiting → Ready: I/O completes or event occurs
Running → Terminated: process finishes or is killed

Suspended states (some OS add 2 more):
Suspended Ready: swapped out to disk from ready state.
Suspended Waiting: swapped out while waiting for I/O.

3. Process Control Block (PCB)

The PCB (also called task control block) is the OS data structure that fully represents a process. It is created when a process is created and deleted when the process terminates.

PCB FieldContents
Process ID (PID)Unique identifier for the process
Process StateNew, Ready, Running, Waiting, Terminated
Program CounterAddress of next instruction to execute
CPU RegistersAll general-purpose registers, stack pointer, flags
Scheduling InfoPriority, scheduling queue pointers, CPU time used
Memory InfoBase/limit registers, page table base address
I/O StatusList of open files, I/O devices allocated
Accounting InfoCPU time used, real time used, time limits, job numbers

4. Context Switching

A context switch occurs when the OS switches the CPU from one process to another.

Steps:
1. Save state of current process P1 into its PCB (PC, registers, state).
2. Update PCB of P1 (state = Ready or Waiting).
3. Move PCB of P1 to appropriate queue.
4. Select next process P2 (scheduler decision).
5. Load state of P2 from its PCB (PC, registers).
6. Update PCB of P2 (state = Running).

Cost: Pure overhead — no useful computation during switch. Context switch time: ~1–10 μs.
Faster on modern CPUs with hardware support (e.g., task state segment in x86).

Context switch is more expensive than thread switch because process switch requires changing memory maps (page table base register), flushing TLB, etc.

5. Process Creation & Termination

fork() in Unix/Linux:
Creates exact copy of parent process. Returns:
• 0 in child process
• child’s PID in parent
• −1 on error

GATE trap — how many processes after n fork() calls in sequence?

fork(); fork(); fork();
// 2^3 = 8 processes total (including original)

Each fork() doubles the number of processes. After k fork() calls: 2k processes.

exec(): Replaces process image with new program. Does NOT create new process.
wait(): Parent waits for child to terminate. Reads exit status.
Zombie process: Terminated but PCB not yet reaped by parent (parent hasn’t called wait()).
Orphan process: Parent terminated before child. Adopted by init (PID 1).

6. Threads

A thread is the basic unit of CPU utilisation within a process. Multiple threads within the same process share resources but execute independently.

ResourceShared among threads?Per-thread?
Code (text segment)Yes
Data segment (globals)Yes
HeapYes
Open files, signalsYes
StackYes (own stack)
Program CounterYes
RegistersYes
Thread IDYes
Benefits of threads over multiple processes:
• Faster creation (no new address space allocation)
• Faster context switch (no TLB flush, same page table)
• Easy data sharing (same address space)
• Responsive UI (one thread handles I/O while another stays responsive)

Challenges: Race conditions, deadlocks, harder debugging.

7. User-level vs Kernel-level Threads

PropertyUser-level Threads (ULT)Kernel-level Threads (KLT)
Managed byUser-space thread libraryOperating system kernel
OS awarenessOS sees only 1 processOS knows each thread
Context switchFast (no kernel call)Slow (requires kernel call)
Blocking callBlocks entire processOnly that thread blocked
Multicore parallelismNo (OS schedules 1 entity)Yes (OS schedules each thread)
ExamplesPOSIX pthreads (old), Green threadsLinux pthreads (NPTL), Windows threads

8. Multithreading Models

Many-to-One (M:1): Many ULTs mapped to one KLT.
Pros: Fast thread management. Cons: One blocking call blocks all; no multicore parallelism.

One-to-One (1:1): Each ULT maps to one KLT.
Pros: True parallelism, blocking one thread doesn’t affect others.
Cons: Creating many KLTs is expensive. Used by Linux (NPTL), Windows.

Many-to-Many (M:N): M ULTs multiplexed onto N KLTs (N ≤ M).
Pros: Best of both — parallelism + efficiency. Cons: Complex implementation.

Two-level model: M:N with option to bind a ULT to a specific KLT. (Solaris)

9. GATE Examples

GATE 2019 — How many processes are created?

int main() {
    fork();
    fork();
    return 0;
}

After 1st fork: 2 processes. After 2nd fork (both processes fork): 4 processes.
Total = 22 = 4 processes (including original). New processes created = 3.

GATE 2020 — Which of the following is NOT shared between threads of the same process?
(A) Heap   (B) Global variables   (C) Stack   (D) Open file table
Each thread has its own stack. Heap, globals, and open files are shared.
Answer: (C) Stack
GATE 2022 — User-level threads vs kernel-level threads:
If a process with 4 user-level threads makes a blocking system call, how many threads continue executing?
With Many-to-One model (ULT): OS blocks the entire process. Answer: 0 threads continue.
With 1:1 model (KLT): only the calling thread blocks. Other 3 continue.

10. Common Mistakes

  • Counting fork() processes incorrectly: After k sequential fork() calls (each executing in all existing processes), total processes = 2k. But inside an if-else around fork(), count carefully — only one branch executes in each copy.
  • Process and thread stack confusion: Threads share heap (dynamic memory) but NOT stack. Local variables in one thread are NOT visible to another thread in the same process.
  • ULT blocking = process blocking: In Many-to-One model, one blocking I/O call blocks the entire process because the OS only sees one schedulable entity — not the individual threads.
  • Zombie vs Orphan mix-up: Zombie = child finished but parent hasn’t called wait(). Orphan = parent finished but child is still running (adopted by init). GATE asks which is which.
  • Context switch direction: On a context switch, the running process’s state is saved first, then the new process’s state is loaded. Both steps are OS overhead — no user code runs during the switch.

11. FAQ

What are the five states of a process?
New (being created), Ready (waiting for CPU in ready queue), Running (executing on CPU), Waiting/Blocked (waiting for I/O or event), and Terminated (finished, PCB being cleaned up). Key: only one process per CPU core can be in Running state at any time. A process moves from Running to Waiting when it requests I/O, and back to Ready when the I/O completes.
What is the difference between a process and a thread?
A process is an independent program in execution with its own address space — code, data, heap, and stack are private. A thread is a lightweight execution unit within a process — all threads share the process’s code, data, and heap, but each thread has its own stack, program counter, and register set. Thread creation is 10–100x faster than process creation, and thread context switching is cheaper because the address space doesn’t change.
What is stored in the Process Control Block (PCB)?
PCB stores everything the OS needs to manage and resume a process: PID, current state, program counter (next instruction), CPU register values, scheduling information (priority, queue pointers), memory management data (page table base, segment limits), I/O status (open files, allocated devices), and accounting info (CPU time consumed, time limits).
What is the difference between user-level and kernel-level threads?
User-level threads are managed by a user-space library — the kernel is unaware of them and sees only one process. They switch fast (no kernel call) but if one makes a blocking syscall, the entire process is blocked. Kernel-level threads are managed by the OS — each thread is individually schedulable, can run in parallel on multicores, and one blocking thread doesn’t affect others, but creation and switching are slower due to kernel involvement.