Module 9 · 14 min read · Free chapter
CPU scheduling
Turnaround time is completion time minus arrival time, and waiting time is turnaround minus burst. SRTF gives the lowest average waiting time, SJF is best among non-preemptive methods, and round robin gives the best response time. Aging stops starvation in priority scheduling, and a very large quantum turns round robin into FCFS.
Start from zero: one counter, many customers
Picture a ration shop with one counter. The first person in the queue has a list for the whole month, and it takes twenty minutes to serve him. Behind him stand three people who each want just one packet of salt, and they wait twenty minutes for a one-minute job.
The shopkeeper could serve the quick customers first, and the average waiting time would fall sharply. But then a customer with a long list might wait for ever if quick customers keep arriving. Or the shopkeeper could serve everyone for two minutes in turn, which feels fair but wastes time as he keeps switching between lists.
A CPU faces exactly these choices. It is the single counter, processes are the customers, and the CPU scheduler decides who is served next. In this chapter we learn the main scheduling methods and how to calculate waiting and turnaround times with Gantt charts, which is one of the most common GATE numericals.
Why it matters. The same hardware can feel fast or sluggish depending only on the scheduling rule. Your phone stays smooth while apps run in the background because the scheduler favours the app you are touching.
Scheduling criteria and the three times
A process alternates between CPU bursts, when it computes, and I/O bursts, when it waits for a device. The scheduler acts whenever the CPU becomes free or a new process arrives, and it judges a method by five criteria.
| Criterion | Meaning | Goal |
|---|---|---|
| CPU utilisation | fraction of time the CPU is busy | maximise |
| Throughput | processes completed per unit time | maximise |
| Turnaround time | completion time − arrival time | minimise |
| Waiting time | time spent in the ready queue = turnaround − burst | minimise |
| Response time | first run − arrival time | minimise |
Memorise the two formulas: and , where CT is completion time, AT arrival time and BT burst time. These two lines solve almost every Gantt-chart question. Response time matters most for interactive users, because it is how long you wait before the program first reacts.
A scheduler is non-preemptive if a process keeps the CPU until it ends or blocks for I/O. It is preemptive if the OS can take the CPU away, for example on a timer interrupt or when a more urgent process arrives. Preemption gives better response but needs care, because a process may be stopped in the middle of updating shared data.
The dispatcher hands the CPU to the chosen process: it switches context, switches to user mode and jumps to the right instruction. The time it takes is dispatch latency, and in numericals it is assumed to be zero unless the question gives it.
Why it matters. Different users want different things. A bank's overnight batch job wants high throughput, while a railway booking clerk wants a fast response, so no single method is best for everyone.
First-come first-served
FCFS is the simplest method: the process that arrives first is served first, and it runs until it finishes or blocks. The ready queue is a plain FIFO queue, and FCFS is non-preemptive.
We will use one set of four processes for the whole chapter, so that the methods can be compared fairly.
| Process | Arrival time | Burst time |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
Worked example 1 (FCFS). The CPU runs the processes in arrival order, which gives this Gantt chart.
| P1 | P2 | P3 | P4 |
0 8 12 21 26
Completion times are 8, 12, 21 and 26. Turnaround times are , , and , so the average is . Waiting times are , , and , so the average waiting time is .
FCFS suffers from the convoy effect. When one long CPU-bound process holds the CPU, many short I/O-bound processes pile up behind it like cars behind a slow truck, and the devices sit idle. Notice also that FCFS results depend heavily on order: if the short jobs had arrived first, the average wait would have been much lower.
Why it matters. FCFS is fair in a simple sense and easy to build, which is why it survives inside other methods, for example as the rule within a single priority level. Its weakness with mixed job lengths is the reason better methods exist.
Shortest job first and shortest remaining time first
Shortest job first (SJF) picks, from the processes that have arrived, the one with the smallest next CPU burst. Ties are broken by FCFS. In its non-preemptive form, once a job starts it runs to completion.
Worked example 2 (non-preemptive SJF). At only P1 has arrived, so it runs to 8. At , P2 (4), P3 (9) and P4 (5) are waiting, so P2 runs, then P4, then P3.
| P1 | P2 | P4 | P3 |
0 8 12 17 26
Waiting times are P1: , P2: , P4: and P3: . The average is , already better than FCFS.
Shortest remaining time first (SRTF) is the preemptive version. Whenever a new process arrives, the scheduler compares its burst with the remaining time of the running process and switches if the newcomer is shorter.
Worked example 3 (SRTF). P1 starts at 0. At , P2 arrives with burst 4 while P1 has 7 left, so P2 preempts P1. P2 finishes at 5; then P4 (5) beats P1 (7) and P3 (9), so P4 runs to 10, P1 runs to 17, and P3 runs to 26.
| P1 | P2 | P4 | P1 | P3 |
0 1 5 10 17 26
Waiting times are P1: , P2: , P3: and P4: . The average is , the lowest of all methods for this data. SRTF is provably optimal for average waiting time, and SJF is optimal among non-preemptive methods.
There is a catch: the OS does not know the next burst in advance. It predicts it by exponential averaging, , where is the last actual burst and the last prediction. With , and actual bursts 6 and 4, the predictions are and .
Why it matters. SJF shows what the best possible average looks like, so it is the yardstick for every other method. Its need to predict the future and its risk of starving long jobs explain why real systems use approximations.
Priority scheduling and aging
In priority scheduling each process carries a number, and the ready process with the highest priority runs first. Many systems treat a smaller number as higher priority, but questions always state the convention, so read it carefully. Priority can be preemptive, where a newly arrived higher-priority process takes the CPU at once, or non-preemptive.
Worked example 4. Four processes all arrive at ; a smaller number means higher priority. A has burst 6 and priority 2, B has burst 3 and priority 1, C has burst 8 and priority 4, and D has burst 4 and priority 3.
| B | A | D | C |
0 3 9 13 21
Waiting times are B = 0, A = 3, D = 9 and C = 13, so the average is . Since all arrive together, waiting time is simply the start time of each process.
The danger is starvation (indefinite blocking). If high-priority processes keep arriving, a low-priority process may never run. The cure is aging: the scheduler raises the priority of a process step by step the longer it waits, so in the end even the lowest process reaches the top and runs.
SJF is a special case of priority scheduling in which the priority is the predicted next burst. That is why SJF shares the starvation problem.
Why it matters. Real systems mix urgent and routine work, such as a mouse click and a background backup. Priorities express that urgency, and aging keeps the system fair.
Round robin and choosing the quantum
Round robin (RR) is FCFS with a timer. Each process gets at most one time quantum q, say 10 to 100 ms, and then goes to the tail of the ready queue if it still has work. A process that finishes or blocks before its quantum ends gives up the CPU at once.
Worked example 5 (RR, ). Use the same four processes. By convention, a process arriving at the same instant as a preempted process is queued ahead of it.
| P1 | P2 | P3 | P4 | P1 | P3 | P4| P3|
0 4 8 12 16 20 24 25 26
P2 finishes at 8, P1 at 20, P4 at 25 and P3 at 26. Waiting times are P1: , P2: , P3: and P4: , so the average is . That is the worst of our methods, yet the average response time is , far better than the 8.75 of FCFS.
| Method | Average waiting time | Preemptive |
|---|---|---|
| FCFS | 8.75 | no |
| SJF | 7.75 | no |
| SRTF | 6.5 | yes |
| RR, | 11.75 | yes |
Choosing q. With n ready processes, no process waits more than for its next turn; with 5 processes and ms, that is 80 ms. If q is very large, every process finishes in one turn and RR becomes FCFS. If q is tiny, the CPU spends most of its time on context switches: with a 1 ms switch and ms, half the CPU is wasted.
A useful rule is to pick q so that about 80% of CPU bursts are shorter than q. Then most interactive bursts finish in one turn and only long jobs rotate.
Why it matters. Round robin is the basis of every time-sharing system, from a college server to your laptop. The quantum is the dial that trades responsiveness against overhead.
Multilevel queues and a note on I/O scheduling
Real workloads mix very different processes, so one queue is not enough. A multilevel queue splits the ready queue into several queues, for example system processes, interactive processes and batch jobs. Each queue has its own algorithm, such as RR for interactive and FCFS for batch, and the queues have fixed priority between them.
In a plain multilevel queue a process stays in one queue for life. That is simple but rigid, and lower queues can starve if upper queues are always busy. A fix is to give each queue a share of CPU time, for example 80% to interactive and 20% to batch.
A multilevel feedback queue (MLFQ) lets processes move. A typical design has Q0 with quantum 8 ms, Q1 with quantum 16 ms and Q2 run by FCFS. A new process enters Q0; if it uses its whole quantum it drops to Q1, and if it again uses its whole quantum it drops to Q2. A process that waits too long in a low queue can be moved up, which is aging.
The effect is clever. Short interactive bursts finish in Q0 and stay at high priority, while long CPU-bound jobs sink to the bottom without anyone having to label them. An MLFQ is defined by the number of queues, the algorithm and quantum of each, and the rules for moving up and down.
A note on I/O scheduling. The OS also schedules requests waiting for each device. For a disk, the order of requests decides how far the head must move, so methods such as shortest seek time first and the elevator method cut total seek time; disk scheduling works these out with numbers. On the CPU side, favouring I/O-bound processes keeps devices busy, because such a process uses the CPU briefly and then starts its next I/O.
Why it matters. MLFQ is the most general scheduler and is the idea behind the schedulers of most desktop systems. It shows how an OS can learn a process's behaviour instead of being told.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about scheduling in four ways:
- Define the criteria and the formulas for turnaround, waiting and response time.
- Solve a process table by several methods with Gantt charts and compare the averages.
- Explain a problem and its cure, such as the convoy effect, starvation and aging, or the choice of quantum.
- Design a scheduler for a stated mix of jobs and justify it, usually leading to a multilevel feedback queue.
Practice questions (our own, not previous-year questions).
- Define throughput, turnaround time, waiting time and response time. Why does round robin improve response time but often worsen average waiting time? (10 marks)
- Five processes arrive at time 0 with bursts 7, 3, 1, 5 and 2 ms. Draw Gantt charts for FCFS, SJF and round robin with ms and compare the average waiting times. (15 marks)
- Explain the multilevel feedback queue and show how it favours interactive processes without knowing burst lengths in advance. (10 marks)
Answer plan for question 3.
- Begin by stating the problem that SJF needs burst lengths that the OS cannot know in advance.
- Describe the structure of several ready queues with rising quanta, such as 8 ms, 16 ms and a final FCFS queue.
- State the rule that a new process enters the top queue and drops one level each time it uses its whole quantum.
- Explain that interactive processes block for I/O before their quantum ends, so they stay in the top queue and get fast response.
- Explain that CPU-bound jobs sink to lower queues, where they run with large quanta and few context switches.
- Add that aging moves a long-waiting process up, which prevents starvation, and close with a labelled diagram of the queues.
Quick revision
- , , and response time is the time of the first run minus .
- Non-preemptive methods let a process keep the CPU, while preemptive methods can take it away.
- FCFS is simple but suffers the convoy effect behind long jobs.
- SJF is optimal for average waiting time among non-preemptive methods, and SRTF is optimal overall.
- Next bursts are predicted by .
- Priority scheduling and SJF can starve processes, and aging is the cure.
- Round robin bounds each wait by and becomes FCFS when q is very large.
- Pick q so that about 80% of bursts finish within one quantum.
- A multilevel feedback queue moves CPU-bound jobs down and keeps interactive jobs up.
- For the chapter's example the averages are 8.75 for FCFS, 7.75 for SJF, 6.5 for SRTF and 11.75 for RR with .