You are on page 1of 25

Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling


• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Thread Scheduling
• Multiple-Processor Scheduling
• OS‘s Examples
• Algorithm Evaluation
Objectives
• To introduce CPU scheduling, which is the basis for
multi-programmed OS’s
• To describe various CPU-scheduling algorithms
• To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system
Basic Concepts
• Maximize CPU utilization obtained with
multiprogramming
• Process schedule is an OS fn.
• Process execution consists of
o cycle of CPU execution 🡪CPU burst
o I/O wait 🡪 I/O burst
Large number of processes
with short CPU bursts

small number of processes


with long CPU bursts

Histogram of CPU-burst durations


CPU Curve is exponential or hyper exponential
CPU Scheduler
• Whenever CPU is idle, OS selects one of the processes in
ready queue to be executed

• Ready queue can be implemented as


o First-in, first-out queue “FIFO queue”
o Priority queue
o Linked list
o Tree

• CPU scheduler (short-term scheduler) is responsible for the


selection

• Records in the queue are generally PCB(s) of the processes


CPU Scheduling
• CPU scheduler (short-term scheduler) is
responsible for the selection
• CPU scheduling decisions take place under
following circumstances:
1. Switches from running to waiting state
• Eg. I/O request or wait for a child process
to terminate
Preemptive
scheduling

Preemptive
scheduling
Non

2. Switches from running to ready state


• Eg. Interrupt occurs

3. Switches from waiting to ready state


• Eg. I/O request completion

4. Terminates
CPU Scheduling
• Tasks are usually assigned with priorities
o Task (1) is running
o A higher priority task (2) has to run
o What will happen to task (1)?
• Types of scheduling
o Non-preemptive scheduling (Cooperative scheduling)
• running task (1) is executed till I/O request or terminated
(cannot be interrupted)
o Preemptive scheduling
• Task (1) is interrupted for some time and resumed later
when the priority task (2) has finished its execution.
CPU Scheduling
o Non-preemptive scheduling (Cooperative scheduling)
• does not allow a process running in kernel mode to be
preempted
• running task is executed till I/O request or terminated
(cannot be interrupted)

o Preemptive scheduling
• allows a process to be preempted while it is running in
kernel mode (but all data must be consistent).
• Task (1) is interrupted for some time and resumed later
when the priority task (2) has finished its execution.
Dispatcher
• Dispatcher module
o Gives control of CPU to selected process
o Should be as fast as possible, since it is invoked during
every process switch
• This involves:
o Switching context
o Switching to user mode
o Jumping to the proper location in the user program to
restart that program

• Dispatch latency – time taken for the dispatcher to stop


one process and start another running
Scheduling Algorithm Optimization
Criteria
1) CPU utilization
o Keep CPU as busy as possible

2) Throughput maximize
o Number of completed process per unit time

3) Turnaround time
o Interval of time from submission till process completion
time
o Sum (waiting period to get to memory, waiting in ready
queue, CPU execution, I/O operation)
minimize
Scheduling Algorithm Optimization
Criteria

4) Waiting time
o Sum of time the process spent in the ready queue
minimize

5) Response time (in interactive system)


o Time from submission of a request until the production
of 1st response (result)
o Limited by speed of the output device

In interactive system,
it is desirable to have a system with reasonable &
predictable response time
rather than a system that is faster on average but is highly
variable
First-Come, First-Served (FCFS)
Scheduling

Process Burst Time (milliseconds)


P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order:
P 1 , P2 , P 3

o Draw Gantt Chart for the schedule?


o What is the average waiting time ?
First-Come, First-Served (FCFS)
Scheduling
Process Burst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order:
P 1 , P2 , P 3
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
FCFS Scheduling (Cont.)
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
• Much better than previous case
• Convoy effect occurs as short process are behind
long process
Example of SJF- Same Arrival
Time
Process Arrival Time Burst Time
P1 0.0 6
P2 0.0 8
P3 0.0 7
P4 0.0 3

o Draw Gantt Chart for the schedule?


o What is the average waiting time ?
Example of SJF- Same Arrival
Time
Process Arrival Time Burst Time
P1 0.0 6
P2 0.0 8
P3 0.0 7
P4 0.0 3
• SJF scheduling chart

P4 P1 P3 P2

0 3 9 16 24
• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
Example of SJF- Non Preemptive
Different Arrival Time
Process Arrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

o Draw Gantt Chart for the schedule?


o What is the average waiting time ?
Example of SJF- Non Premptive
Different Arrival Time
Process Arrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
• SJF scheduling chart non preemptive
P1 P4 P3 P2

0 6 9 16 24

• Average waiting time = (0+(16-2)+(9-4)+(6-5)) / 4 = 5


Priority Scheduling

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


• CPU is allocated to the process with the highest priority
o Highest priority (smallest integer . highest priority)
Process Burst TimePriority Time
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
P2 P5 P1 P3 P4

0 1 6 16 18 19

• Average waiting time = 8.2


Priority Scheduling
• Priority can be defined
o Internally
• Is determined through a set measurable quantities
– memory requirement, average I/O burst & CPU
burst
o Externally
• Set by a criteria outside the OS
– Importance of a process, amount of money for
computer use, political factors, etc…
• When a process arrives at ready queue is compared with
priority of the currently running process and reacts in two
ways of
• Preemptive
– If priority of new process > priority of running
process
• Non-preemptive
– Puts new process at the head of the ready queue
Priority Scheduling
• SJF & Priority Schedule
o are priority scheduling where priority is the predicted
next CPU burst time

o Problem:
– Starvation or indefinite blocking
» low priority processes may never execute

• Solution
o Aging: as time progresses increase the priority of the
process
Round Robin (RR) Scheduling
• Steps:
o Each process gets a small unit of CPU time (time quantum),
usually 10-100 milliseconds.
o After this time has elapsed, the process is preempted and
added to the end of the ready queue.
• No process waits more than (n-1)q time units,
o for n processes in ready queue and time quantum is q,

• Performance is related to q
o If value is large ⇒ FIFO
o If value is small ⇒ overhead is too high
o q must be large with respect to context switching time
Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 3
P3 3

o Draw Gantt Chart for the schedule?


o What is the average waiting time ?
Example of RR with Time Quantum = 4
Process Burst Time Wait Time
P1 24 (10-4)=6
P2 3 4
P3 3 7
• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

• Average waiting time =17/3 milliseconds


• Typically, higher average turnaround than SJF,
but better response
Time Quantum and Context Switch
Time

No of
context
switch

• In modern systems,
o Context switch < 10 microseconds
o q ranges from 10 to 100 milliseconds

You might also like