Operating Systems
CPU Scheduling
The most reliable Operating Systems marks: learn the definitions once, then draw a Gantt chart for every algorithm until completion, turnaround and waiting times come out without thinking.
GATE CSE CPU scheduling — FCFS, SJF, SRTF, Priority with aging, Round Robin, Gantt charts, waiting / turnaround / response time, convoy effect, preemptive vs non-preemptive and exponential averaging, with worked traces, samples and drills.
Learning outcomes
- Compute completion, turnaround, waiting and response time from a Gantt chart
- Simulate FCFS, SJF, SRTF, non-preemptive and preemptive Priority and Round Robin including arrival times
- Explain convoy effect, starvation and aging, and know which algorithm is optimal for average waiting time
- Use exponential averaging to predict CPU bursts and recognise HRRN and multilevel queues
Scheduling criteria and basic terms
- The CPU scheduler picks one process from the ready queue when the running process (1) switches from running to waiting, (2) switches from running to ready (interrupt), (3) switches from waiting to ready, or (4) terminates. In cases 1 and 4 scheduling is NON-PREEMPTIVE (the process keeps the CPU until it blocks or ends); in cases 2 and 3 it can be PREEMPTIVE (the OS may take the CPU away).
- Definitions per process: ARRIVAL TIME (AT), BURST TIME (BT, total CPU time needed), COMPLETION TIME (CT, clock time when it finishes), TURNAROUND TIME (TAT = CT - AT), WAITING TIME (WT = TAT - BT, time spent in the ready queue), RESPONSE TIME (RT = time of first CPU allocation - AT).
- System-wide criteria: maximise CPU utilization and throughput (processes completed per unit time); minimise turnaround, waiting and response time. Average values are what GATE asks.
- If the CPU is idle between jobs (no process has arrived), the Gantt chart shows an idle gap; completion times continue from the next arrival.
- Non-preemptive algorithms: response time = waiting time (the first time a process gets the CPU it runs to completion). In preemptive algorithms response time can be smaller than waiting time.
- CONVOY EFFECT: in FCFS, many short processes wait behind one long CPU-bound process, which lowers CPU and device utilization and increases average waiting time.
- Always list the tie-break rule: equal arrival time or equal burst time is broken by the lower process number / earlier arrival unless the question says otherwise.
- Context-switch overhead is usually ignored in GATE numericals unless it is given explicitly.
- Shortcuts: for NON-PREEMPTIVE algorithms WT = (start time) - AT. For PREEMPTIVE algorithms use WT = CT - AT - BT (the process may have waited in several pieces). Check: sum of all BT + total idle time = finishing time of the last process.
- CPU UTILIZATION = busy time / total time. Example: P1 (AT 0, BT 2) and P2 (AT 5, BT 3): P1 runs 0-2, the CPU is idle 2-5, P2 runs 5-8; utilization = 5/8 = 62.5%, throughput = 2 processes / 8 = 0.25 per unit, and both waiting times are 0.
- DISPATCH LATENCY is the time to stop one process and start another; the dispatcher is invoked on every context switch. A scheduler decision that is also a preemption needs a timer interrupt (RR) or an arrival event (SRTF, preemptive priority).
- Average TAT = average WT + average BT, because TAT = WT + BT for each process. This identity gives a quick cross-check of every Gantt-chart answer.
Formulas to remember
-
TAT = CT - AT -
WT = TAT - BT -
RT = (first time on CPU) - AT -
Throughput = number of processes / total time
Worked example
Problem. A process arrives at t = 2, needs 5 ms of CPU and completes at t = 14. Find TAT and WT.
Solution. TAT = CT - AT = 14 - 2 = 12 ms. WT = TAT - BT = 12 - 5 = 7 ms.
FCFS, SJF, SRTF and Priority
- FCFS (first come first served): non-preemptive, ready queue is a FIFO. Simple and fair but suffers the convoy effect; average waiting time is often high.
- SJF (shortest job first): chooses the ready process with the smallest next CPU burst. NON-PREEMPTIVE SJF gives the minimum average waiting time among all non-preemptive algorithms, but can starve long processes and needs burst lengths in advance (they can only be predicted).
- SRTF (shortest remaining time first) is PREEMPTIVE SJF: whenever a new process arrives with a burst shorter than the REMAINING time of the running process, it preempts. SRTF gives the minimum average waiting time over all algorithms.
- PRIORITY scheduling: the CPU goes to the highest-priority ready process (check whether a smaller number means higher priority!). It can be preemptive or non-preemptive. Indefinite blocking (STARVATION) of low-priority processes is its main problem; AGING (gradually raising the priority of a waiting process) solves it. SJF is priority scheduling where priority = 1 / next burst.
- Priority inversion: a low-priority process holding a lock needed by a high-priority process while a medium-priority process runs. Solved by priority inheritance.
- HRRN (highest response ratio next) is non-preemptive and avoids starvation: response ratio = (waiting time + burst time) / burst time; the highest ratio runs next.
- Predicting the next burst by EXPONENTIAL AVERAGING: tau(n+1) = alpha × t(n) + (1 - alpha) × tau(n), with t(n) the length of the nth actual burst and 0 ≤ alpha ≤ 1. alpha = 0 ignores history of recent bursts; alpha = 1 uses only the last burst.
- Multilevel queue: the ready queue is split into separate queues (foreground interactive with RR, background batch with FCFS) with fixed priority between queues. Multilevel feedback queue lets processes move between queues, which implements aging and favours short/I-O-bound jobs.
- SRTF bookkeeping: re-evaluate ONLY at arrivals and completions. At an arrival compare the new burst with the REMAINING time of the running process (not its original burst); preempt only if it is strictly smaller (on a tie, keep the running process).
- With all arrival times equal to 0, non-preemptive SJF simply sorts by burst time and SRTF gives the same schedule; the average waiting time is then the minimum possible, = (sum of all earlier completion times) / n.
- Non-preemptive priority and SJF can differ sharply from preemptive versions: a long job started just before a short one arrives cannot be stopped in the non-preemptive case, so its waiting-time penalty falls on every later arrival.
Formulas to remember
-
Response ratio (HRRN) = (W + S) / S (W = waiting time, S = service/burst time) -
tau(n+1) = alpha × t(n) + (1 - alpha) × tau(n)
| Algorithm | Preemptive? | Starvation? | Notes |
|---|---|---|---|
| FCFS | No | No | convoy effect |
| SJF | No | Yes (long jobs) | optimal avg WT among non-preemptive |
| SRTF | Yes | Yes (long jobs) | optimal avg WT overall |
| Priority | Either | Yes (low priority) | aging removes starvation |
| Round Robin | Yes | No | time quantum decides behaviour |
| HRRN | No | No | ratio grows while a job waits |
Worked example
Problem. Processes (AT, BT): P1(0,8), P2(1,4), P3(2,9), P4(3,5). Find the average waiting time under FCFS, non-preemptive SJF and SRTF.
Solution. FCFS: P1 0-8, P2 8-12, P3 12-21, P4 21-26 → WT = 0, 7, 10, 18 → average 35/4 = 8.75. SJF: P1 0-8, P2 8-12, P4 12-17, P3 17-26 → WT P1 0, P2 7, P4 9, P3 15 → average 31/4 = 7.75. SRTF: P1 runs 0-1; P2 (4 < 7) preempts and runs 1-5; P4 5-10; P1 10-17; P3 17-26 → WT P1 9, P2 0, P3 15, P4 2 → average 26/4 = 6.5.
Worked comparison: FCFS vs SJF vs SRTF (Gantt-derived table)
- Workload: P1 (AT 0, BT 7), P2 (AT 2, BT 4), P3 (AT 4, BT 1), P4 (AT 5, BT 4). Total burst = 16, no idle time, so every algorithm finishes at t = 16.
- FCFS order P1, P2, P3, P4: CT = 7, 11, 12, 16. SJF (non-preemptive): at t = 7 the ready set is {P2 (4), P3 (1), P4 (4)}, so P3 runs, then P2 (earlier arrival breaks the tie with P4), then P4: CT = 7, 12, 8, 16.
- SRTF timeline: P1 0-2 (5 left); P2 arrives with 4 < 5 and preempts: P2 2-4 (2 left); P3 arrives with 1 < 2 and preempts: P3 4-5; P2 resumes 5-7 (P4 arrives at 5 with 4 > 2); P4 7-11; P1 11-16. CT = 16, 7, 5, 11.
- Cross-check with TAT = WT + BT: FCFS average TAT 8.75 = 4.75 + 4; SJF 8 = 4 + 4; SRTF 7 = 3 + 4. The average burst here is 16 / 4 = 4.
- Response time: FCFS and SJF are non-preemptive so RT = WT (averages 4.75 and 4). SRTF first-run times are 0, 2, 4, 7 → RT = 0, 0, 0, 2, average 0.5, much smaller than its average WT of 3.
- SRTF preempts twice here (P1 by P2 at t = 2, P2 by P3 at t = 4) so there are 5 context switches in 6 slices; FCFS has 3 switches and SJF has 3.
| Process | AT | BT | FCFS WT | SJF WT | SRTF WT |
|---|---|---|---|---|---|
| P1 | 0 | 7 | 0 | 0 | 9 |
| P2 | 2 | 4 | 5 | 6 | 1 |
| P3 | 4 | 1 | 7 | 3 | 0 |
| P4 | 5 | 4 | 7 | 7 | 2 |
| Average WT | 4.75 | 4 | 3 | ||
| Average TAT | 8.75 | 8 | 7 |
Worked example
Problem. For the workload above, which algorithm gives P3 the smallest waiting time and what is P3’s waiting time under each algorithm? Also find the CPU utilization.
Solution. P3 (the shortest job, 1 ms) waits 7 under FCFS (it queues behind P1 and P2), 3 under non-preemptive SJF (it must wait for P1 to finish at t = 7, arrival at 4) and 0 under SRTF (it preempts P2 on arrival). CPU utilization is 100% in all three because the total burst 16 equals the finishing time 16 and the CPU never idles.
Round Robin and worked Gantt charts
- ROUND ROBIN (RR): preemptive, designed for time sharing. Each process gets one time quantum q; if it does not finish it goes to the TAIL of the ready queue. If there are n processes and quantum q, no process waits more than (n - 1) × q time units for its next turn.
- If q is very large, RR becomes FCFS. If q is very small, RR approaches processor sharing but context-switch overhead dominates. A common rule: about 80% of CPU bursts should be shorter than q.
- Queue-order rule when arrival coincides with a quantum expiry: assume the NEWLY ARRIVED process enters the queue BEFORE the preempted process, unless the question states otherwise.
- For RR with all processes arriving at time 0, WT of a process = (completion time) - (burst time). RR usually has higher average turnaround than SJF but a better (smaller) response time.
- Number of context switches in RR with burst B and quantum q for one process alone = ceil(B / q) - 1 preemptions; for several processes count the quantum slices and subtract one for the final completion.
- Steps for any numerical: (1) write AT/BT table; (2) draw the Gantt chart with time stamps; (3) read CT of each process; (4) compute TAT = CT - AT, WT = TAT - BT; (5) average.
- Preemptive priority: whenever a higher-priority process arrives, it preempts; at equal priority the earlier arrival (or lower number) wins. With ties, state the rule used.
- Idle time: if no process is ready, advance the clock to the next arrival; do not count the idle time in any waiting time.
- Worked RR trace (q = 3): P1(0,5), P2(1,3), P3(2,8), P4(3,2). Queue after t = 3 is P2, P3, P4, P1 (P4 arrives at exactly t = 3 and goes before the preempted P1). Gantt: P1 0-3, P2 3-6, P3 6-9, P4 9-11, P1 11-13, P3 13-18. CT = 13, 6, 18, 11; WT = CT - AT - BT = 8, 2, 8, 6, average 6; TAT average = 10.5; response times 0, 2, 4, 6.
- When one process is alone in the ready queue it simply keeps the CPU (P3 above runs 13-18): no context switch is charged for renewing the quantum.
- Preemptive priority trace: P1(AT 0, BT 4, prio 3), P2(1, 3, prio 1), P3(2, 2, prio 2), smaller number = higher priority. P1 runs 0-1; P2 preempts and runs 1-4; P3 (prio 2) beats P1 (prio 3) at t = 4 and runs 4-6; P1 finishes 6-9. WT = 5, 0, 2 → average 7/3 ≈ 2.33.
- RR vs SJF: for equal-length processes all arriving together RR gives the WORST average turnaround time (everyone finishes near the end), although its response time is the best. For jobs of different length it is usually fairer than FCFS.
Worked example
Problem. Round Robin with q = 4: P1 (BT 24), P2 (BT 3), P3 (BT 3), all arrive at t = 0. Find the average waiting time.
Solution. Gantt: P1 0-4, P2 4-7, P3 7-10, P1 10-14, 14-18, 18-22, 22-26, 26-30. CT: P1 = 30, P2 = 7, P3 = 10. WT = CT - BT: P1 = 6, P2 = 4, P3 = 7 → average 17/3 ≈ 5.67 ms.
HRRN, multilevel feedback queue and burst prediction
- HRRN (highest response ratio next) is non-preemptive. At every completion compute ratio = (W + S) / S for each ready process (W = time waited so far, S = burst) and run the largest. Short jobs start with ratio close to 1 + W/S which grows quickly; long jobs also rise with waiting, so nothing starves.
- Worked HRRN: P1(0,3), P2(2,6), P3(4,4), P4(6,5), P5(8,2). P1 runs 0-3. At t = 3 only P2 is ready, so P2 runs 3-9. At t = 9 the ratios are P3 (5+4)/4 = 2.25, P4 (3+5)/5 = 1.6, P5 (1+2)/2 = 1.5 → P3 runs 9-13. At t = 13: P4 (7+5)/5 = 2.4, P5 (5+2)/2 = 3.5 → P5 runs 13-15, then P4 15-20.
- MULTILEVEL FEEDBACK QUEUE (MLFQ) is defined by: number of queues, algorithm per queue, rule to promote and to demote, and rule for the entry queue. Typical: Q0 RR q = 8, Q1 RR q = 16, Q2 FCFS; a new job enters Q0, is demoted if it uses its full quantum, and may be promoted after long waits (aging).
- MLFQ trace: a CPU-bound job with a 30 ms burst runs 8 ms in Q0, is demoted, runs 16 ms in Q1, is demoted again and finishes its last 6 ms in Q2. A job with a burst of 5 ms finishes inside Q0. This is why MLFQ approximates SJF without knowing burst lengths.
- Exponential averaging example: tau0 = 10, alpha = 1/2 and actual bursts 6, 4, 6, 4: tau1 = 0.5×6 + 0.5×10 = 8; tau2 = 0.5×4 + 0.5×8 = 6; tau3 = 0.5×6 + 0.5×6 = 6; tau4 = 0.5×4 + 0.5×6 = 5. The predicted next burst is 5 ms. Expanding the recurrence, old bursts get weights alpha(1 - alpha)^k that decay geometrically.
- Real-time scheduling: rate-monotonic (static priority = 1 / period) is optimal among static-priority schemes and schedulable if utilization ≤ n(2^(1/n) - 1) (about 69% for large n); earliest-deadline-first (EDF) uses dynamic priorities and can reach 100% utilization.
- Priority inversion example: low L holds a lock, high H blocks on it, medium M preempts L; H effectively waits behind M. Priority inheritance gives L the priority of H until it releases the lock.
| Time | Ready (ratio) | Chosen |
|---|---|---|
| 3 | P2 (1.17) | P2 (3-9) |
| 9 | P3 2.25, P4 1.6, P5 1.5 | P3 (9-13) |
| 13 | P4 2.4, P5 3.5 | P5 (13-15) |
| 15 | P4 | P4 (15-20) |
Worked example
Problem. Using the HRRN trace above (P1(0,3), P2(2,6), P3(4,4), P4(6,5), P5(8,2)), find the average waiting time.
Solution. Start times: P1 0, P2 3, P3 9, P5 13, P4 15. WT = start - AT (non-preemptive): P1 0, P2 3 - 2 = 1, P3 9 - 4 = 5, P4 15 - 6 = 9, P5 13 - 8 = 5. Total = 20, average = 20 / 5 = 4 time units.
Exam tips
- Write TAT = CT - AT and WT = TAT - BT on top of every numerical and always draw the Gantt chart with times.
- Non-preemptive: RT = WT. SRTF/RR: RT may be smaller than WT.
- SJF is optimal only for AVERAGE WAITING TIME; check whether arrivals are all at 0 or staggered.
- In RR, new arrivals enter the queue before the preempted process when times coincide.
- Starvation: SJF, SRTF and Priority. No starvation: FCFS, RR, HRRN. Aging fixes priority starvation.
8 GATE-style MCQs for CPU Scheduling with full explanations. Attempt each question before revealing the answer.
Three processes P1, P2, P3 with burst times 24, 3 and 3 ms arrive at time 0 in this order. Under FCFS, the average waiting time is:
Show answer & explanation
Answer: C
P1 starts at 0, P2 at 24, P3 at 27. WT = 0, 24, 27 → average 51/3 = 17 ms.
Four processes arrive at time 0 with burst times 6, 8, 7 and 3 ms. The average waiting time under non-preemptive SJF is:
Show answer & explanation
Answer: B
Order of execution 3, 6, 7, 8. Waiting times: 0, 3, 9, 16 → total 28, average 7 ms.
Processes (arrival, burst): P1(0,8), P2(1,4), P3(2,9), P4(3,5). The average waiting time under SRTF is:
Show answer & explanation
Answer: B
Schedule: P1 0-1, P2 1-5, P4 5-10, P1 10-17, P3 17-26. WT: P1 9, P2 0, P3 15, P4 2 → 26/4 = 6.5 ms.
Round Robin with time quantum 2 ms. Processes P1(arrival 0, burst 5), P2(1, 3), P3(3, 1). A newly arrived process joins the queue before a preempted one. P3 completes at time:
Show answer & explanation
Answer: C
Gantt: P1 0-2, P2 2-4, P1 4-6, P3 6-7, P2 7-8, P1 8-9. At t = 4 the queue is P1, P3, P2 (P3 arrived at 3, P2 re-queued at 4). So P3 finishes at 7.
Non-preemptive priority scheduling (smaller number = higher priority), all arriving at time 0: P1(burst 10, priority 3), P2(1, 1), P3(2, 4), P4(1, 5), P5(5, 2). The average waiting time is:
Show answer & explanation
Answer: C
Order P2, P5, P1, P3, P4 starting at 0, 1, 6, 16, 18. Total waiting 0 + 1 + 6 + 16 + 18 = 41; average 41/5 = 8.2 ms.
The convoy effect, in which many short processes wait behind a long CPU-bound process, is characteristic of:
Show answer & explanation
Answer: B
FCFS is non-preemptive and ordered by arrival, so a long job at the head delays everything behind it.
Which of the following scheduling algorithms can cause starvation?
Show answer & explanation
Answer: C
If short jobs keep arriving, a long job never gets the CPU under SRTF/SJF. FCFS and RR serve every process in finite time.
A predictor uses exponential averaging with alpha = 1/2 and initial estimate tau0 = 10 ms. The actual bursts observed are 6 ms and then 4 ms. The next predicted burst is:
Show answer & explanation
Answer: C
tau1 = 0.5 × 6 + 0.5 × 10 = 8; tau2 = 0.5 × 4 + 0.5 × 8 = 6 ms.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
A process arrives at 3, has burst 6 and completes at 20. Its waiting time is:
Show answer & explanation
Answer: B) 11
TAT = 20 - 3 = 17; WT = 17 - 6 = 11.
P1 (burst 5) and P2 (burst 4) both arrive at 0 and Round Robin with quantum 2 is used. The average waiting time is:
Show answer & explanation
Answer: B) 4
Gantt: P1 0-2, P2 2-4, P1 4-6, P2 6-8, P1 8-9. CT: P1 = 9, P2 = 8. WT = 9 - 5 = 4 and 8 - 4 = 4 → average 4.
Three processes, each with burst 4 and arrival 0, run under Round Robin with q = 2. The number of context switches (excluding the first dispatch and the final completion) is:
Show answer & explanation
Answer: C) 5
Slices: A B C A B C = 6 slices, so 5 switches between consecutive slices.
P1(0,7), P2(2,4), P3(4,1), P4(5,4). The average waiting time under non-preemptive SJF is:
Show answer & explanation
Answer: B) 4
P1 0-7, then at 7 the shortest is P3 (7-8), then P2 (8-12) and P4 (12-16). WT: P1 0, P2 6, P3 3, P4 7 → 16/4 = 4.
For the same set P1(0,7), P2(2,4), P3(4,1), P4(5,4), the average waiting time under SRTF is:
Show answer & explanation
Answer: A) 3
P1 0-2, P2 2-4, P3 4-5, P2 5-7, P4 7-11, P1 11-16. WT: P1 9, P2 1, P3 0, P4 2 → 12/4 = 3.
The technique of gradually increasing the priority of a process that waits in the system for a long time is called:
Show answer & explanation
Answer: B) aging
Aging prevents indefinite blocking (starvation) in priority scheduling.
In HRRN, a process has waited 6 ms and needs a burst of 2 ms. Its response ratio is:
Show answer & explanation
Answer: B) 4
Response ratio = (W + S) / S = (6 + 2) / 2 = 4.
In which of the following algorithms is the response time of a process always equal to its waiting time?
Show answer & explanation
Answer: C) FCFS
In a non-preemptive algorithm like FCFS the process runs to completion once it gets the CPU, so the wait before the first run equals the total wait.
Topic FAQs
Which scheduling algorithm gives the minimum average waiting time?
SRTF (preemptive SJF) is optimal for average waiting time overall; non-preemptive SJF is optimal among non-preemptive algorithms when all processes are available. Both need burst times, which are only estimated in practice.
What is the convoy effect?
In FCFS, short processes queue up behind a long CPU-bound process, so the CPU is busy with one job while I/O devices idle. Average waiting time rises sharply.
How does the choice of time quantum affect Round Robin?
A very large quantum makes RR behave like FCFS; a very small one gives better response but wastes time on context switches. Typical quantum is 10–100 ms and larger than most CPU bursts.
How do I handle ties in GATE scheduling questions?
Use the rule stated in the question. If none is given, prefer the process that arrived earlier (or has the lower process number). In RR, a process arriving exactly when a quantum ends is placed before the preempted process.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.