Computer Science and IT‹›⌂ 0:00 studied

Module 9 · 14 min read · Free chapter

CPU scheduling

In short

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.

What are you preparing for? We show the practice and previous year questions of your exam.

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: TAT=CT−AT\mathit{TAT} = \mathit{CT} - \mathit{AT} and WT=TAT−BT\mathit{WT} = \mathit{TAT} - \mathit{BT}, 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 8−0=88 - 0 = 8, 12−1=1112 - 1 = 11, 21−2=1921 - 2 = 19 and 26−3=2326 - 3 = 23, so the average is 614=15.25\dfrac{61}{4} = 15.25. Waiting times are 8−8=08 - 8 = 0, 11−4=711 - 4 = 7, 19−9=1019 - 9 = 10 and 23−5=1823 - 5 = 18, so the average waiting time is 354=8.75\dfrac{35}{4} = 8.75.

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 t=0t = 0 only P1 has arrived, so it runs to 8. At t=8t = 8, 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: 00, P2: 12−1−4=712 - 1 - 4 = 7, P4: 17−3−5=917 - 3 - 5 = 9 and P3: 26−2−9=1526 - 2 - 9 = 15. The average is 314=7.75\dfrac{31}{4} = 7.75, 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 t=1t = 1, 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: 17−0−8=917 - 0 - 8 = 9, P2: 5−1−4=05 - 1 - 4 = 0, P3: 26−2−9=1526 - 2 - 9 = 15 and P4: 10−3−5=210 - 3 - 5 = 2. The average is 264=6.5\dfrac{26}{4} = 6.5, 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, τn+1=αtn+(1−α)τn\tau_{n+1} = \alpha t_n + (1 - \alpha)\tau_n, where tnt_n is the last actual burst and τn\tau_n the last prediction. With α=0.5\alpha = 0.5, τ0=10\tau_0 = 10 and actual bursts 6 and 4, the predictions are τ1=0.5×6+0.5×10=8\tau_1 = 0.5 \times 6 + 0.5 \times 10 = 8 and τ2=0.5×4+0.5×8=6\tau_2 = 0.5 \times 4 + 0.5 \times 8 = 6.

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 t=0t = 0; 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 254=6.25\dfrac{25}{4} = 6.25. 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, q=4q = 4). 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: 20−8=1220 - 8 = 12, P2: 8−1−4=38 - 1 - 4 = 3, P3: 26−2−9=1526 - 2 - 9 = 15 and P4: 25−3−5=1725 - 3 - 5 = 17, so the average is 474=11.75\dfrac{47}{4} = 11.75. That is the worst of our methods, yet the average response time is 0+3+6+94=4.5\dfrac{0 + 3 + 6 + 9}{4} = 4.5, 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, q=4q = 4 11.75 yes

Choosing q. With n ready processes, no process waits more than (n−1)⋅q(n - 1) \cdot q for its next turn; with 5 processes and q=20q = 20 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 q=1q = 1 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:

  1. Define the criteria and the formulas for turnaround, waiting and response time.
  2. Solve a process table by several methods with Gantt charts and compare the averages.
  3. Explain a problem and its cure, such as the convoy effect, starvation and aging, or the choice of quantum.
  4. 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).

  1. Define throughput, turnaround time, waiting time and response time. Why does round robin improve response time but often worsen average waiting time? (10 marks)
  2. 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 q=2q = 2 ms and compare the average waiting times. (15 marks)
  3. 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

  • TAT=CT−AT\mathit{TAT} = \mathit{CT} - \mathit{AT}, WT=TAT−BT\mathit{WT} = \mathit{TAT} - \mathit{BT}, and response time is the time of the first run minus AT\mathit{AT}.
  • 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 τn+1=αtn+(1−α)τn\tau_{n+1} = \alpha t_n + (1 - \alpha)\tau_n.
  • Priority scheduling and SJF can starve processes, and aging is the cure.
  • Round robin bounds each wait by (n−1)⋅q(n - 1) \cdot q 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 q=4q = 4.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 14 answered

  1. Waiting time of a process equals:

  2. A process arrives at 2, first runs at 5 and completes at 14 with burst 6. Its turnaround and waiting times are:

  3. Which algorithm is always non-preemptive?

  4. Short processes waiting behind one long CPU-bound process is called:

  5. Which algorithm gives the minimum average waiting time for a given set of processes?

  6. With α=0.5\alpha = 0.5, previous prediction 10 and actual burst 6, the next predicted burst is:

  7. Aging in priority scheduling is used to prevent:

  8. All arrive at 0; smaller number = higher priority. A(6, p2), B(3, p1), C(8, p4), D(4, p3). Average waiting time (non-preemptive) is:

  9. If the time quantum of round robin is made very large, RR behaves like:

  10. With 6 ready processes and q=25q = 25 ms (switch time ignored), the longest a process waits for its next turn is:

  11. In a multilevel feedback queue, a process that uses its full quantum in the top queue:

  12. P1–P4 arrive at 0, 1, 2, 3 with bursts 8, 4, 9, 5. Average waiting time under SRTF is:

  13. For the same four processes, FCFS average waiting time is:

  14. Why do schedulers often favour I/O-bound processes on the CPU?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Which scheduling algorithm gives the minimum average waiting time?

Shortest remaining time first is optimal overall, and shortest job first is optimal among non-preemptive methods.

What is the convoy effect?

In FCFS, many short processes wait behind one long CPU-bound process, which raises the average waiting time and leaves devices idle.

What happens if the round robin quantum is too large or too small?

A very large quantum makes round robin behave like FCFS, and a very small one wastes CPU time in context switches.

How does aging prevent starvation?

It raises the priority of a process the longer it waits, so even the lowest-priority process eventually reaches the top and runs.

More in this module: Operating systems

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Operating Systems (IIT Madras, 106106144): CPU scheduling