0% found this document useful (0 votes)
14 views49 pages

4 Process Cpu Scheduling

The document provides an overview of CPU scheduling, including basic concepts, scheduling criteria, and various scheduling algorithms such as FCFS, SJF, SRTF, Priority Scheduling, and Round Robin. It discusses the objectives of CPU scheduling, the role of the CPU scheduler and dispatcher, and the evaluation criteria for selecting scheduling algorithms. Additionally, it includes examples and calculations for average turnaround time and waiting time for different scheduling methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views49 pages

4 Process Cpu Scheduling

The document provides an overview of CPU scheduling, including basic concepts, scheduling criteria, and various scheduling algorithms such as FCFS, SJF, SRTF, Priority Scheduling, and Round Robin. It discusses the objectives of CPU scheduling, the role of the CPU scheduler and dispatcher, and the evaluation criteria for selecting scheduling algorithms. Additionally, it includes examples and calculations for average turnaround time and waiting time for different scheduling methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

CPU Scheduling

CPU Scheduling

 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
 FCFS
 SJF
 SRTF
 PRIORITY SCHEDULING
 ROUND ROBIN SCHEDULING
 MULTILEVEL QUEUE SCHEDULING
 MULTILEVEL FEEDBACK QUEUE SCHEDULING
Objectives

 To introduce CPU scheduling, which is the basis for


multiprogrammed operating systems
 To describe various CPU-scheduling algorithms
 To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system
 To examine the scheduling algorithms of several
operating systems
Basic Concepts

 Maximum CPU utilization


obtained with
multiprogramming
 CPU–I/O Burst Cycle –
Process execution consists of
a cycle of CPU execution and
I/O wait
 CPU burst followed by I/O
burst
 CPU burst distribution is of
main concern
CPU Scheduler

■ Short-term scheduler selects from among the


processes in ready queue, and allocates the CPU to
one of them
● Queue may be ordered in various ways
■ CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Dispatcher

 Dispatcher module gives control of the CPU to the


process selected by the short-term scheduler; this
involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to restart that
program
 Dispatch latency – time it takes for the dispatcher to
stop one process and start another running
Scheduling Criteria

 CPU utilization – keep the CPU as busy as possible


 Throughput – # of processes that complete their execution
per time unit
 Turnaround time – amount of time to execute a particular
process
 TAT=CT-AT
 Waiting time – amount of time a process has been waiting
in the ready queue
 WT=TAT-BT
 Response time – amount of time it takes from when a
request was submitted until the first response is produced,
not output (for time-sharing environment)
Scheduling Algorithm Optimization Criteria

 Max CPU utilization


 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
First- Come, First-Served (FCFS) Scheduling

Process Burst
Time
P 1 24
P 2 3
P 3 3
 Suppose that the processes arrive in the
following order: P , P , P
1 2 3

The Gantt Chart for the schedule is:


Process No. AT BT CT TAT WT

P1 0 24 24 24 0
P2 0 3 27 27 24
P3 0 3 30 30 27

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17
 Average TAT+(24+27+30)/3=27
FCFS Scheduling (Convoy effect - short process behind long process
Consider one CPU-bound and many I/O-bound processes)

Suppose that the processes arrive in the order: P2 , P3 , P1

 The Gantt chart for the schedule is:

Process No. AT BT CT TAT WT

P1 0 24 30 30 6
P2 0 3 3 3 0
P3 0 3 6 6 3

 Average waiting time: (6 + 0 + 3)/3 = 3


 Average Turn around time: (30 + 3 + 6)/3 = 13
 Much better than previous case
Find the average
CRITERIA:-Arrival Time TATMODE:-Non-Preemptive
and WT using FCFS

Process No. AT BT
P1 0 4
P2 1 3
P3 2 1
P4 3 2
P5 4 5
Gantt Chart-

P1 P2 P3 P4 P5

0 4 7 8 10 15
Process No. AT BT CT TAT WT

P1 0 4 4 4 0
P2 1 3 7 6 3
P3 2 1 8 6 5
P4 3 2 10 7 5
P5 4 5 15 11 6

Average TAT=(4+6+6+7+11)/5=6.8

Average WT=(0+3+5+5+6)/5=3.8
Shortest-Job-First (SJF) Scheduling

 CRITERIA:-Burst Time
 MODE:-Non -preemtive
 Associate with each process the length of its next
CPU burst
 Use these lengths to schedule the process with the shortest
time

 SJF is optimal – gives minimum average waiting


time for a given set of processes
Example of SJF

Process Arrival Time


Burst Time
P1 0
6
P2 0
8
P3 0
7
 SJF scheduling chart
P4 0
3
Process No. AT BT CT TAT WT

P1 0 6 9 9 3
P2 0 8 24 24 16
P3 0 7 16 16 9
P4 0 3 3 3 0

 Average Turn around time = (9 + 24 + 16 + 3) / 4 = 13

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


Find average TAT & WT using SJF

P.N0 AT BT
P1 1 7
P2 2 5
P3 3 1
P4 4 2
P5 5 8

Gantt chart-

P1 P3 P4 P2 P5

0 1 8 9 11 16 24
Process No. AT BT CT TAT WT

P1 1 7 8 7 0
P2 2 5 16 14 9
P3 3 1 9 6 5
P4 4 2 11 7 5
P5 5 8 24 19 11

Average TAT=(7+14+6+7+19)/5=10.6

Average WT=(0+9+5+5+11)/5=6
Example of Shortest-remaining-time-first

 CRITERIA:-Burst Time
 MODE:-Preemptive
Process Arrival Time
Burst Time
P 1 0 8
P 2 1 4
P 3 2 9
P 4 3 5
 Preemptive SJF Gantt Chart
Process No. AT BT CT TAT WT

P1 0 8 17 17 9
P2 1 4 5 4 0
P3 2 9 26 24 15
P4 3 5 10 7 2

Average TAT=(17+4+24+7)/4=13

Average WT=(9+0+15+2)/4=6.5
Find average TAT & WT using SRTF

P.No AT BT
P1 0 8
P2 1 6
P3 2 4
P4 3 1
P5 4 5
P6 5 1

Gantt Chart-
P1 P2 P3 P4 P3 P6 P3 P2 P5 P1
0 1 2 3 4 5 6 8 13 18 25
Process No. AT BT CT TAT WT

P1 0 8 25 25 17
P2 1 6 18 17 11
P3 2 4 9 7 3
P4 3 1 4 1 0
P5 4 5 13 9 4
P6 5 1 6 1 0

Average TAT=(25+17+7+1+9+1)/6=10

Average WT=(17+11+3+0+4+0)/6=5.8
Question on SRTF

 Consider three processes, all arriving at time zero, with total


execution time of 10, 20 and 30 units, respectively. Each
process spends the first 20% of execution time doing I/O, the
next 70% of time doing computation, and the last 10% of time
doing I/O again. The operating system uses a shortest
remaining time first scheduling algorithm and schedules a new
process either when the running process gets blocked on I/O or
when the running process finishes its compute burst. Assume
that all I/O operations can be overlapped as much as possible.
For what percentage of time does the CPU remain idle?

(A) 0%
(B) 10.6%
(C) 30.0%
(D) 89.4%
SOLUTION

PNO. AT TET I/O CPU TIME I/O


P1 0 10 2 7 1
P2 0 20 4 14 2
P3 0 30 6 21 3

P1 P2 P3

0 2 9 23 44 47

% of time CPU is IDLE=(5/47)*(100)=10.6 %


Priority Scheduling

 A priority number (integer) is associated with each process


 The CPU is allocated to the process with the highest priority (smallest
integer  highest priority/lowest depends o definitions , default highest
priority)
 Preemptive
 Non-preemptive
 SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
 Problem  Starvation – low priority processes may never execute
 Solution  Aging – as time progresses increase the priority of the process
Example of Priority Scheduling(Non-preemptive)

Process
Burst Time
Priority
P 1 10 3
P 2 1 1
P 3 2 4
P 4 1 5
P5 5 2
 Priority scheduling Gantt Chart
P2 P5 P1 P3 P4

0 1 6 16 18 19
Process AT BT Priority CT TAT WT
No.

P1 0 10 3 16 16 6
P2 0 1 1 1 1 0
P3 0 2 4 18 18 16
P4 0 1 5 19 19 18
P5 0 5 2 6 6 1

Average TAT=(16+1+18+19+6)/5=12

Average WT=(6+0+16+18+1)/5=8.2
Preemtive priority Scheduling-( Smallest integer means low priority)

PNO. PRIORITY AT BT
P1 2(low priority) 0 4
P2 4 1 2
P3 6 2 3
P4 10 3 5
P5 8 4 1
P6 12(high priority) 5 4
P7 9 6 6

Gantt Chart-

P1 P2 P3 P4 P6 P4 P7 P5 P3 P2 P1

0 1 2 3 5 9 12 18 19 21 22 25
PNO. PRIORIT AT BT CT TAT WT RT
Y
25 25 21 0
P1 2 0 4
P2 4 1 2 22 21 19 0

P3 6 2 3 21 19 16 0
P4 10 3 5 12 9 4 0
P5 8 4 1 19 15 14 14
P6 12 5 4 9 4 0 0
P7 9 6 6 18 12 6 6
P1 P2 P3 P4 P6 P4 P7 P5 P3 P2 P1
0 1 2 3 5 9 12 18 19 21 22 25
Average TAT=(25+21+19+9+15+4+12)/7=15
Average WT=(21+19+16+4+14+0+6)/7=11.43
Average RT=(0+0+0+0+14+0+6)/7=2.86
Round Robin (RR)

 Each process gets a small unit of CPU time (time


quantum q), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and added
to the end of the ready queue.
 If there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once.
No process waits more than (n-1)q time units.
 Timer interrupts every quantum to schedule next
process
 Performance
 q large  FIFO
 q small  q must be large with respect to context switch,
otherwise overhead is too high
Example of RR with Time Quantum = 4

Process Burst Time


P 24
1

P 3 2

P 3 3

 The Gantt chart is:

 Typically, higher average turnaround than SJF, but


better response
 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 usec
Find the average TAT & WT using Round
robin with Time Quantum=2
PNO. AT BT
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3

Gantt Chart

P1 P2 P3 P1 P4 P5 P2 P6 P5 P2 P6 P5
0 2 4 6 8 9 11 13 15 17 18 19 21
PNO AT BT CT TAT WT
P1 0 4 8 8 4
P2 1 5 18 17 12
P3 2 2 6 4 2
P4 3 1 9 6 5
P5 4 6 21 17 11
P6 6 3 19 13 10

Average TAT=(8+17+4+6+17+13)/6=10.83
Average WT=(4+12+2+5+11+10)/6=7.33
Find the average TAT & WT using Round
robin with Time Quantum=4
PNO. AT BT
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3

Gantt Chart

P1 P2 P3 P4 P5 P6 P2 P5
0 4 8 10 11 15 18 19 21
PNO AT BT CT TAT WT
P1 0 4 4 4 0
P2 1 5 19 18 13
P3 2 2 10 8 6
P4 3 1 11 8 7
P5 4 6 21 17 11
P6 6 3 18 13 10

Average TAT=(4+18+8+8+17+13)/6=11.33
Average WT=(0+13+6+7+11+10)/6=7.83
LJF

 Characteristics of Longest Job First(Non-Preemptive)


• Among all the processes waiting in a waiting queue, the
CPU is always assigned to the process having the largest
burst time.
• If two processes have the same burst time then the tie is
broken using FCFS i.e. the process that arrived first is
processed first.
• LJF CPU Scheduling can be of both preemptive and non-
preemptive types.
 This may lead to a convoy effect.
LJF

• Step-1: First, sort the processes in increasing order


of their Arrival Time.
• Step 2: Choose the process having the highest Burst
Time among all the processes that have arrived till
that time.
• Step 3: Then process it for its burst time. Check if
any other process arrives until this process
completes execution.
• Step 4: Repeat the above three steps until all the
processes are executed.

LJF Example
Processes Arrival time Burst Time

P1 1 ms 2 ms

P2 2 ms 4 ms

P3 3 ms 6 ms

P4 4 ms 8 ms
LRTF Example

Processes Arrival time Burst Time

P1 1 ms 2 ms

P2 2 ms 4 ms

P3 3 ms 6 ms

P4 4 ms 8 ms
HRRN

• Highest Response Ratio Next is a non-preemptive CPU Scheduling algorithm and it is


considered as one of the most optimal scheduling algorithms.
• The criteria for HRRN is Response Ratio, and the mode is Non-Preemptive.
• HRRN is considered as the modification of the Shortest Job First to reduce the problem of
starvation.
• In comparison with SJF, during the HRRN scheduling algorithm, the CPU is allotted to the
next process which has the highest response ratio, and not to the process having less
burst time.
• HRRN is not practically feasible to implement the HRRN scheduling algorithm because we
cannot predict the burst time of every process in advance.
• HRRN requires the calculation of the response ratio for each process, which adds some
computational overhead, especially in systems with a large number of processes.
• It works by giving priority to processes that have been waiting longer or require less CPU
time. This method helps in reducing waiting times and ensures fairness by preventing
starvation.
HRRN
HRRN
HRRN
Multilevel Queue

 Ready queue is partitioned into separate queues, eg:


 foreground (interactive)
 background (batch)
 Process permanently in a given queue
 Each queue has its own scheduling algorithm:
 foreground – RR
 background – FCFS
 Scheduling must be done between the queues:
 Fixed priority scheduling; (i.e., serve all from foreground then from
background). Possibility of starvation.
 Time slice – each queue gets a certain amount of CPU time which it
can schedule amongst its processes; i.e., 80% to foreground in RR
 20% to background in FCFS
Multilevel Queue Scheduling
Priority of queue 1 is greater than queue 2. queue 1 uses Round Robin (Time Quantum
= 2) and queue 2 uses FCFS.
Below is the Gantt chart of the problem:
Multilevel Feedback Queue

 A process can move between the various queues;


aging can be implemented this way
 Multilevel-feedback-queue scheduler defined by the
following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter
when that process needs service
Example of Multilevel Feedback Queue

 Three queues:
 Q0 – RR with time quantum 8 milliseconds
 Q1 – RR time quantum 16 milliseconds
 Q2 – FCFS

 Scheduling
 A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to
queue Q1
 At Q1 job is again served FCFS and receives 16
additional milliseconds
If it still does not complete, it is preempted and
moved to queue Q2

You might also like