0% found this document useful (0 votes)
37 views92 pages

Chapter 5-CPU Scheduling

Uploaded by

70131290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views92 pages

Chapter 5-CPU Scheduling

Uploaded by

70131290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 92

Chapter 5

CPU Scheduling
CPU- and I/O-bound processes

• Bursts of CPU usage alternate with periods of I/O


wait
– a CPU-bound process
– an I/O bound process
Goals of Scheduling
Types of Scheduling
Scheduling and Process State
Transitions
Levels of
Scheduling
Queuing Diagram
Histogram of CPU-burst
Times
The figure shows the durations of CPU bursts.
Processes generally have a large number of short CPU bursts and
small number of long CPU bursts.
Alternating Sequence of CPU and
I/O Bursts
Remember: I/O bound VS CPU
bound
• Processes can be described as either:
– I/O-bound process – spends more time doing I/O than computations, many short
CPU bursts
– CPU-bound process – spends more time doing computations; few very long
CPU bursts

• Long term scheduler should select a good mix of I/O bound and CPU bound
processes.

• I/O bound -> ready queue almost empty, CPU bound -> I/O wait queue empty (wasting
resources)
Remember: Diagram of
Process State
CPU Scheduler
• Selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them

• 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

• Scheduling under 1 and 4 is non preemptive

• All other scheduling is preemptive


CPU Scheduler
• Preemptive
Preemptive scheduling is the act of temporarily interrupting a task
being carried out without requiring its cooperation, and with the
intention of resuming the task at a later time. Context switch from
one running process to another process.

• Non- preemptive
Non-preemptive scheduling is an act of never initiating a context
switch from a running process to another process. In this case the
task can self-interrupt and voluntarily give control to other tasks.
When non preemptive is used, a process that receives such
resources can not be interrupted until it is finished.

In preemptive scheduling we preempt the currently executing process.

In non preemptive we allow the current process to finish its CPU burst time.
Scheduling Criteria
Scheduling Criteria
Scheduling Algorithm
Optimization Criteria
Scheduling Algorithms
Scheduling Algorithms
1. FCFS Scheduling
Convoy Effect

 Convoy Effect is phenomenon associated with the First Come


First Serve (FCFS) algorithm, in which the whole Operating
System slows down due to few slow processes.

 To avoid Convoy Effect, preemptive scheduling algorithms like


Round Robin Scheduling can be used – as the smaller processes
don’t have to wait much for CPU time – making their execution
faster and leading to less resources sitting idle.
Scheduling Algorithms

• For simplicity, we consider only one CPU


burst per process in the following
examples.
– In reality, each process can have several
hundred CPU bursts and I/O bursts.

• The measure of comparison is the


average waiting time.
First-Come, First-Served Scheduling
• The simplest CPU-scheduling algorithm is the first-
come, first-served (FCFS) scheduling algorithm.

• The process that requests the CPU first is allocated the


CPU first.

• Can be easily managed with a FIFO queue.


– When a process enters the ready queue, its PCB is
linked onto the tail of the queue.
– When the CPU is free, it is allocated to the process at
the head of the queue.
First-Come, First-Served (FCFS) Scheduling

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
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P 2 , P3 , P 1
• 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 short process behind long process
FCFS Scheduling (Cont.)
• The code for FCFS is simple to write and understand
• The average waiting time under FCFS is often quite long
• FCFS scheduling algorithm is non-preemptive
• FCFS is troublesome for time-sharing systems ?
• Convoy effect : other processes wait for one big process to get off the CPU
Shortest-Job-First (SJF)
Scheduling
• Associate with each process the length of its next CPU burst. Use
these lengths to schedule the process with the shortest time.

• Two schemes:
– Nonpreemptive – once CPU given to the process it cannot
be preempted until completes its CPU burst.
– Preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).

• SJF is optimal – gives minimum average waiting time for a given set
of processes
– The difficulty is knowing the length of the next CPU request.
Example of SJF (non-
preemptive)
ProcessArrival TimeBurst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.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)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
P1 P3 P2 P4

0 3 7 8 12 16

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


Example of SRTF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SRTF
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

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


Example of SRTF
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• SRTF

• Average waiting time = ?


Question
Job Arrival Time Burst Time Priority
J1 4 5 3
J2 0 5 0
J3 1 10 2
J4 2 3 1

• FCFS
• SJF
• SRTF
• RR with Q=3
• Preemptive Priority (A higher number indicates a greater priority)

• Average waiting time = ?


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)
– Preemptive – can preempt the CPU if the priority of the newly
arrived process is higher than the priority of the currently running
process.
– No-npreemptive – simply put the new process at the head of the
ready queue.

• SJF is a priority scheduling where priority is the predicted next CPU


burst time

• Problem  Starvation – low priority processes may never execute

• Solution  Aging – as time progresses increase the priority of the


process
Priority Scheduling
• Problem  Starvation – low priority processes may never execute
– Rumor: when the IBM 7094 at MIT shut down in 1973,
administrators found a low-priority process that had been
submitted in 1967, and had not yet been run

• Solution  Aging – as time progresses increase the priority of the


process
– Gradually increase the priority of processes that wait in the system
for a long time.
– A process would eventually arrive the highest priority and would be
executed
Priority Scheduling
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 arrive in the order
If the process 5 2 P1, P2, …, P5 at time 0, we would
schedule these processes as follows:

• Priority Scheduling
P2 P5 P1 P3 P4
0 1 6 16 18 19

• Average waiting time =


The average waiting time is 8.2 ms.
Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum), usually
10-100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.

• RR = FCFS + Time Quantum.

• 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.

• Performance
– q large  FCFS / 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
P1 24
P2 3
P3 3

• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30
Example of RR with Time
Quantum = 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


Time Quantum and Context Switch Time
Turnaround Time Varies With
The Time Quantum
Round Robin (RR)
Time Quantum and Context
Switch Time
• Avg. Turnaround time can be improved if most processes finish their
next CPU Burst in a single time quantum

• Time quantum should be large compared with context switch time, but
not too large else >> FCFS / FIFO

• Rule of thumb : 80 % of the CPU Bursts < Time Quantum

• In most modern systems Time Quantum = 10 -100 milliseconds

• Time required for context switching < 10 microseconds


Queuing Diagram
Comparisons between FCFS
and Round Robin
• Assuming zero-cost context-switching
time, is RR always better than FCFS?
• Simple example: 5 jobs, each takes 5s of
CPU time RR scheduler quantum of 1s
All jobs start at the same time
Comparisons between FCFS
and Round Robin
• Both RR and FCFS finish at the same time
• Average response time is much worse under RR!
– Bad when all jobs same length
• Total time for RR longer even for zero-cost switch!
• Example: Process Burst Time
P1 53
P2 8
P3 68
P4 24

P P4 P1 P3
Best FCFS: [8]2 [24] [53] [68]
0 8 32 85 153

P3 P1 P4 P2
Worst FCFS: [68] [53] [24] [8]
0 68 121 145 153
Quantum P1 P2 P3 P4 Average
Best FCFS 32 0 85 8 31¼
Q=1 84 22 85 57 62
Q=5 82 20 85 58 61¼
Wait
Q=8 80 8 85 56 57¼
Time
Q = 10 82 10 85 68 61¼
Q = 20 72 20 85 88 66¼
Worst FCFS 68 145 0 121 83½
Best FCFS 85 8 153 32 69½
Q=1 137 30 153 81 100½
Q=5 135 28 153 82 99½
Completion
Q=8 133 16 153 80 95½
Time
Q = 10 135 18 153 92 99½
Q = 20 125 28 153 112 104½
Worst FCFS 121 153 68 145 121¾
Highest Response Ratio Next
• Choose next process with the greatest ratio

– shorter processes are favored


– aging without service increases the ratio so that
a longer process will eventually be served
Highest Response Ratio Next
• Choose next process with the greatest ratio
Multilevel Queue
• Ready queue is partitioned into separate queues:
– foreground (interactive)
– background (batch)

• 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 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
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.
Multilevel Feedback Queues
Question
• On a system using multilevel feedback
queues, a totally CPU-bound process
requires 40 seconds to execute. If the first
queue uses a time quantum of 2 and at
each level the time quantum increases by
5 time-units, how many times will the job
be interrupted and on what queue will it be
when it terminates?
Question
• On a system using multilevel feedback
queues, a totally CPU-bound process
requires 50 seconds to execute. If the first
queue uses a time quantum of 5, and at
each lower level the time quantum
doubles, how many times will the job be
interrupted and on what queue will it be
when it terminates?
Question
Process Arrival time Burst Time
1 0 4
2 3 4
3 4 3
4 9 2
Question
• Imagine we have a multi-level feedback queue
with 2 queues. The highest priority queue is a
RR scheduler with a quantum of 2. The second
priority queue runs as a FCFS queue. Processes
start in the RR queue and are demoted to the
FCFS queue if they exceed their quantum.
Processes in the RR queue are always
prioritized over the FCFS queue. Calculate the
average waiting time by using the multi level
feed back queue and draw the Gantt chart
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
 n 1  t n  1    n .

1. t n actual length of n th CPU burst


2.  n 1 predicted value for the next CPU burst
3.  , 0  1
4. Define :
Exponential Averaging
 Plugging in value for n, we get
n+1 = tn + (1- )[tn-1 + (1- )n-1]
= tn + (1- )tn-1 + (1- )2n-1
 Continuing like this results in
n+1 =  tn+ (1 - )  tn-1 + …
+ (1 - )j  tn-j + …
+ (1 - )n+1 0
Exponential Averaging
 If  = 0
 n+1 = n
 Actual previous bursts do not
count
 If  = 1
 n+1 = tn
 Estimates do not count
Exponential Averaging
• Typical value used for  is ½. With this
value, our n+1st estimate is

n+1 = tn/2 + tn-1/22 + tn-2/23 + tn-3/24 + …

Hence the name exponential


averaging
Prediction of the Length of the Next CPU Burst

You might also like