0% found this document useful (0 votes)
83 views36 pages

CPU Scheduling

This document discusses CPU scheduling and different scheduling algorithms. It begins by explaining the CPU-I/O burst cycle, where a process alternates between CPU execution and I/O wait. It then discusses the role of the CPU scheduler in allocating the CPU to ready processes and when scheduling decisions take place. Common scheduling criteria like CPU utilization, throughput, turnaround time and waiting time are presented. First-come first-served (FCFS) scheduling and shortest job first (SJF) scheduling, both non-preemptive and preemptive variants, are described and compared using examples. SJF scheduling provides the optimal solution of minimum average waiting time but can starve longer processes. The challenges of accurately predicting process CPU burst

Uploaded by

Arpan Dalai
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)
83 views36 pages

CPU Scheduling

This document discusses CPU scheduling and different scheduling algorithms. It begins by explaining the CPU-I/O burst cycle, where a process alternates between CPU execution and I/O wait. It then discusses the role of the CPU scheduler in allocating the CPU to ready processes and when scheduling decisions take place. Common scheduling criteria like CPU utilization, throughput, turnaround time and waiting time are presented. First-come first-served (FCFS) scheduling and shortest job first (SJF) scheduling, both non-preemptive and preemptive variants, are described and compared using examples. SJF scheduling provides the optimal solution of minimum average waiting time but can starve longer processes. The challenges of accurately predicting process CPU burst

Uploaded by

Arpan Dalai
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/ 36

 CPU Scheduling

Dr. Manmath N. Sahoo


Dept. of CSE, NIT Rourkela
CPU-I/O Burst Cycle
 CPU–I/O Burst Cycle – Process
execution consists of a cycle of CPU
execution and I/O wait.
 CPU burst is length of time process
needs to use CPU before it next
makes a I/O request.
 I/O burst is the length of time
process spends waiting for I/O to
complete.

Dr. Manmath N. Sahoo (CS) 2


Histogram of CPU-burst Times
Typical CPU burst distribution

Dr. Manmath N. Sahoo (CS) 3


CPU Scheduler

 Allocates CPU to one of processes that are ready to execute (in


ready queue)
 CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state (e.g. I/O request)
2. Terminates
3. Switches from waiting to ready (e.g. I/O completion)
4. Switches from running to ready state (e.g. priority based interruption)
 Scheduling only when 1 and 2 happen is nonpreemptive -
process keeps CPU until it voluntarily releases it
 Scheduling also when 3 & 4 happen is preemptive - CPU can be
taken away from process by OS

Dr. Manmath N. Sahoo (CS) 4


Dispatcher

 Dispatcher gives control of the CPU to the process


selected by the short-term scheduler; this involves:
 switching context
 switching to user mode (if CPU was executing the previous
process in kernel mode and preempted)
 jumping to the proper location in the user program to restart that
program (i.e., last action is to set program counter)
 Dispatch latency – time it takes for the dispatcher
to switch between processes and start new one
running

Dr. Manmath N. Sahoo (CS) 5


Scheduling Criteria

 CPU utilization: i.e. CPU usage – want to maximize


 Throughput: number of processes that complete their
execution per time unit – want to maximize
 Turnaround time: amount of time to execute a particular
process – want to minimize
 Waiting time: amount of time a process has been waiting
in the ready queue – want to minimize
 Response time: amount of time it takes from when a job
was submitted until it initiates its first response (output) –
want to minimize

Dr. Manmath N. Sahoo (CS) 6


First-Come, First-Served Scheduling

Schedule: order of arrival of process in ready queue


Example: Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3. The Gantt
Chart for the schedule is:

P1 P2 P3

0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17

Dr. Manmath N. Sahoo (CS) 7


FCFS Scheduling

Suppose that the processes arrive in the


order P2 , P3 , P1 .
 The Gantt chart for the schedule is:
P2 P3 P1

0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


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

Dr. Manmath N. Sahoo (CS) 8


FCFS Scheduling

+ Simple to implement by FIFO queue.


+ Code is easy to write and understand
- Average waiting time is not minimal
- Not suitable for time-sharing systems
- Convoy effect: short I/O intensive processes
behind long CPU intensive process
NOTE: FCFS Scheduling is non-preemptive
Dr. Manmath N. Sahoo (CS) 9
Shortest-Job-First (SJF) Scheduling

Schedule: order of the CPU burst of the


processes.
Two schemes:
Non-Preemptive SJF / Shortest Job Next (SJN)
once CPU given to the process it cannot be preempted until
completes its CPU burst.
Preemptive SJF / Shortest Remaining Time First
(SRTF)
if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt.
Dr. Manmath N. Sahoo (CS) 10
Non-Preemptive SJF (SJN)

Process Arrival Time Burst Time


P1 0 7
P2 2 4
P3 4 1
P4 5 4
 Gantt Chart:
P1 P3 P2 P4

0 3 7 8 12 16

 Average waiting time = (0 + 6 + 3 + 7)/4 = 4

Dr. Manmath N. Sahoo (CS) 11


Preemptive SJF (SRTF)

Process Arrival Time Burst Time


P1 0 7
P2 2 4
P3 4 1
P4 5 4
 Gantt Chart:

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

 Average waiting time = (9 + 1 + 0 +2)/4 = 3

Dr. Manmath N. Sahoo (CS) 12


Example

Process Arrival Time Burst Time


P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4

 Draw Gantt chat and find the average waiting time


in each of the following cases:
 SRTF Scheduling
 SJN Scheduling
 FCFS Scheduling

Dr. Manmath N. Sahoo (CS) 13


Answer to Example Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
SRTF

P1 P2 P2 P4 P2 P5 P3 P1
0 2 4 6 8 12 16 28 42

Average waiting time = (26+6+12+0+0)/5


= 8.8

Dr. Manmath N. Sahoo (CS) 14


Answer to Example Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
SJN
P1 P4 P5 P2 P3
0 16 18 22 30 42

Average waiting time = (0+20+26+10+10)/5


= 13.2

Dr. Manmath N. Sahoo (CS) 15


Answer to Example Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
FCFS
P1 P2 P3 P4 P5
0 16 24 36 38 42

Average waiting time = (0+14+20+30+30)/5


= 18.8

Dr. Manmath N. Sahoo (CS) 16


Answer to Example

Scheduling Algorithm Average Waiting Time


FCFS 18.8
SJN 13.2
SRTF 8.8

Dr. Manmath N. Sahoo (CS) 17


Shortest Job First

+ SRTF is Optimal: Minimum Average waiting time.


- Longer processes may be starved.
- Difficult to know the CPU burst of processes before their
execution

Dr. Manmath N. Sahoo (CS) 18


Determining Length of Next CPU
Burst
Can only estimate the length.
Can be done by using the length of previous
CPU bursts, using exponential averaging
(decaying average).

Dr. Manmath N. Sahoo (CS) 19


Determining Length of Next CPU
Burst
1. t n  actual lenght of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :  n1   tn  1    n .

Dr. Manmath N. Sahoo (CS) 20


Determining Length of Next CPU
Burst
  =0, n+1 = n
 last CPU burst does not count - only longer term history
  =1, n+1 = tn
 Only the actual last CPU burst counts.
 If we expand the formula, we get:
n+1 =  tn+(1 - )  tn-1 + …
+(1 -  )j  tn-j + …
+(1 -  )n+1 0

 Since both  and (1 - ) are less than or equal to 1,


each successive term has less weight than its
predecessor.
Dr. Manmath N. Sahoo (CS) 21
Round Robin (RR) Scheduling

Each process gets a time quantum, 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.
No process waits more than (n-1)q time units.

Dr. Manmath N. Sahoo (CS) 22


Example: RR with q= 20

Process Burst Time


P1 53
P2 17
P3 68
P4 24
The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162


Typically, higher average turnaround than SJF,
but better response.
Dr. Manmath N. Sahoo (CS) 23
Round Robin (RR) Scheduling

FCFS + Preemption = RR
Performance
q large  behaves similar to FCFS
q small  more context switch overhead

Dr. Manmath N. Sahoo (CS) 24


Modified RR Scheduling

Based on variable time quantum


A preempted process, on its next turn will
use an increased time quantum
RR Scheduling
P1: 300ms, q=20ms, time per context switch=2ms
#of preemptions due to this process = 14
Preemption overhead = 28ms

Dr. Manmath N. Sahoo (CS) 25


Modified RR Scheduling

Modified RR Scheduling – q is doubled in


next turn.
P1: 300ms, q=20ms (initial)
#of preemptions due to this process = 3.
(20+40+80+160=300)
Preemption overhead = 6ms

Dr. Manmath N. Sahoo (CS) 26


Priority Scheduling

 A priority number (integer) is associated with each


process and the CPU is allocated to the process with
the highest priority
 Some schemes use smallest integer  highest
priority.
 SJF is effectively priority scheduling where priority
is defined on next CPU burst time.
 FCFS is effectively priority scheduling where
priority is defined on arrival time.

Dr. Manmath N. Sahoo (CS) 27


Priority Scheduling

Problem: Starvation – low priority


processes may never execute.
Solution: Aging – as time progresses
increase the priority of the process.
e.g. in every 10ms increase the priority of process by 1

Dr. Manmath N. Sahoo (CS) 28


Multi level Queue Scheduling

Ready queue is partitioned into separate


queues.
Each queue holds a particular category of
processes.
Each queue can implement whatever
scheduling algorithm is most appropriate for
that type of processes.
Dr. Manmath N. Sahoo (CS) 29
Multi level Queue Scheduling

Dr. Manmath N. Sahoo (CS) 30


Multilevel Queue Scheduling

Scheduling must be done between the


queues.
Priority scheduling; But possibility of starvation.
Time slice – each queue gets a certain amount of CPU
time which it can schedule amongst its processes;

Dr. Manmath N. Sahoo (CS) 31


Multilevel Feedback Queue Scheduling

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 initially

Dr. Manmath N. Sahoo (CS) 32


Multilevel Feedback Queue Scheduling
FCFS

quantum = 16

quantum = 8

Dr. Manmath N. Sahoo (CS) 33


Multi Processor Scheduling

Separate queue for each processor


Common ready queue

Dr. Manmath N. Sahoo (CS) 34


Multi Processor Scheduling:
Separate queues

4 Processors: C1, C2, C3, C4.


P1 goes to C1
P2 goes to C2
P3 goes to C3
P4 goes to C4
P5 goes to C1
P6 goes to C2 …
Load balancing w.r.t size of the processes???
C1 may be overloaded, while C3 is idle

Dr. Manmath N. Sahoo (CS) 35


Multi Processor Scheduling:
Common ready queue

Symmetric Approach:
Each processor examines the common ready queue
and selects a process to execute.
Several instances of STS will be running on different
processors, whenever necessary
Mutual exclusive access to the common queue???
Asymmetric Approach
One processor acts as scheduler (Master) for other
processor (Slaves)

Dr. Manmath N. Sahoo (CS) 36

You might also like