4 Cpu Scheduling

Operating Systems ยท Unit 4

CPU Scheduling

Exam-focused notes for CPU Scheduling (Operating Systems, BIT204): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.

CPU Scheduling in short

CPU scheduling decides which ready process gets the CPU next. Good scheduling keeps the CPU busy and gives users fast responses. This chapter covers the main algorithms and how to calculate their times.

CPU Scheduling: notes

Scheduling objectives and criteria

Goals of a scheduler:

  • Keep the CPU as busy as possible.
  • Give every process a fair share.
  • Finish as many jobs as possible in a given time.
  • Give short response times to users.

Criteria used to compare algorithms:

  • CPU utilization: the share of time the CPU is busy. Higher is better.
  • Throughput: the number of processes completed per unit time.
  • Turnaround time: the time from arrival to completion.
  • Waiting time: the total time spent in the ready queue.
  • Response time: the time from arrival to the first response.

A scheduler is preemptive if it can take the CPU from a running process. It is non-preemptive if the process keeps the CPU until it finishes or blocks.

The key formulas are shown below.

$$\text{Turnaround time} = \text{Completion time} - \text{Arrival time}$$

$$\text{Waiting time} = \text{Turnaround time} - \text{Burst time}$$

FIFO scheduling algorithm

First In First Out (also First Come First Served) runs processes in the order they arrive. It is non-preemptive and uses a simple queue.

Example: P1, P2 and P3 arrive at time 0 with burst times 24, 3 and 3.

$$\begin{aligned} \text{Order} &: P1 \to P2 \to P3 \ \text{Completion} &: 24,\ 27,\ 30 \ \text{Waiting} &: 0,\ 24,\ 27 \ \text{Average waiting} &= \frac{0+24+27}{3} = 17 \end{aligned}$$

If the order is P2, P3, P1, the waiting times are 0, 3 and 6, so the average is 3. This large change shows the convoy effect: short jobs wait behind one long job.

What this unit covers

  • Scheduling objectives and criteria
  • FIFO scheduling algorithm
  • Shortest Job First scheduling
  • Priority scheduling
  • Round Robin scheduling
  • Turnaround time calculation
  • Waiting time calculation
  • Multiple queue scheduling

Turnaround time calculation

2080 ยท 10 marks

CPU Scheduling Analysis

Find Average waiting time and turn-around time for the following example using FIFO, SRTF and Round Robin scheduling algorithm. Assume quantum as 4 ms.

Process idArrival timeBurst time (ms)
P108
P215
P3110
P4213
P5217

[10]

Process Arrival Time Burst Time ---------------------------------- P1 0 8 P2 1 5 P3 1 10 P4 2 13 P5 2 17 Quantum $q = 4$ ms. Formulas: TAT = CT - AT, WT = TAT - BT. --- Order by arrival: P1 โ†’ P2 โ†’ P3 โ†’ P4 โ†’ P5. Gantt: P1[0-8] P2[8-13] P3[13-23] P4[23-36] P5[36-53] P AT BT CT TAT WT --------------...

Open this answer on its own page โ†’

Round Robin scheduling

2078 ยท 5 marks

CPU Scheduling Analysis

Consider the following set of processes, with the length of CPU burst time given in milliseconds. The processes are assumed to have arrived in the order A, B, C and D all at time 0. What is the turnaround time and waiting time for the scheduling algorithms, RR (quantum = 3) and Priority Algorithm.

ProcessCPU BurstPriority
A83
B91 (Lowest)
C102
D64 (Highest)

[5]

Show solved answer

CPU Scheduling: RR (q=3) and Priority

Given

ProcessCPU BurstPriority
A83
B91 (Lowest)
C102
D64 (Highest)

All arrive at time 0, in order A, B, C, D.

Formulas: $TAT = CT - AT$, $WT = TAT - Burst$.


Solution

1. Round Robin (Quantum = 3)

Ready queue starts: A, B, C, D. A process that still has remaining burst rejoins the tail of the queue after its slice.

Tracing execution (remaining bursts A=8, B=9, C=10, D=6):

SlotProcRangeRemaining after
1A0-3A=5
2B3-6B=6
3C6-9C=7
4D9-12D=3
5A12-15A=2
6B15-18B=3
7C18-21C=4
8D21-24D=0 โœ“
9A24-26A=0 โœ“
10B26-29B=0 โœ“
11C29-32C=1
12C32-33C=0 โœ“

Gantt:

| A | B | C | D | A | B | C | D | A | B | C | C |
0   3   6   9  12  15  18  21 24  26  29  32  33
ProcessBurstCTTATWT
A8262618
B9292920
C10333323
D6242418

$$\text{Avg TAT} = \frac{26+29+33+24}{4} = \frac{112}{4} = 28 \text{ ms}$$ $$\text{Avg WT} = \frac{18+20+23+18}{4} = \frac{79}{4} = 19.75 \text{ ms}$$

2. Priority Scheduling (Non-Preemptive)

Higher number = higher priority. Execution order: D (4) โ†’ A (3) โ†’ C (2) โ†’ B (1)

Gantt:

| D | A | C | B |
0   6  14  24  33
ProcessBurstCTTATWT
D6660
A814146
C10242414
B9333324

$$\text{Avg TAT} = \frac{6+14+24+33}{4} = \frac{77}{4} = 19.25 \text{ ms}$$ $$\text{Avg WT} = \frac{0+6+14+24}{4} = \frac{44}{4} = 11 \text{ ms}$$

Summary

AlgorithmAvg TATAvg WT
RR (q=3)28 ms19.75 ms
Priority19.25 ms11 ms
Open this answer on its own page โ†’

0 ยท 5 marks

CPU Scheduling Analysis

Consider the following set of processes, with the length of CPU burst time given in milliseconds.

ProcessesCPU BurstPriority
A83
B131 (Lowest)
C192
D34 (Highest)

The processes are assumed to have arrived in the order A, B, C and D all at time 0. What is the turnaround time and waiting time for the scheduling algorithms, RR (quantum = 5) and Priority Algorithm?

[5]

Show solved answer

CPU Scheduling: RR (quantum = 5) and Priority

Given

ProcessCPU Burst (ms)Priority
A83
B131 (Lowest)
C192
D34 (Highest)

Arrival: all at time 0, order A, B, C, D. Total burst = $8+13+19+3 = 43$ ms.


Solution

Part 1: Round Robin (q = 5)

Ready queue starts A, B, C, D. Track remaining bursts.

SlotProcStartEndRemaining after
1A053
2B5108
3C101514
4D15180 (needs 3)
5A18210 (needs 3)
6B21263
7C26319
8B31340 (needs 3)
9C34394
10C39430

Gantt:

| A | B | C | D | A | B | C | B | C | C |
0   5  10  15  18  21  26  31  34  39  43

Completion times: A = 21, B = 34, C = 43, D = 18

Turnaround (TAT = Completion โˆ’ 0):

ProcTATWT = TAT โˆ’ Burst
A2121 โˆ’ 8 = 13
B3434 โˆ’ 13 = 21
C4343 โˆ’ 19 = 24
D1818 โˆ’ 3 = 15

$$\text{Avg TAT} = \frac{21+34+43+18}{4} = \frac{116}{4} = 29 \text{ ms}$$

$$\text{Avg WT} = \frac{13+21+24+15}{4} = \frac{73}{4} = 18.25 \text{ ms}$$


Part 2: Priority Scheduling (Non-preemptive)

Highest priority number runs first: D(4) โ†’ A(3) โ†’ C(2) โ†’ B(1)

Gantt:

| D | A | C | B |
0   3   11  30  43
ProcCompletionTATWT = TAT โˆ’ Burst
D330
A11113
C303011
B434330

$$\text{Avg TAT} = \frac{3+11+30+43}{4} = \frac{87}{4} = 21.75 \text{ ms}$$

$$\text{Avg WT} = \frac{0+3+11+30}{4} = \frac{44}{4} = 11 \text{ ms}$$


Summary

AlgorithmAvg TATAvg WT
RR (q = 5)29 ms18.25 ms
Priority21.75 ms11 ms

All values confirmed correct.

Open this answer on its own page โ†’

Multiple queue scheduling

0 ยท 5 marks

Why do we need multiple queues in process scheduling? What is the significance of TLB? [5]

--- A single queue for all processes is inefficient because processes have different characteristics, priorities, and resource requirements. Multiple queues address this by organizing processes into separate categories. 1. Different Process Priorities - High-priority processes (system processes) ...

Open this answer on its own page โ†’