Operating Systems
Unit 3: CPU Scheduling
From FCFS to Multilevel Feedback Queues โ master every scheduling algorithm with fully worked numerical problems, Gantt charts, and step-by-step solutions.
โฑ๏ธ 8 hrs theory + 6 hrs lab | ๐ฐ Earning Potential: โน5,000โโน20,000/month | ๐ 30 MCQs (Bloom's Mapped)
๐ผ Jobs this unlocks: Performance Engineer (โน6โ10 LPA) | SRE โ Site Reliability Engineer (โน8โ15 LPA) | Systems Programmer (โน7โ12 LPA)
Opening Hook โ The 60-Second Window That Breaks India's Internet
๐ IRCTC Tatkal Booking โ 12 Lakh Users, 60 Seconds Each
Every morning at 10:00 AM sharp, over 12 lakh (1.2 million) users simultaneously hit the IRCTC servers trying to book Tatkal tickets. Each user gets roughly a 60-second window before their session times out. The server must decide: Who gets CPU time first? How long? What if someone's request is more urgent?
This is CPU scheduling in action. IRCTC uses a variant of Round Robin scheduling โ each user request gets a fixed time quantum, ensuring no single request hogs the server. Priority queues ensure Tatkal requests (higher revenue) get processed before general waitlist queries.
The result? On a good day, 15,000 tickets are booked per minute. On a bad day? The entire system crashes because the scheduler couldn't handle the load. The difference between success and failure is the scheduling algorithm.
Could YOU have designed a better scheduler? By the end of this chapter, you'll know exactly how โ and you'll be able to prove it with numbers.
Learning Outcomes โ Bloom's Taxonomy Mapped
| Bloom's Level | Learning Outcome |
|---|---|
| ๐ต Remember | Define scheduling criteria (CPU utilization, throughput, turnaround time, waiting time, response time) and list all 7 scheduling algorithms |
| ๐ต Understand | Explain the difference between preemptive and non-preemptive scheduling, and describe the convoy effect, starvation, and aging |
| ๐ข Apply | Solve numerical problems for FCFS, SJF, SRTF, Priority, and Round Robin โ compute Gantt charts, waiting time, and turnaround time |
| ๐ข Analyze | Compare scheduling algorithms on the basis of average waiting time, throughput, and fairness using computed results |
| ๐ Evaluate | Evaluate which scheduling algorithm is optimal for given real-world scenarios (e.g., IRCTC, trading systems, batch processing) |
| ๐ Create | Design a multilevel feedback queue scheduling policy for a given workload mix and implement a CPU scheduler simulator in Python |
Concept Explanation โ CPU Scheduling Algorithms with Full Numericals
1. Scheduling Criteria & Key Formulas
The CPU scheduler decides which process in the ready queue gets the CPU next. But how do we measure if a scheduling algorithm is "good"? We use these five criteria:
๐ Key Scheduling Metrics & Formulas
1. CPU Utilization โ Keep the CPU as busy as possible (target: 40%โ90%). Idle CPU = wasted resource.
2. Throughput โ Number of processes completed per unit time. Higher = better.
3. Turnaround Time (TAT) โ Total time from submission to completion.
TAT = Completion Time (CT) โ Arrival Time (AT)
4. Waiting Time (WT) โ Time a process spends waiting in the ready queue (not executing).
WT = TAT โ Burst Time (BT) = CT โ AT โ BT
5. Response Time (RT) โ Time from submission to first CPU allocation.
RT = First CPU Time โ Arrival Time (AT)
Goal: Maximize CPU utilization and throughput. Minimize turnaround time, waiting time, and response time.
2. Preemptive vs Non-Preemptive Scheduling
| Feature | Non-Preemptive | Preemptive |
|---|---|---|
| CPU Release | Process runs until it finishes or blocks for I/O | OS can forcibly take CPU from a running process |
| Context Switches | Fewer | More (overhead) |
| Response Time | Higher (long jobs block short ones) | Lower (short jobs can preempt long ones) |
| Complexity | Simpler to implement | More complex (need to save/restore state) |
| Starvation Risk | Lower in FCFS, higher in SJF | Possible in Priority/SRTF |
| Examples | FCFS, SJF (non-preemptive), Priority (non-preemptive) | SRTF, RR, Priority (preemptive) |
| Best For | Batch systems | Interactive/real-time systems |
3. The Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the scheduler. It performs:
- Context switching โ saving the state of the old process and loading the state of the new process
- Switching to user mode from kernel mode
- Jumping to the proper location in the user program to resume execution
Dispatch Latency = time the dispatcher takes to stop one process and start another. This should be as small as possible (typically microseconds). Every context switch incurs dispatch latency โ this is why too many context switches (e.g., tiny RR quantum) hurt performance.
4. Algorithm 1: FCFS (First Come First Served)
Concept: The simplest scheduling algorithm. Processes are executed in the order they arrive. It is non-preemptive โ once a process gets the CPU, it runs to completion.
Implementation: FIFO queue. New processes go to the back. The process at the front gets the CPU.
โ๏ธ Worked Example: FCFS
๐ FCFS Numerical โ Step-by-Step Solution
Given:
| Process | Arrival Time (AT) | Burst Time (BT) |
|---|---|---|
| P1 | 0 | 24 |
| P2 | 1 | 3 |
| P3 | 2 | 3 |
Find: Gantt Chart, Waiting Time, Turnaround Time, Avg WT, Avg TAT
Solution:
Step 1: At t=0, only P1 has arrived. P1 starts execution. Runs from t=0 to t=24.
Step 2: At t=24, P1 finishes. P2 arrived at t=1 (already waiting). P2 runs from t=24 to t=27.
Step 3: At t=27, P2 finishes. P3 arrived at t=2 (already waiting). P3 runs from t=27 to t=30.
Gantt Chart:
Calculation Table:
| Process | AT | BT | CT | TAT = CTโAT | WT = TATโBT |
|---|---|---|---|---|---|
| P1 | 0 | 24 | 24 | 24 โ 0 = 24 | 24 โ 24 = 0 |
| P2 | 1 | 3 | 27 | 27 โ 1 = 26 | 26 โ 3 = 23 |
| P3 | 2 | 3 | 30 | 30 โ 2 = 28 | 28 โ 3 = 25 |
Avg WT = (0 + 23 + 25) / 3 = 48 / 3 = 16.00
Avg TAT = (24 + 26 + 28) / 3 = 78 / 3 = 26.00
5. Algorithm 2: SJF (Shortest Job First โ Non-Preemptive)
Concept: Among all processes in the ready queue, select the one with the smallest burst time. It is provably optimal for minimizing average waiting time among non-preemptive algorithms.
Problem: We can't always know the burst time in advance (prediction needed). Also causes starvation โ long processes may never get CPU if short processes keep arriving.
โ๏ธ Worked Example: SJF (Non-Preemptive)
๐ SJF Numerical โ Step-by-Step Solution
Given:
| Process | Arrival Time (AT) | Burst Time (BT) |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 4 | 1 |
| P4 | 5 | 4 |
Find: Gantt Chart, WT, TAT, Avg WT, Avg TAT
Solution:
Step 1: At t=0, only P1 has arrived (AT=0). P1 runs from t=0 to t=7.
Step 2: At t=7, P2 (BT=4), P3 (BT=1), P4 (BT=4) are all in ready queue. Shortest BT = P3 (BT=1). P3 runs from t=7 to t=8.
Step 3: At t=8, P2 (BT=4) and P4 (BT=4) remain. Tie! Break by arrival time: P2 (AT=2) arrived before P4 (AT=5). P2 runs from t=8 to t=12.
Step 4: At t=12, only P4 remains. P4 runs from t=12 to t=16.
Gantt Chart:
Calculation Table:
| Process | AT | BT | CT | TAT = CTโAT | WT = TATโBT |
|---|---|---|---|---|---|
| P1 | 0 | 7 | 7 | 7 โ 0 = 7 | 7 โ 7 = 0 |
| P2 | 2 | 4 | 12 | 12 โ 2 = 10 | 10 โ 4 = 6 |
| P3 | 4 | 1 | 8 | 8 โ 4 = 4 | 4 โ 1 = 3 |
| P4 | 5 | 4 | 16 | 16 โ 5 = 11 | 11 โ 4 = 7 |
Avg WT = (0 + 6 + 3 + 7) / 4 = 16 / 4 = 4.00
Avg TAT = (7 + 10 + 4 + 11) / 4 = 32 / 4 = 8.00
6. Algorithm 3: SRTF (Shortest Remaining Time First โ Preemptive SJF)
Concept: The preemptive version of SJF. Whenever a new process arrives, compare its burst time with the remaining time of the currently running process. If the new process has a shorter remaining time, preempt the current process.
SRTF is provably optimal โ it gives the minimum possible average waiting time among all scheduling algorithms.
โ๏ธ Worked Example: SRTF (Same processes as SJF for comparison)
๐ SRTF Numerical โ Step-by-Step Preemption Trace
Given: (Same as SJF example)
| Process | Arrival Time (AT) | Burst Time (BT) |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 4 | 1 |
| P4 | 5 | 4 |
Find: Gantt Chart with preemption points, WT, TAT, Avg WT, Avg TAT
Solution โ Trace at each arrival event:
t=0: Only P1 available (remaining=7). P1 starts running.
t=2: P2 arrives (BT=4). Compare: P1(remaining=5) vs P2(remaining=4). P2 is shorter โ Preempt P1! P2 starts. P1 goes back to ready queue with remaining=5.
t=4: P3 arrives (BT=1). Compare: P2(remaining=2) vs P3(remaining=1) vs P1(remaining=5). P3 is shortest โ Preempt P2! P3 starts. P2 goes back with remaining=2.
t=5: P3 finishes. P4 arrives (BT=4). Ready queue: P2(rem=2), P1(rem=5), P4(rem=4). Shortest = P2(rem=2) โ P2 runs.
t=7: P2 finishes. Ready queue: P4(rem=4), P1(rem=5). Shortest = P4(rem=4) โ P4 runs.
t=11: P4 finishes. Only P1(rem=5) left โ P1 runs.
t=16: P1 finishes. All done.
Gantt Chart:
Calculation Table:
| Process | AT | BT | CT | TAT = CTโAT | WT = TATโBT |
|---|---|---|---|---|---|
| P1 | 0 | 7 | 16 | 16 โ 0 = 16 | 16 โ 7 = 9 |
| P2 | 2 | 4 | 7 | 7 โ 2 = 5 | 5 โ 4 = 1 |
| P3 | 4 | 1 | 5 | 5 โ 4 = 1 | 1 โ 1 = 0 |
| P4 | 5 | 4 | 11 | 11 โ 5 = 6 | 6 โ 4 = 2 |
Avg WT = (9 + 1 + 0 + 2) / 4 = 12 / 4 = 3.00
Avg TAT = (16 + 5 + 1 + 6) / 4 = 28 / 4 = 7.00
โข SJF: Avg WT = 4.00, Avg TAT = 8.00
โข SRTF: Avg WT = 3.00, Avg TAT = 7.00
SRTF wins! Preemption allows short arriving jobs to run immediately, reducing overall waiting. But SRTF has more context switches (4 switches vs 3 in SJF) โ there's a trade-off.
7. Algorithm 4: Priority Scheduling
Concept: Each process is assigned a priority number. The CPU is allocated to the process with the highest priority. Convention: lower number = higher priority (in most textbooks and this chapter).
Can be preemptive (new high-priority process preempts current) or non-preemptive (current process finishes first).
โ๏ธ Worked Example: Priority Scheduling (Non-Preemptive)
๐ Priority (Non-Preemptive) โ Step-by-Step Solution
Given: (Lower number = higher priority)
| Process | AT | BT | Priority |
|---|---|---|---|
| P1 | 0 | 10 | 3 |
| P2 | 1 | 1 | 1 |
| P3 | 2 | 2 | 4 |
| P4 | 3 | 1 | 5 |
| P5 | 4 | 5 | 2 |
Solution:
Step 1: At t=0, only P1 (pri=3) available. Non-preemptive โ P1 runs completely: t=0 to t=10.
Step 2: At t=10, all processes have arrived. Priorities: P2(1), P5(2), P3(4), P4(5). Highest = P2 (pri=1). P2 runs t=10 to t=11.
Step 3: At t=11, remaining: P5(2), P3(4), P4(5). Highest = P5 (pri=2). P5 runs t=11 to t=16.
Step 4: At t=16, remaining: P3(4), P4(5). Highest = P3 (pri=4). P3 runs t=16 to t=18.
Step 5: At t=18, only P4 (pri=5) left. P4 runs t=18 to t=19.
Gantt Chart:
Calculation Table:
| Process | AT | BT | Priority | CT | TAT | WT |
|---|---|---|---|---|---|---|
| P1 | 0 | 10 | 3 | 10 | 10 | 0 |
| P2 | 1 | 1 | 1 | 11 | 10 | 9 |
| P3 | 2 | 2 | 4 | 18 | 16 | 14 |
| P4 | 3 | 1 | 5 | 19 | 16 | 15 |
| P5 | 4 | 5 | 2 | 16 | 12 | 7 |
Avg WT = (0 + 9 + 14 + 15 + 7) / 5 = 45 / 5 = 9.00
Avg TAT = (10 + 10 + 16 + 16 + 12) / 5 = 64 / 5 = 12.80
โ๏ธ Worked Example: Priority Scheduling (Preemptive)
๐ Priority (Preemptive) โ Step-by-Step Solution
Given: (Lower number = higher priority)
| Process | AT | BT | Priority |
|---|---|---|---|
| P1 | 0 | 4 | 2 |
| P2 | 1 | 3 | 1 |
| P3 | 2 | 1 | 3 |
| P4 | 3 | 5 | 4 |
| P5 | 4 | 2 | 5 |
Solution โ Trace at each arrival:
t=0: P1(pri=2) starts running.
t=1: P2(pri=1) arrives. pri=1 < pri=2 โ Preempt P1! P2 runs. P1 goes to ready queue (remaining=3).
t=2: P3(pri=3) arrives. P2(pri=1) still has highest priority โ P2 continues.
t=3: P4(pri=4) arrives. P2(pri=1) still highest โ P2 continues.
t=4: P2 finishes (CT=4). P5(pri=5) arrives. Ready: P1(rem=3, pri=2), P3(rem=1, pri=3), P4(rem=5, pri=4), P5(rem=2, pri=5). Highest = P1(pri=2) โ P1 runs.
t=7: P1 finishes (CT=7). Ready: P3(pri=3), P4(pri=4), P5(pri=5). Highest = P3(pri=3) โ P3 runs.
t=8: P3 finishes (CT=8). Ready: P4(pri=4), P5(pri=5). Highest = P4(pri=4) โ P4 runs.
t=13: P4 finishes (CT=13). Only P5(pri=5) left โ P5 runs.
t=15: P5 finishes (CT=15). All done.
Gantt Chart:
Calculation Table:
| Process | AT | BT | Priority | CT | TAT | WT |
|---|---|---|---|---|---|---|
| P1 | 0 | 4 | 2 | 7 | 7 | 3 |
| P2 | 1 | 3 | 1 | 4 | 3 | 0 |
| P3 | 2 | 1 | 3 | 8 | 6 | 5 |
| P4 | 3 | 5 | 4 | 13 | 10 | 5 |
| P5 | 4 | 2 | 5 | 15 | 11 | 9 |
Avg WT = (3 + 0 + 5 + 5 + 9) / 5 = 22 / 5 = 4.40
Avg TAT = (7 + 3 + 6 + 10 + 11) / 5 = 37 / 5 = 7.40
Starvation & Aging
Starvation: Low-priority processes may wait indefinitely if high-priority processes keep arriving. In the example above, P5 (pri=5) waits until t=13!
Aging: The solution โ gradually increase the priority of waiting processes. For example, increase priority by 1 every 15 minutes. Eventually, even the lowest-priority process becomes the highest.
8. Algorithm 5: Round Robin (RR)
Concept: Each process gets a fixed time quantum (time slice). After the quantum expires, the process is preempted and placed at the back of the ready queue. The next process in the queue gets the CPU. This is the most widely used algorithm for time-sharing systems.
Key Design Decision: Quantum size matters enormously:
- Too small (e.g., 1ms) โ too many context switches โ overhead dominates
- Too large (e.g., 100ms) โ degenerates into FCFS
- Rule of thumb: 80% of CPU bursts should be shorter than the quantum
โ๏ธ Worked Example: Round Robin with Quantum = 2
๐ Round Robin (q=2) โ Step-by-Step Solution
Given:
| Process | Arrival Time (AT) | Burst Time (BT) |
|---|---|---|
| P1 | 0 | 5 |
| P2 | 1 | 3 |
| P3 | 2 | 1 |
| P4 | 3 | 2 |
| P5 | 4 | 3 |
Time Quantum = 2
Solution โ Ready Queue Trace:
t=0โ2: P1 runs for 2ms (remaining=3). During this time, P2 arrives at t=1. Queue after: [P2, P1]
t=2โ4: P2 runs for 2ms (remaining=1). P3 arrives at t=2, P4 at t=3. Queue after: [P1, P3, P4, P2]
t=4โ5: P3 runs for 1ms, finishes! (remaining=0). P5 arrives at t=4. Queue after: [P4, P2, P5, P1]
Wait โ P3 only needed 1ms but got a 2ms quantum. It finishes early and releases CPU.
t=5โ7: P1 runs for 2ms (remaining=1). Queue after: [P2, P5, P4, P1]
Correction on ordering โ let me re-trace carefully:
Detailed trace:
t=0: Ready=[P1]. P1 runs.
t=1: P2 arrives โ Ready=[P2] (P1 still running).
t=2: P1 quantum expires (rem=3). P3 arrives. P1 goes to back. Ready=[P2, P3, P1]. P2 runs.
t=3: P4 arrives โ Ready=[P3, P1, P4] (P2 still running).
t=4: P2 quantum expires (rem=1). P5 arrives. P2 goes to back. Ready=[P3, P1, P4, P5, P2]. P3 runs.
t=5: P3 finishes (needed only 1ms). Ready=[P1, P4, P5, P2]. P1 runs.
t=7: P1 quantum expires (rem=1). Ready=[P4, P5, P2, P1]. P4 runs.
t=9: P4 finishes (needed exactly 2ms). Ready=[P5, P2, P1]. P5 runs.
t=10: P2 wasn't served yet โ let me re-check. Ready=[P5, P2, P1]. P5 runs for 2ms.
t=11: P5 quantum expires (rem=1). Ready=[P2, P1, P5]. P2 runs.
t=12: P2 finishes (rem was 1, needs 1ms). Ready=[P1, P5]. P1 runs.
t=13: P1 finishes (rem was 1, needs 1ms). Ready=[P5]. P5 runs.
t=14: P5 finishes (rem was 1). All done.
Gantt Chart:
Calculation Table:
| Process | AT | BT | CT | TAT = CTโAT | WT = TATโBT |
|---|---|---|---|---|---|
| P1 | 0 | 5 | 13 | 13 โ 0 = 13 | 13 โ 5 = 8 |
| P2 | 1 | 3 | 12 | 12 โ 1 = 11 | 11 โ 3 = 8 |
| P3 | 2 | 1 | 5 | 5 โ 2 = 3 | 3 โ 1 = 2 |
| P4 | 3 | 2 | 9 | 9 โ 3 = 6 | 6 โ 2 = 4 |
| P5 | 4 | 3 | 14 | 14 โ 4 = 10 | 10 โ 3 = 7 |
Avg WT = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.80
Avg TAT = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.60
โ๏ธ Worked Example: Round Robin with Quantum = 4 (Same Processes)
๐ Round Robin (q=4) โ Impact of Larger Quantum
Given: Same process table. Time Quantum = 4.
Solution:
t=0: Ready=[P1]. P1 runs for 4ms (rem=1). P2(t=1), P3(t=2), P4(t=3) arrive during.
t=4: P1 quantum expires. P5 arrives. Ready=[P2, P3, P4, P5, P1]. P2 runs for 3ms, finishes at t=7.
t=7: P2 done. Ready=[P3, P4, P5, P1]. P3 runs for 1ms, finishes at t=8.
t=8: P3 done. Ready=[P4, P5, P1]. P4 runs for 2ms, finishes at t=10.
t=10: P4 done. Ready=[P5, P1]. P5 runs for 3ms, finishes at t=13.
t=13: P5 done. Ready=[P1]. P1 runs for 1ms (rem=1), finishes at t=14.
Gantt Chart:
Calculation Table:
| Process | AT | BT | CT | TAT | WT |
|---|---|---|---|---|---|
| P1 | 0 | 5 | 14 | 14 | 9 |
| P2 | 1 | 3 | 7 | 6 | 3 |
| P3 | 2 | 1 | 8 | 6 | 5 |
| P4 | 3 | 2 | 10 | 7 | 5 |
| P5 | 4 | 3 | 13 | 9 | 6 |
Avg WT = (9 + 3 + 5 + 5 + 6) / 5 = 28 / 5 = 5.60
Avg TAT = (14 + 6 + 6 + 7 + 9) / 5 = 42 / 5 = 8.40
Impact of Quantum Size โ Comparison
| Metric | q = 2 | q = 4 |
|---|---|---|
| Avg Waiting Time | 5.80 | 5.60 |
| Avg Turnaround Time | 8.60 | 8.40 |
| Context Switches | 8 | 5 |
| Response Time (P1) | 0 | 0 |
In this example, q=4 happens to give slightly better Avg WT, but with fewer context switches. However, for interactive systems, smaller quantum gives better response time for newly arriving processes.
WT = CT โ AT โ BTThis formula works for ALL algorithms! Build the Gantt chart first, read CT, then compute directly.
9. Algorithm 6: Multilevel Queue Scheduling
Concept: The ready queue is divided into multiple separate queues, each with its own scheduling algorithm. Processes are permanently assigned to a queue based on their type.
๐๏ธ Multilevel Queue Structure
Queue 1 (Highest Priority): System Processes โ Scheduled using Priority
Queue 2: Interactive Processes (foreground) โ Scheduled using Round Robin
Queue 3: Interactive Editing Processes โ Scheduled using RR
Queue 4: Batch Processes โ Scheduled using FCFS
Queue 5 (Lowest Priority): Student Processes โ Scheduled using FCFS
Inter-Queue Scheduling: Fixed-priority preemptive. Queue 1 must be empty before Queue 2 gets CPU. This means batch processes may starve if interactive processes keep arriving.
Alternative: Time-slicing between queues โ e.g., 80% of CPU to foreground (RR), 20% to background (FCFS).
10. Algorithm 7: Multilevel Feedback Queue Scheduling (MLFQ)
Key difference from Multilevel Queue: Processes can move between queues! This is the most sophisticated and flexible scheduling algorithm.
๐ How MLFQ Works
Queue 0 (Highest Priority): RR with quantum = 8ms
Queue 1: RR with quantum = 16ms
Queue 2 (Lowest Priority): FCFS
Rules:
1. New processes enter Queue 0 (highest priority).
2. If a process uses its full quantum in Queue 0 without finishing, it's demoted to Queue 1.
3. If a process uses its full quantum in Queue 1 without finishing, it's demoted to Queue 2.
4. If a process voluntarily gives up CPU (I/O), it stays in the same queue or moves up.
5. Aging: Processes that wait too long in lower queues get promoted back to Queue 0.
Why it's brilliant:
โข Short, interactive processes finish quickly in Queue 0 โ great response time
โข CPU-bound processes naturally sink to lower queues โ don't hurt interactive users
โข I/O-bound processes stay in high-priority queues โ good I/O utilization
โข Aging prevents starvation
11. Multiprocessor Scheduling
When there are multiple CPUs, scheduling becomes more complex:
| Approach | Description | Example |
|---|---|---|
| Asymmetric Multiprocessing | One master CPU handles all scheduling; others execute user code | Older mainframes |
| Symmetric Multiprocessing (SMP) | Each CPU has its own scheduler; shared ready queue or per-CPU queues | Modern Linux, Windows |
| Processor Affinity | Try to keep a process on the same CPU (warm cache) | Linux sched_setaffinity() |
| Load Balancing | Distribute work evenly across CPUs (push/pull migration) | Linux work-stealing scheduler |
12. Real-Time Scheduling
Hard Real-Time: Deadlines MUST be met (pacemakers, ABS brakes). Missing a deadline = system failure.
Soft Real-Time: Deadlines are preferred but not absolute (video streaming, gaming).
| Algorithm | Full Name | How It Works | Optimal? |
|---|---|---|---|
| EDF | Earliest Deadline First | Process with nearest deadline runs first. Dynamic priority. | Yes โ for single-processor, preemptive |
| RMS | Rate Monotonic Scheduling | Shorter period = higher priority. Static priority assignment. | Optimal among fixed-priority algorithms |
13. Thread Scheduling
| Type | Full Name | Description |
|---|---|---|
| PCS | Process Contention Scope | User-level threads compete among threads of the SAME process. Thread library schedules. OS doesn't know about them. |
| SCS | System Contention Scope | Kernel-level threads compete with ALL threads in the system. OS scheduler handles them directly. |
Modern systems (Linux, Windows) use SCS โ every thread is a kernel thread, scheduled by the OS.
Learn by Doing โ 3-Tier Lab Structure
๐ข Tier 1 โ GUIDED TASK: Python CPU Scheduler Simulator
What You'll Build:
A Python program that takes a process table as input and computes Gantt chart + metrics for FCFS, SJF (Non-Preemptive), and Round Robin.
Python def fcfs(processes): """First Come First Served Scheduling""" # Sort by arrival time, then by process id procs = sorted(processes, key=lambda x: (x['at'], x['pid'])) time = 0 gantt = [] results = [] for p in procs: if time < p['at']: time = p['at'] # CPU idle until process arrives start = time time += p['bt'] ct = time tat = ct - p['at'] wt = tat - p['bt'] gantt.append((p['pid'], start, ct)) results.append({'pid': p['pid'], 'at': p['at'], 'bt': p['bt'], 'ct': ct, 'tat': tat, 'wt': wt}) return gantt, results def sjf_non_preemptive(processes): """Shortest Job First (Non-Preemptive)""" procs = [p.copy() for p in processes] n = len(procs) done = [False] * n time = 0 gantt = [] results = [] completed = 0 while completed < n: # Find available processes (arrived and not done) available = [(i, p) for i, p in enumerate(procs) if not done[i] and p['at'] <= time] if not available: time += 1 continue # Pick shortest burst time (tie-break by arrival) idx, p = min(available, key=lambda x: (x[1]['bt'], x[1]['at'])) start = time time += p['bt'] ct = time tat = ct - p['at'] wt = tat - p['bt'] gantt.append((p['pid'], start, ct)) results.append({'pid': p['pid'], 'at': p['at'], 'bt': p['bt'], 'ct': ct, 'tat': tat, 'wt': wt}) done[idx] = True completed += 1 return gantt, results def round_robin(processes, quantum): """Round Robin Scheduling""" from collections import deque procs = sorted([p.copy() for p in processes], key=lambda x: x['at']) n = len(procs) remaining = {p['pid']: p['bt'] for p in procs} ct_map = {} time = 0 gantt = [] queue = deque() idx = 0 # next process to arrive # Add first process if procs: time = procs[0]['at'] queue.append(procs[0]['pid']) idx = 1 while queue: pid = queue.popleft() exec_time = min(quantum, remaining[pid]) start = time time += exec_time remaining[pid] -= exec_time gantt.append((pid, start, time)) # Add newly arrived processes while idx < n and procs[idx]['at'] <= time: queue.append(procs[idx]['pid']) idx += 1 if remaining[pid] > 0: queue.append(pid) # Not finished, back to queue else: ct_map[pid] = time # Finished! # If queue empty but processes remain if not queue and idx < n: time = procs[idx]['at'] queue.append(procs[idx]['pid']) idx += 1 results = [] for p in procs: ct = ct_map[p['pid']] tat = ct - p['at'] wt = tat - p['bt'] results.append({'pid': p['pid'], 'at': p['at'], 'bt': p['bt'], 'ct': ct, 'tat': tat, 'wt': wt}) return gantt, results def print_results(algo_name, gantt, results): print(f"\n{'='*50}") print(f" {algo_name}") print(f"{'='*50}") # Gantt Chart print("\nGantt Chart:") chart = "| " times = "" for pid, start, end in gantt: width = max(4, (end - start) * 2) chart += f"{pid}".center(width) + "| " print(chart) # Timeline time_line = "" for pid, start, end in gantt: width = max(4, (end - start) * 2) + 2 time_line += str(start).ljust(width) time_line += str(gantt[-1][2]) print(time_line) # Results Table print(f"\n{'Process':<10}{'AT':<6}{'BT':<6}{'CT':<6}{'TAT':<6}{'WT':<6}") print("-" * 40) total_wt, total_tat = 0, 0 for r in results: print(f"{r['pid']:<10}{r['at']:<6}{r['bt']:<6}{r['ct']:<6}{r['tat']:<6}{r['wt']:<6}") total_wt += r['wt'] total_tat += r['tat'] n = len(results) print(f"\nAvg WT = {total_wt}/{n} = {total_wt/n:.2f}") print(f"Avg TAT = {total_tat}/{n} = {total_tat/n:.2f}") # โโ Main Program โโ if __name__ == "__main__": processes = [ {'pid': 'P1', 'at': 0, 'bt': 5}, {'pid': 'P2', 'at': 1, 'bt': 3}, {'pid': 'P3', 'at': 2, 'bt': 1}, {'pid': 'P4', 'at': 3, 'bt': 2}, {'pid': 'P5', 'at': 4, 'bt': 3}, ] g, r = fcfs(processes) print_results("FCFS (First Come First Served)", g, r) g, r = sjf_non_preemptive(processes) print_results("SJF (Shortest Job First - Non-Preemptive)", g, r) g, r = round_robin(processes, quantum=2) print_results("Round Robin (Quantum = 2)", g, r)
Sample Output:
scheduler.py and run python scheduler.py. Try changing the process table and quantum value. Verify your manual calculations match the program output โ this is the best way to prepare for exams.
๐ก Tier 2 โ SEMI-GUIDED: Add I/O Burst Handling
Your Mission:
Modify the Tier 1 simulator to handle I/O bursts. Each process now has alternating CPU and I/O bursts.
Hints:
- Data Structure: Change burst time to a list:
bursts: [cpu, io, cpu, io, cpu] - States: Add a
BLOCKEDstate for processes doing I/O. Blocked processes should be in a separate I/O queue. - I/O Completion: When I/O finishes, move the process back to the ready queue for its next CPU burst.
- Idle Time: Track CPU idle time when no process is in the ready queue (but some are doing I/O).
- Metrics: Add CPU utilization = (total time โ idle time) / total time ร 100.
๐ด Tier 3 โ OPEN CHALLENGE: CPU Scheduler Visualizer with tkinter
The Brief:
Build a GUI-based CPU Scheduler Visualizer using Python's tkinter library.
Requirements:
- Input Panel: Form to enter process table (AT, BT, Priority) dynamically (add/remove rows)
- Algorithm Selector: Dropdown to choose FCFS, SJF, SRTF, Priority, RR
- Quantum Input: Text field (only visible when RR is selected)
- Visual Gantt Chart: Color-coded horizontal bars representing each process execution segment
- Metrics Panel: Display CT, TAT, WT for each process + averages
- Comparison Mode: Run all algorithms on the same input, show side-by-side results
- Export: Save Gantt chart as PNG image
Deliverable: Push to GitHub. Include a README with screenshots. Share the repository link.
Industry Spotlight โ A Day in the Life
๐ฉโ๐ป Meera Krishnamurthy, 29 โ Performance Engineer at Zerodha, Bangalore
Background: B.Tech in Computer Science from NIT Surathkal. Fascinated by OS internals since her 3rd semester. Did a summer internship at HasGeek (Bangalore) working on server optimization. Joined Zerodha as a Systems Engineer and grew into a Performance Engineer role within 2 years.
A Typical Day:
8:30 AM โ Review overnight batch job scheduling logs. Check if any trading data reconciliation jobs missed their SLA windows. Investigate a batch job that took 3ร longer than expected โ turns out a low-priority cleanup job was starving the critical reconciliation job. Fix: implement aging in the job scheduler.
10:00 AM โ Tune Linux kernel CFS scheduler parameters for the trading engine servers. Adjust sched_min_granularity_ns and sched_latency_ns to reduce tail latency for order execution.
12:00 PM โ Benchmark the order matching engine. The goal: process 1 million orders per second with p99 latency under 500 microseconds. Today's test shows p99 at 480ยตs โ within SLA!
2:00 PM โ Write a custom BPF (Berkeley Packet Filter) program to trace context switch frequency on production servers. Too many context switches = scheduler thrashing.
4:00 PM โ Team meeting: discuss moving from CFS to a custom real-time scheduling policy for the order matching thread. Priority scheduling with aging for the matching engine, RR for client-facing API threads.
5:30 PM โ Document today's findings. Update Grafana dashboards showing scheduler metrics.
| Detail | Info |
|---|---|
| Tools Used Daily | Linux perf, BPF/eBPF, Python, Go, Prometheus, Grafana, strace, vmstat |
| Entry Salary | โน6โ10 LPA |
| Mid-Level (3โ5 yrs) | โน12โ20 LPA |
| Senior (7+ yrs) | โน25โ45 LPA |
| Companies Hiring | Zerodha, Google, Microsoft, Amazon, Flipkart, Razorpay, Uber, LinkedIn, VMware, Red Hat |
Earn With It โ Freelance & Income Roadmap
๐ฐ Your Earning Path After This Chapter
Portfolio Pieces:
โข Python CPU Scheduler Simulator โ working code on GitHub
โข Scheduler Visualizer GUI โ tkinter project with screenshots
โข Performance benchmarking report โ Linux scheduler tuning analysis
Freelance Gig Ideas:
โข Performance benchmarking scripts for startups โ โน5,000โโน15,000
โข OS tuning reports for server optimization (cloud VMs) โ โน8,000โโน20,000
โข CPU profiling and optimization for cloud workloads โ โน10,000โโน25,000
โข Write technical blog posts on OS scheduling โ โน2,000โโน5,000/article
| Platform | Best For | Typical Rate |
|---|---|---|
| Toptal | Senior systems engineering gigs | $40โ$80/hour |
| Upwork | Linux server optimization, performance tuning | $20โ$50/hour |
| Fiverr | Quick scripts, benchmarking tools | โน3,000โโน15,000/gig |
| Internshala | Indian students โ OS/Linux internships | โน5,000โโน15,000/month |
| Direct outreach to CTOs of Indian startups | โน8,000โโน25,000/project |
โฑ๏ธ Time to First Earning: 3โ4 weeks (complete Tier 1 + Tier 3 labs, build GitHub portfolio, apply on Internshala)
MCQ Assessment Bank โ 30 Questions (Bloom's Mapped)
Remember / Identify (Q1โQ5)
Which scheduling algorithm is non-preemptive and processes requests in the order they arrive?
- SJF
- Round Robin
- FCFS
- SRTF
Turnaround Time (TAT) is defined as:
- Burst Time โ Arrival Time
- Completion Time โ Arrival Time
- Waiting Time + Arrival Time
- Completion Time โ Burst Time
Which scheduling algorithm is provably optimal for minimizing average waiting time?
- FCFS
- Round Robin
- SRTF (Shortest Remaining Time First)
- Priority Scheduling
In Round Robin scheduling, what happens when a process's time quantum expires?
- The process is terminated
- The process is moved to the blocked queue
- The process is preempted and placed at the back of the ready queue
- The process continues until it finishes
The dispatcher is responsible for all of the following EXCEPT:
- Context switching
- Switching to user mode
- Selecting the next process to run
- Jumping to the correct location in the user program
Understand / Explain (Q6โQ10)
What is the "convoy effect" in FCFS scheduling?
- All processes finish at the same time
- Short processes get stuck waiting behind a long process, increasing average waiting time
- Processes are executed in reverse order
- CPU utilization drops to zero
Why does SJF scheduling suffer from starvation?
- It uses too many context switches
- Long processes may never get the CPU if shorter processes keep arriving
- It doesn't support I/O operations
- It requires hardware support
In the context of Round Robin scheduling, what happens if the time quantum is set to an extremely large value?
- It becomes identical to SJF
- It becomes identical to FCFS
- It becomes identical to Priority Scheduling
- The system crashes
What is "aging" in the context of scheduling?
- Removing old processes from the system
- Gradually increasing the priority of processes that have been waiting a long time
- Decreasing the time quantum over time
- Archiving completed processes
How does MLFQ (Multilevel Feedback Queue) differ from a simple Multilevel Queue?
- MLFQ uses only one queue
- In MLFQ, processes can move between queues based on their behavior
- MLFQ doesn't support preemption
- Multilevel Queue allows process movement but MLFQ doesn't
Apply โ Numerical Problems (Q11โQ20)
Given: P1(AT=0, BT=5), P2(AT=1, BT=3), P3(AT=2, BT=4). Using FCFS, what is the average waiting time?
- 3.00
- 4.00
- 3.33
- 5.00
Solution:
Gantt: |P1(0โ5)|P2(5โ8)|P3(8โ12)|
P1: CT=5, TAT=5โ0=5, WT=5โ5=0
P2: CT=8, TAT=8โ1=7, WT=7โ3=4
P3: CT=12, TAT=12โ2=10, WT=10โ4=6
Avg WT = (0+4+6)/3 = 10/3 = 3.33
Given: P1(AT=0, BT=6), P2(AT=2, BT=2), P3(AT=4, BT=8), P4(AT=5, BT=3). Using SJF (Non-Preemptive), what is the average turnaround time?
- 7.75
- 8.50
- 8.25
- 9.00
Solution:
t=0: Only P1. P1 runs 0โ6.
t=6: P2(BT=2), P3(BT=8), P4(BT=3) available. Shortest=P2. P2 runs 6โ8.
t=8: P3(BT=8), P4(BT=3). Shortest=P4. P4 runs 8โ11.
t=11: P3 runs 11โ19.
P1: TAT=6โ0=6. P2: TAT=8โ2=6. P3: TAT=19โ4=15. P4: TAT=11โ5=6.
Avg TAT = (6+6+15+6)/4 = 33/4 = 8.25
Given: P1(AT=0, BT=8), P2(AT=1, BT=4), P3(AT=2, BT=9), P4(AT=3, BT=5). Using SRTF, which process completes first?
- P1
- P2
- P3
- P4
Solution:
t=0: P1(rem=8) runs.
t=1: P2(rem=4) arrives. P1(rem=7) vs P2(rem=4). P2 shorter โ preempt. P2 runs.
t=2: P3(rem=9) arrives. P2(rem=3) still shortest โ continues.
t=3: P4(rem=5) arrives. P2(rem=2) still shortest โ continues.
t=5: P2 finishes! (CT=5). P2 completes first.
Given: P1(AT=0, BT=3), P2(AT=1, BT=6), P3(AT=2, BT=4), P4(AT=3, BT=2). Using FCFS, what is the waiting time of P3?
- 5
- 7
- 6
- 4
Solution:
Gantt: |P1(0โ3)|P2(3โ9)|P3(9โ13)|P4(13โ15)|
P3: CT=13, TAT=13โ2=11, WT=11โ4=7
Given: P1(AT=0, BT=4), P2(AT=1, BT=5), P3(AT=2, BT=2), P4(AT=3, BT=1). Using SJF (Non-Preemptive), what is the average waiting time?
- 2.50
- 3.00
- 2.75
- 3.25
Solution:
t=0: Only P1. P1 runs 0โ4.
t=4: P2(BT=5), P3(BT=2), P4(BT=1). Shortest=P4(BT=1). P4 runs 4โ5.
t=5: P2(BT=5), P3(BT=2). Shortest=P3(BT=2). P3 runs 5โ7.
t=7: P2 runs 7โ12.
P1: WT=0. P2: WT=(12โ1)โ5=6. P3: WT=(7โ2)โ2=3. P4: WT=(5โ3)โ1=1.
Avg WT = (0+6+3+1)/4 = 10/4 = 2.50
Wait โ let me recheck. Actually answer is (A) 2.50.
Corrected: โ Answer: (A) 2.50
Given: P1(AT=0, BT=6), P2(AT=1, BT=2), P3(AT=2, BT=8). Using SRTF, what is the completion time of P1?
- 6
- 8
- 10
- 16
Solution:
t=0: P1(rem=6) starts.
t=1: P2(rem=2) arrives. P1(rem=5) vs P2(rem=2). P2 shorter โ preempt. P2 runs.
t=2: P3(rem=8) arrives. P2(rem=1) still shortest โ continues.
t=3: P2 done. P1(rem=5) vs P3(rem=8). P1 shorter โ P1 runs.
t=8: P1 done. CT(P1) = 8.
t=8โ16: P3 runs.
Given: P1(AT=0, BT=4), P2(AT=0, BT=3), P3(AT=0, BT=5). Using Round Robin with quantum=2, what is the completion time of P2?
- 7
- 9
- 6
- 8
Solution (all arrive at t=0, ready order: P1,P2,P3):
t=0โ2: P1 runs (rem=2). Queue: [P2,P3,P1]
t=2โ4: P2 runs (rem=1). Queue: [P3,P1,P2]
t=4โ6: P3 runs (rem=3). Queue: [P1,P2,P3]
t=6โ7: P1 runs? No โ P1 has rem=2 so runs t=6โ8. But let me trace carefully.
Actually: t=6โ8: P1 runs (rem=0), done. Queue: [P2,P3]
Wait โ P1 rem=2, quantum=2, runs 6โ8, done.
t=6โ8: P1 runs 2ms, finishes. Queue: [P2,P3]
t=8โ9: P2 runs 1ms, finishes. Queue: [P3]
Hmm, then CT(P2)=9, not 7.
Let me retrace: t=0โ2: P1(rem=4โ2). t=2โ4: P2(rem=3โ1). t=4โ6: P3(rem=5โ3). t=6โ8: P1(rem=2โ0, done). t=8โ9: P2(rem=1โ0, done). CT(P2)=9.
Corrected: โ Answer: (B) 9
Given: P1(AT=0, BT=3, Priority=3), P2(AT=1, BT=1, Priority=1), P3(AT=2, BT=4, Priority=2). Using Preemptive Priority (lower number = higher priority), what is the average waiting time?
- 1.33
- 2.00
- 1.67
- 2.33
Solution:
t=0: P1(pri=3) starts.
t=1: P2(pri=1) arrives. pri=1 < pri=3 โ preempt P1. P2 runs.
t=2: P2 finishes (BT=1). P3(pri=2) arrives. Ready: P1(rem=2,pri=3), P3(rem=4,pri=2). P3(pri=2) higher โ P3 runs.
t=6: P3 finishes. P1(rem=2) runs.
t=8: P1 finishes.
Gantt: |P1(0โ1)|P2(1โ2)|P3(2โ6)|P1(6โ8)|
P1: CT=8, TAT=8, WT=8โ3=5
P2: CT=2, TAT=1, WT=1โ1=0
P3: CT=6, TAT=4, WT=4โ4=0
Avg WT = (5+0+0)/3 = 5/3 = 1.67
Given: P1(AT=0, BT=10), P2(AT=0, BT=5), P3(AT=0, BT=8). Using SJF (Non-Preemptive) with all arriving at t=0, what is the waiting time of P1?
- 0
- 5
- 13
- 8
Solution:
All arrive at t=0. SJF order: P2(BT=5) โ P3(BT=8) โ P1(BT=10).
Gantt: |P2(0โ5)|P3(5โ13)|P1(13โ23)|
P1: CT=23, TAT=23, WT=23โ10=13
Given: P1(AT=0, BT=7), P2(AT=2, BT=4), P3(AT=4, BT=1), P4(AT=5, BT=4). Using SRTF, what is the average waiting time?
- 2.00
- 3.00
- 4.00
- 2.50
Solution: (Same as our SRTF worked example!)
Gantt: |P1(0โ2)|P2(2โ4)|P3(4โ5)|P2(5โ7)|P4(7โ11)|P1(11โ16)|
P1: CT=16, TAT=16, WT=16โ7=9
P2: CT=7, TAT=5, WT=5โ4=1
P3: CT=5, TAT=1, WT=1โ1=0
P4: CT=11, TAT=6, WT=6โ4=2
Avg WT = (9+1+0+2)/4 = 12/4 = 3.00
Analyze / Compare (Q21โQ25)
For the same set of processes, which ordering correctly ranks algorithms from lowest to highest average waiting time (in general)?
- FCFS < SJF < SRTF
- SRTF < SJF < FCFS
- RR < FCFS < SJF
- SJF < SRTF < FCFS
A system has a mix of 70% I/O-bound and 30% CPU-bound processes. Which scheduling approach would be MOST effective?
- FCFS
- Multilevel Feedback Queue with I/O-bound processes in higher-priority queues
- SJF only
- Priority scheduling with static priorities
Why does increasing the time quantum in Round Robin not always improve performance?
- Because larger quantum means more context switches
- Because it increases response time for newly arriving short processes
- Because it requires more memory
- Because it causes starvation
Compare SRTF and Priority Scheduling: In which scenario would Priority Scheduling be preferred over SRTF?
- When all processes have the same burst time
- When some processes have urgent real-time deadlines that must be met regardless of burst time
- When minimizing average waiting time is the only goal
- When there is only one process
In a Multilevel Queue with fixed-priority inter-queue scheduling, what is the primary disadvantage?
- Too many queues to manage
- Processes in lower-priority queues may starve
- It doesn't support Round Robin
- It requires kernel modification
Evaluate / Create (Q26โQ30)
A web server handles requests of varying lengths. Short requests (API calls, ~5ms) are 80% of traffic. Long requests (report generation, ~500ms) are 20%. Which scheduling approach maximizes user satisfaction?
- FCFS โ simplest to implement
- MLFQ โ short requests finish quickly in high-priority queue, long requests move to lower queues
- SJF โ always pick the shortest job
- Round Robin with quantum=250ms
An embedded system controlling an automobile's ABS (Anti-lock Braking System) needs to process brake sensor data within 10ms. Which scheduling approach is mandatory?
- Round Robin with quantum=5ms
- FCFS
- Hard real-time scheduling with EDF or RMS
- MLFQ
A student is designing a CPU scheduler for a new OS. The requirements are: (1) responsive for interactive users, (2) fair to CPU-bound batch jobs, (3) no starvation. Which single algorithm best fits?
- FCFS
- SRTF
- Multilevel Feedback Queue with aging
- Non-preemptive Priority
You're optimizing a Linux server running both a real-time trading application and background analytics jobs. What combination of scheduler classes would you use?
- SCHED_FIFO for trading, SCHED_OTHER for analytics
- SCHED_OTHER for both
- SCHED_RR for both
- SCHED_BATCH for trading, SCHED_FIFO for analytics
Design question: If you were building a scheduling policy for IRCTC's Tatkal booking system with 12 lakh concurrent users, which combination would you propose?
- FCFS for all requests
- Priority queue for Tatkal + Round Robin for each priority level + aging for general queue
- Random scheduling
- Single SRTF queue for all requests
Short Answer Questions
Q1. Compare FCFS and SJF scheduling algorithms with an example. Which gives better average waiting time?
Answer: FCFS (First Come First Served) executes processes in arrival order โ simple but suffers from the convoy effect. SJF (Shortest Job First) selects the process with the smallest burst time โ optimal for non-preemptive scheduling.
Example: Processes: P1(AT=0, BT=6), P2(AT=0, BT=2), P3(AT=0, BT=8).
FCFS: Order P1โP2โP3. WT: P1=0, P2=6, P3=8. Avg WT = 14/3 = 4.67.
SJF: Order P2โP1โP3. WT: P2=0, P1=2, P3=8. Avg WT = 10/3 = 3.33.
SJF always gives โค FCFS average waiting time. However, SJF requires knowing burst times in advance and may cause starvation for long processes.
Q2. What is the convoy effect? Illustrate with an example.
Answer: The convoy effect occurs in FCFS scheduling when a long CPU-bound process arrives before several short processes. All short processes must wait for the long process to complete, creating a "convoy" of waiting processes โ similar to slow trucks blocking fast cars on a highway.
Example: P1(AT=0, BT=100), P2(AT=1, BT=2), P3(AT=2, BT=1). With FCFS: P2 waits 99ms and P3 waits 100ms for their 2ms and 1ms jobs! Avg WT = (0+99+100)/3 = 66.33ms. If we used SJF after P1 finishes (since P1 arrived first and started first), we'd still have the convoy. The fundamental issue: FCFS doesn't consider burst time.
Q3. Explain starvation and aging in the context of priority scheduling.
Answer: Starvation occurs when a low-priority process waits indefinitely because higher-priority processes keep arriving and getting the CPU. In priority scheduling, if a process with priority=10 (low) is waiting while processes with priority=1 (high) keep entering the system, the low-priority process may never execute.
Aging is the solution: the OS gradually increases the priority of waiting processes over time. For example, every 15 minutes, increase priority by 1. A process with initial priority=10 will become priority=9 after 15 minutes, priority=8 after 30 minutes, and so on. Eventually, it reaches priority=1 and gets scheduled. This guarantees every process will eventually execute, preventing starvation while still respecting priorities.
Q4. How does the choice of time quantum affect Round Robin performance?
Answer:
Too Small Quantum (e.g., 1ms): Frequent context switches โ high overhead (dispatch latency accumulates). Most CPU time is spent switching rather than executing useful work. Throughput drops.
Too Large Quantum (e.g., 1000ms): Degenerates into FCFS โ each process finishes within one quantum. Loses the advantage of time-sharing. Response time increases for interactive users.
Optimal Quantum: Rule of thumb โ 80% of CPU bursts should be shorter than the quantum. Typical values: 10โ100ms. Linux CFS uses dynamic quanta based on system load. A well-tuned quantum balances response time (good for interactive users) with throughput (minimal context switch overhead).
Q5. Compare Multilevel Queue and Multilevel Feedback Queue scheduling.
Answer:
| Feature | Multilevel Queue | Multilevel Feedback Queue |
|---|---|---|
| Process Movement | Permanent โ processes stay in assigned queue | Dynamic โ processes move between queues |
| Flexibility | Low โ fixed classification | High โ adapts to process behavior |
| Starvation | Possible (lower queues may starve) | Prevented via aging |
| I/O-bound Handling | Depends on initial assignment | I/O-bound processes stay in high-priority queues |
| Complexity | Simpler | More complex |
| Used In | Simple embedded systems | Modern OS (Linux CFS inspired, Windows) |
MLFQ is preferred for general-purpose OS because it automatically classifies processes based on behavior without requiring prior knowledge of process types.
Case Studies
๐ Case Study 1: IRCTC Tatkal Booking โ Scheduling 12 Lakh Concurrent Users
Background:
Every day at 10:00 AM, IRCTC's servers face a tsunami of 12 lakh (1.2 million) concurrent users attempting to book Tatkal railway tickets. Each user session has approximately 60 seconds of active interaction (filling forms, making payment). The total ticket inventory is limited โ only a few thousand seats per train. The system must be fair, responsive, and prevent server crashes.
The Scheduling Challenge:
| Factor | Requirement | Challenge |
|---|---|---|
| Concurrency | 1.2 million users simultaneously | Server must time-share CPU across millions of request threads |
| Fairness | No user should be unfairly prioritized (within the same booking class) | FCFS alone would cause convoy effect โ one slow payment blocks others |
| Response Time | Each page must load in <3 seconds | Users will abandon if page takes >5 seconds |
| Priority | Tatkal > Premium Tatkal > General > Wait-list queries | Multiple priority levels needed |
| Starvation | General queue users should eventually get served | Tatkal flood shouldn't completely block general bookings |
Proposed Scheduling Solution:
Architecture: Multilevel Feedback Queue with Time-Slicing
- Queue 1 (Highest): Premium Tatkal requests โ Round Robin, quantum=50ms, 40% CPU share
- Queue 2: Regular Tatkal requests โ Round Robin, quantum=100ms, 35% CPU share
- Queue 3: General booking requests โ Round Robin, quantum=200ms, 20% CPU share
- Queue 4 (Lowest): PNR status checks, wait-list queries โ FCFS, 5% CPU share
Aging Rule: If a general queue request waits more than 30 seconds, promote it to Queue 2.
Load Balancing: Distribute across 500+ servers using consistent hashing. Each server runs this MLFQ independently.
Discussion Questions:
- Why can't IRCTC use pure FCFS for all users?
- What would happen if the time quantum for Queue 1 were set to 1 second instead of 50ms?
- How does aging prevent general queue starvation during the 10 AM Tatkal rush?
- Should payment processing threads have different priority than form-filling threads? Why?
๐ Case Study 2: Zerodha โ Real-Time Trading System OS Design
Background:
Zerodha processes over 15 million orders per day with a peak rate of 1 million+ orders per minute during market opening (9:15 AM IST). The order matching engine must process each order in under 500 microseconds (0.5ms). A delay of even 1 millisecond can mean financial losses for traders.
The OS Scheduling Requirements:
| Component | Latency Requirement | Scheduling Class |
|---|---|---|
| Order Matching Engine | <500 ยตs (hard real-time) | SCHED_FIFO (Linux real-time, priority 99) |
| Market Data Feed Handler | <1 ms | SCHED_FIFO (priority 90) |
| Risk Management Module | <5 ms | SCHED_RR (real-time, priority 80) |
| Client API Server | <50 ms | SCHED_OTHER (CFS, nice -10) |
| Analytics & Reporting | <5 seconds | SCHED_BATCH (lowest priority) |
| Log Archival | No deadline | SCHED_IDLE (only when CPU is idle) |
Key Design Decisions:
1. CPU Isolation: The order matching engine is pinned to dedicated CPU cores (CPU affinity) using taskset. No other processes run on these cores โ eliminating context switch overhead entirely.
2. Real-Time Priority: The matching engine thread runs with SCHED_FIFO at the highest priority. It never gets preempted by any other process (except hardware interrupts).
3. NUMA-Aware Scheduling: Memory allocation is done on the same NUMA node as the CPU to avoid cross-node memory access latency.
4. Timer Resolution: Linux kernel is compiled with CONFIG_HZ=1000 for 1ms timer resolution. For sub-microsecond precision, the engine uses busy-waiting (spin locks) instead of sleep-based scheduling.
Priority of Orders:
Market Orders (execute immediately at best price) get higher priority than Limit Orders (execute only at specified price). Within the same type, orders are processed in FCFS order โ ensuring time-priority fairness as required by SEBI regulations.
Discussion Questions:
- Why does Zerodha use SCHED_FIFO instead of CFS for the matching engine?
- What would happen if the analytics thread accidentally got SCHED_FIFO priority?
- How does CPU pinning reduce scheduling overhead?
- Why can't Zerodha use Round Robin for the order matching engine?
- How do SEBI regulations influence the choice of scheduling algorithm for order processing?
Chapter Summary
๐ Key Takeaways โ CPU Scheduling
1. Scheduling Criteria: CPU Utilization, Throughput, Turnaround Time (TAT = CT โ AT), Waiting Time (WT = TAT โ BT), Response Time (RT = First CPU โ AT).
2. FCFS: Simple, non-preemptive, FIFO order. Suffers from convoy effect. Avg WT can be very high.
3. SJF (Non-Preemptive): Optimal for non-preemptive avg WT. Requires knowing burst time. Causes starvation.
4. SRTF (Preemptive SJF): Provably optimal for minimum avg WT across ALL algorithms. More context switches.
5. Priority Scheduling: Explicit priorities. Can be preemptive or non-preemptive. Causes starvation โ fix with aging.
6. Round Robin: Time quantum-based. Fair for interactive systems. Quantum too small โ overhead. Too large โ becomes FCFS.
7. Multilevel Queue: Multiple fixed queues with different algorithms. No movement between queues.
8. MLFQ: Most sophisticated. Processes move between queues based on behavior. Aging prevents starvation. Used in real OS.
9. Multiprocessor: SMP, processor affinity, load balancing.
10. Real-Time: EDF (dynamic, optimal), RMS (static, optimal among fixed-priority).
11. Dispatcher: Handles context switch, mode switch, program counter jump. Dispatch latency = overhead per switch.
12. Key Formula Chain: Build Gantt Chart โ Read CT โ TAT = CT โ AT โ WT = TAT โ BT. Always this order!
Earning Checkpoint โ What Can You Do Now?
| Skill Learned | Tool/Method | Portfolio Artifact | Earning Ready? |
|---|---|---|---|
| Scheduling Algorithm Numericals | Manual + Python verification | Solved problem sets (10+ problems) | โ Yes โ strong for exam + interview prep tutoring |
| Python Scheduler Simulator | Python (FCFS, SJF, RR) | GitHub repository with working code | โ Yes โ shows coding + OS knowledge |
| Gantt Chart Construction | Manual + Python | Step-by-step solutions | โ Yes โ tutoring for juniors (โน500โโน1000/hr) |
| Algorithm Comparison | Analytical | Comparison tables with metrics | โ Yes โ interview preparation content |
| Linux Scheduler Concepts | Conceptual (CFS, SCHED_FIFO) | โ | โฌ Not yet โ need hands-on Linux tuning |
| Scheduler Visualizer GUI | Python/tkinter | GUI application on GitHub | โ Yes โ portfolio project for placements |
| Performance Analysis | Conceptual | Case study analysis documents | โ Yes โ technical writing for blogs |
โ Unit 3 complete. Numerical problems included: 10+. MCQs: 30. Ready for Unit 4!
[QR: Link to EduArtha video tutorial โ CPU Scheduling Algorithms]