0% found this document useful (0 votes)
11 views115 pages

OS_CH5 (2)

Chapter 5 of 'Operating System Concepts' discusses CPU scheduling, emphasizing the importance of maximizing CPU utilization through multiprogramming and various scheduling algorithms. It outlines key scheduling criteria such as CPU utilization, throughput, turnaround time, waiting time, and response time, and compares different scheduling algorithms including First-Come, First-Served (FCFS), Shortest-Job-First (SJF), and Round Robin. The chapter also highlights the differences between preemptive and nonpreemptive scheduling methods and their respective advantages and disadvantages.

Uploaded by

roror raghad
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)
11 views115 pages

OS_CH5 (2)

Chapter 5 of 'Operating System Concepts' discusses CPU scheduling, emphasizing the importance of maximizing CPU utilization through multiprogramming and various scheduling algorithms. It outlines key scheduling criteria such as CPU utilization, throughput, turnaround time, waiting time, and response time, and compares different scheduling algorithms including First-Come, First-Served (FCFS), Shortest-Job-First (SJF), and Round Robin. The chapter also highlights the differences between preemptive and nonpreemptive scheduling methods and their respective advantages and disadvantages.

Uploaded by

roror raghad
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/ 115

Chapter 5: CPU Scheduling

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Chapter 5: CPU Scheduling

• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Objectives

• Describe various CPU scheduling algorithms


• Assess CPU scheduling algorithms based on scheduling criteria

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Basic Concepts

• Maximum CPU utilization obtained with multiprogramming


• By switching among processes the OS makes the computer more
productive
• The idea is simple: the process is executed until it must wait; for
example waiting for I/O completion
• In simple computer, if the CPU becomes idle until the I/O finishes
then this is a waste of time
• With multiprogramming the goal is to utilize this time
• OS keeps several processes in memory
• When a process has to wait, the OS takes the CPU from that
process and gives to another process

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Basic Concepts
• Scheduling is a fundamental operating
system function.
• Maximum CPU utilization obtained with
multiprogramming.
• The algorithm, the scheduler uses is called
scheduling algorithm.
• Several processes are kept in memory at one
time.
• CPU–I/O Burst Cycle – Process execution
consists of a cycle of CPU execution and I/O
wait
• Either CPU executing instruction
• Or process is waiting for I/O
• The process alternate between these two
states
• CPU burst followed by I/O burst
• CPU burst distribution is of main concern

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Histogram of CPU-burst Times

Large number of short bursts

Small number of longer bursts

• I/O-bound program typically has many short CPU


bursts
• CPU-bound program has few long CPU bursts
Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
(with multiprograming)

• In a single processor system, only one process can run at a time.


Others must wait until CPU is free and can be rescheduled.
• Whenever the CPU becomes idle, the operating system must select
one of the processes in the ready queue to be executed (the records
in the queues are generally PCBs of the pocesses).
• The short term scheduler (or CPU scheduler- a part of OS) selects a
process to get the processor (CPU) among the processes which are
already in memory. (i.e. from the ready queue).
• Processor scheduling algorithms try to answer the following crucial
question.
• Which process in the ready queue will get the processor?
• In order to answer this, one should consider the relative importance
of several performance criteria.

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
• The CPU scheduler selects from among the processes in ready
queue, and allocates the a CPU core to one of them
• Queue may be ordered in various ways; not necessarily FIFO
(First In First Out)
• But all processes in the ready queue are linked waiting for a
chance to run on the CPU
• Records in the queue are simply the PCBs of the processes

Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
1. As a result of I/O request or an invocation of wait() for
termination of the child process
2. Switches from running to ready state
2. When an interrupt occurs
3. Switches from waiting to ready
3. For example. I/O completion
4. Terminates
4. When a process terminates
5. Or a new process joins the ready queue

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
(with multiprograming)

4
5
2

3 1

Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Preemptive – Nonpreemptive Scheduling

5 4 Interrupt: Scheduler

2
picks another
process
Scheduler dispatch:
Scheduler picks
this process to
execute
3 1

• In (1) and (4), a new process must be selected from the ready queue.
• In (2), (3) and (5), previously running process or a new process may be selected.
• Scheduling algorithms that act only in circumstances (1) and (4) are called nonpreemptive(or
noncooperative). Once CPU has been allocated to a process, that process keeps the CPU
until it releases the CPU (either by termination or by requesting I/O). This scheduling method
was used by Microsoft Windows 3.x.
• Otherwise it is preemptive. Windows 95 and all subsequent versions of Windows operating
systems have used preemptive scheduling.

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Preemptive – Nonpreemptive Scheduling
• A scheduling algorithm which acts on all circumstances is called preemptive.
(i.e. such an algorithm can select a new process in circumstances 2, 3 and 5).
• The CPU is allocated to the highest-priority process among all ready processes.
The scheduler is called each time a process enters the ready state.
• Advantages of non-preemptive scheduling algorithms:
• They cannot lead the system to a race condition.
• They are simple.
• Disadvantage of non-preemptive scheduling algorithms:
• They do not allow real multiprogramming.

• Advantage of preemptive scheduling algorithms:


• They allow real multiprogramming.
• Disadvantages of preemptive scheduling algorithms:
• They can lead the system to a race condition.

Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
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
• Dispatcher should be as fast as possible, since it
is invoked during every context switch
• Dispatch latency – time it takes for the
dispatcher to stop one process and start another
running

Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria

• The criteria include the following


• CPU utilization – keep the CPU as busy as possible
• top command can be used to obtain CPU utilization on linux
and mac
• Throughput – # of processes that complete their execution per
time unit
• Turnaround time – amount of time to execute a particular
process
• Interval of time from the time of submission of a process to
the time of completion
• Waiting time – amount of time a process has been waiting in
the ready queue
• 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)

Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria

• CPU utilization - keep the CPU as busy as possible. It is defined as:


• (processor busy time) / (processor busy time +processor idle time)
• Throughput – # of processes that complete their execution per time unit.

• Turnaround time – The interval from the time of submission to the time of
completion of a process.

• Waiting time – amount of time a process has been waiting in the ready queue
(waiting for I/O device is not counted)

• Response time – amount of time it takes from when a request was submitted until
the first response is produced, not output/completion.

Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
• The previous mentioned criteria are used to judge and compare CPU-
scheduling algorithms
• It is desirable to maximize CPU utilization and throughput, and to minimize
turnaround time, waiting time, and response time
• In most cases, we optimize for the average measure
• However, under some circumstances, you might prefer to optimize the
maximum or the minimum values rather than the average
• For example to guarantee that all users get good service, we might want to
minimize the maximum response time

Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Scheduling Algorithm Optimization Criteria

• Maximize CPU utilization


• Maximize throughput
• Minimize turnaround time
• Minimize waiting time
• Minimize response time

Operating System Concepts – 10th Edition 17 Silberschatz, Galvin and Gagne ©2018
Scheduling Algorithms
• CPU scheduling deals with the problem of deciding which of the processes in
the ready queue is to be allocated to the CPU’s core
• Next, we describe some of scheduling algorithms
• Assuming that only one processing core is available

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
CPU Scheduling Algorithms
• First- Come, First-Served (FCFS) - Non preemptive
• Shortest-Job-First (SJF) - Non preemptive
• Shortest-Remaining-Time-First (SRTF) - Preemptive
• Priority Scheduling - Non preemptive, Preemptive
• Round Robin (RR) - Preemptive
• Priority – RR (Multilevel Queue Scheduling, Multilevel Feedback Queue
Scheduling)

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
First-Come, First-Served (FCFS) Scheduling

• This is the simplest one. In this algorithm the set of ready processes is
managed as FIFO (first-in-first-out) Queue. The processes are serviced by the
CPU until completion in the order of their entering in the FIFO queue.

• Once allocated the CPU, each process keeps it until releasing either due to
termination or requesting I/O.
• Average waiting time under FCFS policy is often quite long.

• FCFS scheduling algorithm is nonpreemptive.

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
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 (a bar chart that illustrates a particular schedule, include the start and
finish times of each participating process) for the schedule is:

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


• Average waiting time: (0 + 24 + 27)/3 = 17
• CPU utilization = (30/(30+0))x100 = 100 %

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
• The Gantt chart for the schedule is:

• 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
• Consider one CPU-bound and many I/O-bound processes
• This observation is used in the next algorithm

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
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
• If the next CPU bursts of two processes are the same, FCFS
scheduling is used to break the tie.
• 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
• Some techniques are used to predict the next CPU burst time

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Example of Non-Preemptive SJF
Process Arrival Time CPU Burst Time(ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (non-preemptive)
P1 P3 P2 P4

0 7 8 12 16
• P1 runs first because it arrives first no other process arrives at that time
• P1 takes 7ms to finish, during that time three processes arrived
• Pick the one with shortest CPU burst which is P3
• When P3 finishes, pick the next process, P2 and P4 have the same CPU
burst, thus pick the one which arrived first P2
• Finally pick P4 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8
Example of Non-Preemptive SJF

Process Arrival Time CPU Burst Time(ms)


P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (non-preemptive)

P1 P3 P2 P4

0 7 8 12 16

• W(P1) = 0; W(P2) = 8-2 = 6ms;


• W(P3 ) = 7-4 = 3ms ; W(P4 ) = 12-5 = 7ms
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4ms

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Determining Length of Next CPU Burst

• There is no way to know the length of next CPU burst, but we might be able to
predict it
• Can only estimate the length – should be similar to the previous ones
• Then pick process with shortest predicted next CPU burst
• Can be done by using the length of previous CPU bursts, using

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 :
• Commonly, α set to ½
• tn most recent information, while the second part stores the past history
• Preemptive version called shortest-remaining-time-first

Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Examples of Exponential Averaging

• α =0
• τn+1 = τn
• Recent history does not count
• α =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 τ0

• Since both α and (1 - α) are less than or equal to 1, each successive


term has less weight than its predecessor

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Shortest-Job-First (SJF) Scheduling

• The SJF can be either preemptive or nonpreemptive


• Preemptive version called shortest-remaining-time-first
• When a new process arrives at the ready queue while a process is still
running
• The next CPU burst of the newly arrived process is shorter than
what is left of the currently running process
• The nonpreemptive SJF algorithm will allow the currently running
process to finish its CPU burst
• The preemptive will preempt the currently running process

Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Example of Preemptive SJF (SRTF)
Arrival time for all processes is 0
ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

• SJF scheduling chart

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

Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Recap

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling/
nonpreemptive

The average waiting time depends on which order processes run

• The Gantt chart for the schedule is:


Avg. waiting time = 17ms

Avg. waiting time = 3ms

• Convoy effect - short process behind long process


• This observation is used in the next algorithm

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Shortest-Job-First (SJF) Scheduling

• The SJF can be either preemptive or nonpreemptive


• Preemptive version called shortest-remaining-time-first
• When a new process arrives at the ready queue while a process is still
running
• The next CPU burst of the newly arrived process is shorter than
what is left of the currently running process
• The nonpreemptive SJF algorithm will allow the currently running
process to finish its CPU burst
• The preemptive will preempt the currently running process

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Example of Non-Preemptive SJF

Process Arrival Time CPU Burst Time(ms)


P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (non-preemptive)

P1 P3 P2 P4

0 7 8 12 16

• W(P1) = 0; W(P2) = 8-2 = 6ms;


• W(P3 ) = 7-4 = 3ms ; W(P4 ) = 12-5 = 7ms

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


Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Example of Preemptive SJF (SRTF)
Arrival time for all processes is 0
ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

• SJF scheduling chart

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

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Example of Shortest-remaining-time-first
• Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• Preemptive SJF Gantt Chart

• P1 start: arrived first at time 0 no other process arrived at that time


• P2 arrived at 1, the remaining time of P1 is 8-1 =7 which is larger than CPU burst of P2.
Therefore, P1 is preempted and P2 starts
• P3 arrived at 2, the CPU time of P3 is 9 which larger than the remaining time of P2
(remaining = 4 -1 =3). Thus, P2 continues
• P4 arrived at 3, the CPU time for P4 = 5, compare it with the remaining time of P2 (2). So,
P2 continues
Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Example of Shortest-remaining-time-first
• Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• Preemptive SJF Gantt Chart

• P2 finishes
• Now there are three processes waiting P1 (7 which 8 -1 it already ran for 1 ms), P3
(9), P4(5)
• The next process to run is P4 it has the shortest CPU time
• After P4, starts P1 has CPU time shorter than P3
• Finally, starts P3

Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Example of Shortest-remaining-time-first

• Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• Preemptive SJF Gantt Chart

• Waiting (p1) = 0 + (10 -1) (it ran the first time for 1 ms)
• Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 ms

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Example of Preemptive SJF (SRTF)
Process Arrival Time CPU Burst Time(ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

• Process P1 is started at time 0, since it is the only process in the queue. Process P2
arrives at time 2. The remaining time for process P1 (5ms) is larger than the time
required by process P2 (4ms), so process P1 is preempted, and process P2 is
scheduled. At time 4 process P3 arrived the remaining time of P2 is larger than CPU
burst for P2, thus P2 is preempted and P3 starts.
• P3 finished at time 5, at this time there are three process waiting P1 (was preempted,
5), P2 (was preempted, 2), and P4 just arrived (4)
• P2 starts it has the shortest CPU burst, then P4 and finally P1
Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Example of Preemptive SJF (SRTF)
Process Arrival Time CPU Burst Time(ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

• W(P1) = (0-0)+(11-2)=9ms;
• W(P2) = (2-2)+(5-4)=1ms;
• W(P3 )= 4-4=0;
• W(P4 )= 7-5=2ms
• Average waiting time = (9 + 1 + 0 +2)/4 = 3ms

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR)

• This scheduling algorithm is designed specially for time sharing systems. It is


similar to FCFS scheduling, but preemption is added to switch between processes.
• 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 ⇒ a lot of context switching overhead
• q must be large with respect to context switch, otherwise overhead is too high

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR)
• In the RR scheduling algorithm,
• no process is allocated the CPU for more than 1 time quantum in a row
(unless it is the only runnable process).
• If a process’s CPU burst exceeds 1 time quantum (need more than one
quantum), that process is preempted and is put back in the ready queue

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

P4 P3 P2 P1

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33
P1 P4 P3 P2 P1

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33
P1 P4 P3 P2

P2 finished in one quantum

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33
P3 P1 P4

48
P3 did not finish

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33
P4 P3 P1

48
P4 did not finish
4

Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33 13
P1 P4 P3

48
P1 did not finish
4
need 33 ms
quantum is 20

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33 13
P3 P1 P4

48 28
P3 did not finish
4
need 48 ms
quantum is 20

Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33 13
P3 P1 P4

48 28
P4 finished
4
remaining 4ms

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33 13
P3 P1

48 28
P1 finished
4
remaining 13ms

Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33 13
P3

48 28 8
P3 did not finish
4
need 28 ms
quantum is 20

Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Example: RR with Time Quantum = 20
• Consider the following set of processes that arrive at time 0 in the order P1,
P2, P3, P4, with the length of the CPU burst given in milliseconds.

33 13
P3

48 28 8
run P3 again,
4 the only one in the
ready queue
now P3 finish

Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 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 ms

Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
The performance of the RR algorithm
• The performance of the RR algorithm depends heavily on the size of the time
quantum.
• At one extreme, if the time quantum is extremely large, the RR policy is the
same as the FCFS policy.
• In contrast, if the time quantum is extremely small (say, 1 millisecond), the RR
approach can result in a large number of context switches

Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Time Quantum and Context Switch Time

• for example, that we have only one process of 10 time


units. If the quantum is 12 time units, the process finishes
in less than 1 time quantum, with no overhead.
• If the quantum is 6 time units, however, the process
requires 2 quanta, resulting in a context switch.
• If the time quantum is 1 time unit, then nine context
switches will occur

Operating System Concepts – 10th Edition 17 Silberschatz, Galvin and Gagne ©2018
Turnaround Time Varies With The Time Quantum

80% of CPU bursts


should be shorter than q

Operating System Concepts – 10th Edition 18 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

DCBA

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

DCBA

A needs 20 ms > q

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 ADCB

A needs 20 ms > q

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 ADCB

B needs 40 ms > q

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 BADC
30

B needs 40 ms > q

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 BADC
30

C needs 14 ms > q

Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CBAD
30

4
C needs 14 ms > q

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CBAD
30

4
D needs 6 ms < q

Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CBAD
30

4 D needs 6 ms < q
D finished

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CBA
30

Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CBA
30

4
A needs 10 ms = q

Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CB
30

4
A finished

Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 CB
30

4
B needs 30 ms > q

Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 BC
30 20

4
B did not finish

Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 BC
30 20

4
C needs 4 ms < q

Operating System Concepts – 10th Edition 17 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 B
30 20

4
C finished

Operating System Concepts – 10th Edition 18 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 B
30 20 10

4
B needs 20 ms > q

Operating System Concepts – 10th Edition 19 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

10 B
30 20 10

4
B is the only one in ready queue, give it the quantum again

Operating System Concepts – 10th Edition 20 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum : 10 ms

Operating System Concepts – 10th Edition 21 Silberschatz, Galvin and Gagne ©2018
Example: RR with Quantum = 10ms
• Consider the following set of processes that arrive at time 0 in the order A,B,C,D
with the following given CPU burst time. Find the average waiting time with RR of
quantum = 10 ms and context switch time = 2ms

CPU

I/O

Operating System Concepts – 10th Edition 22 Silberschatz, Galvin and Gagne ©2018
Chapter 5: CPU
Scheduling

Operating System Concepts – 10th Edition Silberschatz, Galvin and


Gagne ©2018
Priority Scheduling

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

• The CPU is allocated to the process with the highest priority (smallest or
largest integer value may be defined as the highest priority)
• Preemptive
• Nonpreemptive

• 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

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Example of Priority Scheduling
The smaller the Priority number the higher the priority
ProcessAarri Burst TimeT Priority
P1 10 3
P2 1 1 highest
P3 2 4
P4 1 5 lowest
P5 5 2

• Priority scheduling Gantt Chart

• Average waiting time = 8.2 msec

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Example - Priority Scheduling

E arrived at time 80, while


A is running, preemptive
So A preempted

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Priority Scheduling w/ Round-Robin
ProcessAarri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1 Highest priority
P5 3 3
• Run the process with the highest priority. Processes with the same priority run
round-robin

• Gantt Chart with 2 ms time quantum

p3

18
P2 and P3 have same priority
alternate execution using RR the quantum
same for P1 and P5
Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue
• With priority scheduling, have separate queues for each priority.
• Schedule the process in the highest-priority queue!

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue
• a common division is made between foreground (interactive) processes and
background (batch) processes. These two types of processes have different
response-time requirements and so may have different scheduling needs.
• Separate queues might be used for foreground and background processes,
and each queue might have its own scheduling algorithm.
• The foreground queue might be scheduled by an RR algorithm, for
example,
• while the background queue is scheduled by an FCFS algorithm.

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue
• Prioritization based upon process type

Each queue has absolute priority over lower-priority queues. No


process in the batch queue, for example, could run unless the
queues for real-time processes, system processes, and interactive
processes were all empty. If an interactive process entered the
ready queue while a batch process was running, the batch
process would be preempted.
Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
queue1: q=4
queue2: q=3

Queue1 (priority 1):

Queue2 (priority 2):

Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
queue1: q=4
queue2: q=3

Queue1 (priority 1):

Queue2 (priority 2): A

Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
queue1: q=4
queue2: q=3

Queue1 (priority 1):

Queue2 (priority 2): A

Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 queue1: q=4
queue2: q=3

Queue1 (priority 1): B

Queue2 (priority 2): A

Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 queue1: q=4
queue2: q=3

Queue1 (priority 1): B

Queue2 (priority 2): CA C arrived to queue 2


B has higher priority

Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 queue1: q=4
queue2: q=3

Queue1 (priority 1): B B finished

Queue2 (priority 2): CA

Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 queue1: q=4
queue2: q=3

Queue1 (priority 1): B B finished

Queue2 (priority 2): CA at time 10,


we have 2 processes in queue2

Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 queue1: q=4
queue2: q=3

Queue1 (priority 1): D

Queue2 (priority 2): CA at time 12, D arrived


has higher priority

Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3

Queue1 (priority 1): D at time 12, D arrived


has higher priority
Queue2 (priority 2): A C preempt A, start D

Operating System Concepts – 10th Edition 17 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3

Queue1 (priority 1): D at time16, D finished the quantum


there is no other process in queue 1
Queue2 (priority 2): A C so give D another quantum

Operating System Concepts – 10th Edition 18 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3

Queue1 (priority 1): D D finished at time 17

Queue2 (priority 2): A C

Operating System Concepts – 10th Edition 19 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3

Queue1 (priority 1): E


run C from 17 to 18
Queue2 (priority 2): A C at time 18 E arrived
E has higher priority

Operating System Concepts – 10th Edition 20 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3
5

Queue1 (priority 1): E


Preempt C,
Queue2 (priority 2): C A put again in the queue 2
run E

Operating System Concepts – 10th Edition 21 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3
5

Queue1 (priority 1): E


E, did not finish in the
Queue2 (priority 2): C A first quantum,
no other process, so give
E a second quantum

Operating System Concepts – 10th Edition 22 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3
5

Queue1 (priority 1):


E finished
Queue2 (priority 2): C A no process in queue 1
so pick process from queue 2

Operating System Concepts – 10th Edition 23 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 queue1: q=4
queue2: q=3
5

Queue1 (priority 1):


we have 2 processes in queue1
Queue2 (priority 2): C A

Operating System Concepts – 10th Edition 24 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 2 queue1: q=4
queue2: q=3
5

Queue1 (priority 1):

Queue2 (priority 2): C A

Operating System Concepts – 10th Edition 25 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 2 queue1: q=4
queue2: q=3
5 2

Queue1 (priority 1):

Queue2 (priority 2): C A

Operating System Concepts – 10th Edition 26 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 2 queue1: q=4
queue2: q=3
5 2

Queue1 (priority 1):

Queue2 (priority 2): C A A finished

Operating System Concepts – 10th Edition 27 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling
7 5 2 queue1: q=4
queue2: q=3
5 2

Queue1 (priority 1):

Queue2 (priority 2): C A C finished

Operating System Concepts – 10th Edition 28 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue Scheduling

W(A) = (0 - 0) + (10 - 3) + (26 - 12) + (32 - 29) = 0 + 7 + 14 + 3 = 24ms


W(B) = (3 - 3) + (7 - 7) = 0, W(C) = (17-4) +(29-18)+(34-32) = 13+11+2=26
W(D) = (12-12) +(16-16) = 0
W(E) = (18-18) + (22-22) = 0

Operating System Concepts – 10th Edition 29 Silberschatz, Galvin and Gagne ©2018
Multilevel Feedback Queue
• Normally, when the multilevel queue scheduling algorithm is used, processes are
permanently assigned to a queue when they enter the system.
• The multilevel feedback queue scheduling algorithm allows a process to move
between queues.
• The scheduler first executes all processes in Queue 1. Only when Queue 1 is empty
will it execute processes in Queue 2. Similarly, processes in Queue 3 will be
executed only if Queues 1 and 2 are empty.
• A process that arrives for Queue 1 will preempt a process in Queue 2.
• this might cause starvation for processes in low priority queue
• A process entering the ready queue is put in Queue 1. If it does not finish within one
quantum, it is moved to the tail of Queue 2. If it does not complete in one quantum,
it is then preempted and put into Queue 3.

Operating System Concepts – 10th Edition 30 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 31 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 32 Silberschatz, Galvin and Gagne ©2018
Algorithm Evaluation
• How to select CPU-scheduling algorithm for an OS?
• Determine criteria, then evaluate algorithms
• Deterministic modeling
• Type of analytic evaluation
• Takes a particular predetermined workload and defines the
performance of each algorithm for that workload
• Consider 5 processes arriving at time 0:

Operating System Concepts – 10th Edition 33 Silberschatz, Galvin and Gagne ©2018
Deterministic Evaluation

• For each algorithm, calculate minimum average waiting time


• Simple and fast, but requires exact numbers for input, applies only to
those inputs
• FCFS is 28ms:

• Non-preemptive SJF is 13ms:

• RR is 23ms:

Operating System Concepts – 10th Edition 34 Silberschatz, Galvin and Gagne ©2018
End of Chapter 5

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

You might also like