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
Master OS core internals: Process Life Cycle, Process Control Blocks (PCB), CPU Scheduling Algorithms (FCFS, SJF, Round Robin), and the 4 Coffman Deadlock Conditions.
- 1Subject — Computer Science
- 2Target Audience — BCA, BTech & MCA Students
- 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
| Segment | Role in Memory | Growth Direction | Lifecycle / Allocation |
|---|---|---|---|
| Text Segment | Compiled executable binary machine instructions | Fixed size | Read-only, loaded at program start |
| Data Segment | Initialized and uninitialized (BSS) global and static variables | Fixed size | Persists for entire process lifetime |
| Heap Segment | Dynamically allocated memory via , , etc. | Grows upward toward higher memory addresses | Managed explicitly by programmer or runtime GC |
| Stack Segment | Local variables, function arguments, and stack activation frames | Grows downward toward lower memory addresses | Automatically allocated and freed on function entry/exit |
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 , , 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
New
The process is being created and its PCB initialized in memory
- 2
Ready
Process resides in RAM in the Ready Queue, waiting to be assigned a CPU core by the scheduler
- 3
Running
CPU instructions are currently executing on a physical processor core
- 4
Waiting / Blocked
Process yielded CPU waiting for an I/O event, disk read, or network response
- 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
| Algorithm | Type | Decision Metric | Pros | Cons / Pitfalls |
|---|---|---|---|---|
| First-Come, First-Served (FCFS) | Non-Preemptive | Arrival time | Simple to implement with FIFO queue | Convoy Effect: short processes get blocked behind long CPU-bound process |
| Shortest Job First (SJF) | Non-Preemptive | Smallest CPU burst time | Provably optimal minimum average waiting time | Impossible to know upcoming burst time in advance; starves long jobs |
| Shortest Remaining Time First (SRTF) | Preemptive | Smallest remaining burst time | Extremely responsive for short tasks | High context switching overhead; starvation for CPU-intensive processes |
| Round Robin (RR) | Preemptive | Time Quantum (q) slice (e.g. 10–50ms) | Fair, starvation-free; optimal for interactive time-sharing systems | Performance heavily dependent on quantum size: too small = excessive context switch overhead; too large = degenerates into FCFS |
| Priority Scheduling | Both | Numerical priority integer | Accommodates critical OS kernel tasks | Indefinite blocking / starvation of low-priority jobs (mitigated by aging) |
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)
| Condition | Definition | Prevention Strategy |
|---|---|---|
| 1. Mutual Exclusion | At least one resource must be held in a non-shareable mode (only one process uses it at a time) | Make resources shareable where possible (e.g. read-only files) |
| 2. Hold and Wait | A process holding at least one resource is actively waiting to acquire additional resources held by other processes | Require processes to request all resources simultaneously before starting, or release current holds before requesting new ones |
| 3. No Preemption | Resources cannot be forcibly seized; a resource can be released only voluntarily by the holding process | If a process holding resources requests an unavailable resource, preempt and release all of its currently held resources |
| 4. Circular Wait | A closed cycle exists where P₀ waits for P₁, P₁ waits for P₂, and Pₙ waits for P₀ | Impose a strict global numerical ordering on all resources; processes may request resources only in strictly increasing order |
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
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
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)?Question 2
In Round Robin CPU scheduling, what happens if the time quantum (slice) is set to an extraordinarily large value?