Skip to content

CPU Scheduling

Made from the PDF Notes

Process Scheduling Algorithm : A way of Selecting an algorithm from ready queue & putting it on the CPU

RAM UniProcessor
***** *****
** ** ** **
* READY * ------Scheduling Algo.------> * CPU *
** ** ** **
***** *****
^
|
More Processes
p1, p2, .... pn

If a process is taken out from ready queue or RAM and is put in running process (queue/ CPU).

Preemptive Scheduling: then we can stop the process P1 and give another process p2, and send p1 to ready queue. (It provides responsiveness)

P2
(Ready) (Running)
P1

Non-preemptive Scheduling: then P1 would be processed till its, BT(burst time), after then only new process can be introduced.

P1
(Ready) (Running)
⤿
P2
PreemptiveNon-Preemptive
SRTF (Shortest Remaining time first)SJF (Shortest Job first)
LRTF (Shortest Remaining time first)LJF (Longest Job first)
Round RobinFCFS (First come first Serve)
Priority basedPriority Queue
Multilevel Queue
HRRN (High response ration next)

Arrival Time(AT) - The time at which process enters the ready Queue or stack Burst Time(BT) - Time Required by a process to is get executed on CPU Completion Time(CT) - The time at which Process completes its execution Turn Around Time (TAT), Waiting Time (WT), Response Time (RT)

TurnaroundTime=CompletionArrivalTime=BurstTime+WaitingTimeTurnaround Time = Completion - Arrival Time = Burst Time + Waiting Time WaitingTime=TurnaroundBurstTimeWaiting Time = Turn around - Burst Time ResponseTime=TheTimeAtWhichProcessGetCPUArrivalTimeResponse Time = The Time At Which Process Get CPU - Arrival Time

Criteria of Scheduling AT (Arrival Time) Dependent : FCFS BT (Burst Time) Dependent : SJF, LJF, SRTF, LRTF TQ (Time Quantum) Dependent : Round Robin

Arrival Time --- process execution duration --> Completion time
(poin of time) (point of time)

Mode : Non Preemptive Criteria : Arrival Time

FCFS is an OS Scheduling algorithm that automatically executes queued requests and process in order of their arrival.

  1. The process which requests the CPU first get the CPU allocation first
  2. Managed using FIFO queue

Example:

Process No.Arrival TimeBurst Time
P102
P212
P353
P464

Solution:

When CPU Assigned = max(AT, ACT of previous Process)Process No.ATBTCT = max(AT, CT of previous) + BTTAT = CT-ATWT=TAT-BTRT = when CPU Assigned - AT
0P1020+2 =22-2=02-2=00-0=0
2P2122+2 = 44-1=34-2=12-1=1
5P3535+3 = 88-5=33-3=05-5=0
8P4648+4 = 1212-6=66-428-6=2
Gantt Chart:
[P1P1|P2P2|X|P3P3P3|P4P4P4P4]
0 2 4 5 8 12
time ->

Note : X-> idle

Avg. TAT = 14/4 Avg. WAT = 3/4

Mode: Non-Preemptive Criteria - Burst Time

SJF is an algorithm in which process having the smallest execution time is choosen for next execution

  1. It can be preemptive (SJF) or non-preemptive (SRTF)
  2. It reduces the waiting time for other process
  3. It is a greedy Algo

Example:

Process No.Arrival TimeBurst Time
P113
P224
P312
P444
Solution:
  1. Upto T=0: No one Ready
  2. Upto T=1 : P1 & P3 Ready
  • Execution : P3 -> P1 (BT: P3<P1)
  1. Upto T= 2 : P1, P2 and P3 Ready
  • Execution: P3 -> P1 -> P3
  1. Upto T=4 : P1, P2, P3 and P4 Ready
  • Execution: P3 -> P1 -> P3 -> P4
Gantt Chart:
[X|P3P3|P1P1P1|P2P2P2P2|P4P4P4P4]
0 1 3 6 10 14
When CPU Assigned = max(AT, ACT of previous Process)Process No.ATBTCT = max(AT, CT of previous) + BTTAT = CT-ATWT=TAT-BTRT = when CPU Assigned - AT
1P3121+2 =33-1=22-2=01-1=0
3P1133+3 = 66-1=55-3=23-1=2
6P2246+4 = 1010-2=88-4=410-6=4
10P44410+4 = 1414-4=1010-4=610-4=6
Note: When burst time is same, see the one having less Arrival time, else can use process id.

Avg. TAT = 25/4 Avg. WAT = 12/4 = 3

SRTF = SJF + Preemptive

Mode: Preemptive Criteria - Burst Time

Example:

Process No.Arrival TimeBurst Time
P105
P213
P324
P441
Solution:
  1. Upto T = 0 : Only P1 Ready
  • Execution : P1
[P1]
0 1
BT[P1]--
BT[P1]=4
  1. Upto T = 1 : P1 & P2 Ready
  • Execution : P2 -> P1 (BT: P2<P1)
[P1|P2]
0 1 2
BT[P2]--
BT[P2]=2
  1. Upto T = 2 : P1, P2 and P3 Ready
  • Execution : P2 -> P1->P3 (BT: P2<P1=P3)
[P1|P2P2P2]
0 1 4
BT[P2]-=2
BT[P2]=0 X Done
  1. Upto T = 4 : P1, P3 and P4 Ready
  • Execution : P2 -> P1->P3 (BT: P4<P1=P3)
[P1|P2P2P3|P4]
0 1 4 5
BT[P4]--
BT[P4]=0 X Done
  1. Upto T = 5 : P1 and P3 Ready
  • Execution : P1->P3 (BT:P1=P3, BUT AT: P1<P3 ) Note: if BT same, AT will be prioritize
[P1|P2P2P3|P4|P1P1P1P1]
0 1 4 5 9
BT[P1]-=4
BT[P1]=0 X Done
  1. Upto T = 9 : P3 Ready
  • Execution : P3
[P1|P2P2P2|P4|P1P1P1P1|P3P3P3P3]
0 1 4 5 9 13
BT[P3]-=4
BT[P3]=0 X Done
When CPU Assigned= From Gantt DiagramProcess No.ATBTCT = From Gantt DiagramTAT = CT-ATWT=TAT-BTRT = when CPU Assigned - AT
0P105 4 099-0=99-5=40-0=0
1P213 044-1=33-3=01-1=0
9P324 01313-2=1111-0=119-2=7
4P441 055-4=11-1=04-4=0
Note: When burst time is same, see the one having less Arrival time, else can use process id.

Avg. TAT = 24/4 Avg. WAT = 15/4

Mode: Non-Preemptive Criteria - Burst Time

LRTF = LJF + Preemptive

Mode: Preemptive Criteria - Burst Time

Mode: Preemptive Criteria - Time Quantum

Round robin is

  • preemptive process scheduling algorithm
  • Each process is provided a fix time to execute, it is called quantum.
  • Once a process is executed for given time period, it is preempted and other process executes for a given time period
2
(Ready) (Running)

Example:

Process No.Arrival TimeBurst Time
P105
P213
P324
P441
TQ = 2

Solution:

  1. Upto T = 0 : Only P1 Ready
  • Queue (AT) : P1
  • Execution : P1 x TQ
[P1P2]
0 2
BT[P1]--2
BT[P1]=3
  1. Upto T = 2 : P2 and P3 Ready
  • Queue : P2 P3 P1 ( P1 push last of the queue)
[P1P1|P2P2]
0 2 4
BT[P2]-=2
BT[P2]=2
  1. Upto T = 4 : P1, P3 and P4 Ready,
  • Queue : P3 P1 P4 P2 (P2 push last of the queue)
[P1P1|P2P2|P3P3]
0 2 4 6
BT[P3]-=2
BT[P3]=0 X Done
  1. Upto T = 6 : P1 and P4 Ready,
  • Queue : P1 P4 P2 (P3 Completed)
[P1P1|P2P2|P3P3|P1P1]
0 2 4 6 8
BT[P1]-=2
BT[P1]=1
  1. Upto T = 8 : P4 and P2 Ready,
  • Queue : P4 P2 P1 (P1 Pushed at end of the queue)
[P1P1|P2P2|P3P3|P1P1|P4]
0 2 4 6 8 9
BT[P4]-=1 ( because only BT of 1 is remaining)
BT[P4]=0
  1. Upto T = 9 : P2 and P1 Ready,
  • Queue : P2 P1 (P4 Completed)
[P1P1|P2P2|P3P3|P1P1|P4|P2P2]
0 2 4 6 8 9 11
BT[P2]-=2
BT[P2]=0
  1. Upto T = 11 : P1 Ready,
  • Queue : P1 (P2 Completed)
[P1P1|P2P2|P3P3|P1P1|P4|P2P2|P1]
0 2 4 6 8 9 11 12
BT[P1]-=1
BT[P1]=0

All Completed

[P1P1|P2P2|P3P3|P1P1|P4|P2P2|P1]
0 2 4 6 8 9 11 12
When CPU Assigned = From Gantt DiagramProcess No.ATBTCT = From Gantt DiagramTAT = CT-ATWT=TAT-BTRT = when CPU Assigned - AT
0P105 3 1 01212-0=1212-5=71-1=0
2P214 2 01111-1=1010-4=62-1=1
4P322 066-2=44-2=24-2=2
8P441 099-4=55-1=48-4=4
Avg. TAT = 31/4
Avg. WT = 19/4