Computer Science · College · BCA, BTech & MCA

Operating Systems: Process Scheduling, Context Switching & Deadlocks

Master OS core internals: Process Life Cycle, Process Control Blocks (PCB), CPU Scheduling Algorithms (FCFS, SJF, Round Robin), and the 4 Coffman Deadlock Conditions.

1Process States & The Process Control Block (PCB)

Operating Systems: Process Scheduling, Context Switching & Deadlocks: Subject, Target Audience, Topic Core Full size

Operating Systems: Process Scheduling, Context Switching & Deadlocks

Master OS core internals: Process Life Cycle, Process Control Blocks (PCB), CPU Scheduling Algorithms (FCFS, SJF, Round Robin), and the 4 Coffman Deadlock Conditions.

  1. 1Subject — Computer Science
  2. 2Target Audience — BCA, BTech & MCA Students
  3. 3Topic Core — Process Lifecycle, CPU Scheduling & Deadlocks

AProcessis a program in active execution. Unlike passive code stored on secondary storage, a process possesses an allocated address space (Text, Data, Heap, Stack) and runtime resources managed by the kernel via a data structure called theProcess Control Block (PCB).

Process Address Space Memory Segments

  • Text Segment

    Role in Memory
    Compiled executable binary machine instructions
    Growth Direction
    Fixed size
    Lifecycle / Allocation
    Read-only, loaded at program start
  • Data Segment

    Role in Memory
    Initialized and uninitialized (BSS) global and static variables
    Growth Direction
    Fixed size
    Lifecycle / Allocation
    Persists for entire process lifetime
  • Heap Segment

    Role in Memory
    Dynamically allocated memory via malloc()malloc(), new \text{new}\ , etc.
    Growth Direction
    Grows upward toward higher memory addresses
    Lifecycle / Allocation
    Managed explicitly by programmer or runtime GC
  • Stack Segment

    Role in Memory
    Local variables, function arguments, and stack activation frames
    Growth Direction
    Grows downward toward lower memory addresses
    Lifecycle / Allocation
    Automatically allocated and freed on function entry/exit

5-State Process Lifecycle Transitions

  1. 1

    New

    The process is being created and its PCB initialized in memory

  2. 2

    Ready

    Process resides in RAM in the Ready Queue, waiting to be assigned a CPU core by the scheduler

  3. 3

    Running

    CPU instructions are currently executing on a physical processor core

  4. 4

    Waiting / Blocked

    Process yielded CPU waiting for an I/O event, disk read, or network response

  5. 5

    Terminated

    Execution finished; kernel reclaims memory and closes open file descriptors

2CPU Scheduling Algorithms: FCFS, SJF, Priority & Round Robin

The CPU Scheduler selects a process from the ready queue and allocates the CPU. Schedulers are classified asNon-Preemptive(process keeps the CPU until it yields or terminates) orPreemptive(OS timer interrupt forces context switch back to ready queue).

CPU Scheduling Algorithms Comparison Matrix

  • First-Come, First-Served (FCFS)

    Type
    Non-Preemptive
    Decision Metric
    Arrival time
    Pros
    Simple to implement with FIFO queue
    Cons / Pitfalls
    Convoy Effect: short processes get blocked behind long CPU-bound process
  • Shortest Job First (SJF)

    Type
    Non-Preemptive
    Decision Metric
    Smallest CPU burst time
    Pros
    Provably optimal minimum average waiting time
    Cons / Pitfalls
    Impossible to know upcoming burst time in advance; starves long jobs
  • Shortest Remaining Time First (SRTF)

    Type
    Preemptive
    Decision Metric
    Smallest remaining burst time
    Pros
    Extremely responsive for short tasks
    Cons / Pitfalls
    High context switching overhead; starvation for CPU-intensive processes
  • Round Robin (RR)

    Type
    Preemptive
    Decision Metric
    Time Quantum (q) slice (e.g. 10–50ms)
    Pros
    Fair, starvation-free; optimal for interactive time-sharing systems
    Cons / Pitfalls
    Performance heavily dependent on quantum size: too small = excessive context switch overhead; too large = degenerates into FCFS
  • Priority Scheduling

    Type
    Both
    Decision Metric
    Numerical priority integer
    Pros
    Accommodates critical OS kernel tasks
    Cons / Pitfalls
    Indefinite blocking / starvation of low-priority jobs (mitigated by aging)

3Deadlocks: The 4 Coffman Conditions & Prevention

Definition

Deadlock — A situation in a multi-processing system where a set of processes are permanently blocked because each process holds a resource and waits for another resource held by another process in the set.

The Four Coffman Conditions for Deadlock (All 4 Must Hold Simultaneously)

  • 1. Mutual Exclusion

    Definition
    At least one resource must be held in a non-shareable mode (only one process uses it at a time)
    Prevention Strategy
    Make resources shareable where possible (e.g. read-only files)
  • 2. Hold and Wait

    Definition
    A process holding at least one resource is actively waiting to acquire additional resources held by other processes
    Prevention Strategy
    Require processes to request all resources simultaneously before starting, or release current holds before requesting new ones
  • 3. No Preemption

    Definition
    Resources cannot be forcibly seized; a resource can be released only voluntarily by the holding process
    Prevention Strategy
    If a process holding resources requests an unavailable resource, preempt and release all of its currently held resources
  • 4. Circular Wait

    Definition
    A closed cycle exists where P₀ waits for P₁, P₁ waits for P₂, and Pₙ waits for P₀
    Prevention Strategy
    Impose a strict global numerical ordering on all resources; processes may request resources only in strictly increasing order
Exam tip: Banker’s Algorithm(developed by Edsger Dijkstra) is a classic deadlockavoidancealgorithm that tests for safety by simulating the allocation of predetermined maximum resources before actually committing the allocation.

Remember

  • A Process address space comprises Text (code), Data (globals), Heap (dynamic allocations), and Stack (activation records).
  • Process states transition through New → Ready → Running → Waiting → Terminated via context switches.
  • SJF achieves provably optimal average waiting time; Round Robin guarantees responsiveness via time quanta slices.
  • Deadlock occurs if and only if all 4 Coffman conditions hold: Mutual Exclusion, Hold & Wait, No Preemption, and Circular Wait.

Check yourself

  1. Question 1

    Which of the following conditions is broken when an OS requires processes to acquire resources in a strictly ascending numerical order (e.g. Resource 1 before Resource 2)?
  2. Question 2

    In Round Robin CPU scheduling, what happens if the time quantum (slice) is set to an extraordinarily large value?

Read it? Now test it. Timed exam questions on this topic, then a report on what to revise.

Take a test

Want a lesson on another topic? Type any topic or add a photo of your notes — get a lesson like this in seconds.

Learn a topic

More Computer Science explained