0% found this document useful (0 votes)
3 views

IT 106 UNIT 2 Lesson 2 CPU Scheduling Algorithm

The document discusses CPU scheduling, which is essential for multiprogrammed operating systems to maximize CPU utilization and efficiency. It outlines the types of schedulers (long-term, medium-term, and short-term), their functions, and the conditions under which scheduling decisions are made. Additionally, it describes various scheduling algorithms, including First-Come, First-Served and Shortest-Job-First, along with their advantages and disadvantages.

Uploaded by

Son Chaeyoung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

IT 106 UNIT 2 Lesson 2 CPU Scheduling Algorithm

The document discusses CPU scheduling, which is essential for multiprogrammed operating systems to maximize CPU utilization and efficiency. It outlines the types of schedulers (long-term, medium-term, and short-term), their functions, and the conditions under which scheduling decisions are made. Additionally, it describes various scheduling algorithms, including First-Come, First-Served and Shortest-Job-First, along with their advantages and disadvantages.

Uploaded by

Son Chaeyoung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

CPU Scheduling and

CPU Scheduling Algorithm


CPU SCHEDULING

➢ Scheduler – is a part of the operating system that


carries out the scheduling activity. Schedulers are
often implemented so they keep all computer
resources busy (as in load balancing), allow
multiple users to share system resources
effectively, or to achieve a target quality of service.
➢ CPU Scheduling – is the basis of multiprogrammed
operating systems. By switching the CPU among
processes, the operating system can make the
computer more productive.
BASIC CONCEPTS OF CPU SCHEDULING
▪ Almost all programs have some alternating cycle of CPU number
crunching and waiting for I/O of some kind. (Even a simple fetch
from memory takes a long time relative to CPU speeds. )
▪ In a simple system running a single process, the time spent
waiting for I/O is wasted, and those CPU cycles are lost forever.
▪ A scheduling system allows one process to use the CPU while
another is waiting for I/O, thereby making full use of otherwise
lost CPU cycles.
▪ The challenge is to make the overall system as “efficient” and
“fair” as possible, subject to varying and often dynamic
conditions, and where “efficient” and “fair” are somewhat
subjective terms, often subject to shifting priority policies.
TYPES OF SCHEDULERS

1. Long-term Scheduler – it is responsible for selecting


processes from the secondary storage and loads them
into memory for execution. It is in charge with the
admission of jobs into the system. It is also known as Job
Scheduler/ Admission Schedule.
The long term, or admission scheduler, decides which
jobs or processes are to be admitted to the ready queue
(in the Main Memory); that is, when an attempt is made
to execute a program, its admission to the set of
currently executing processes is either authorized or
delayed by the long-term scheduler.
TYPES OF SCHEDULERS

2. Medium-term Scheduler – Also called as the swapper,


it swaps processes in and out of the memory. The
swapper suspends a process in memory by temporarily
removing it from memory and transferring it in a
backing store. It then replaces the swapped out process
with another job from the secondary storage
This scheduler temporarily removes processes from
main memory and places them on secondary
memory(such as a disk drive) or vice versa. This is
commonly referred to as “swapping out” or swapping
in”.
TYPES OF SCHEDULERS

2. Medium-term Scheduler
The medium-term scheduler may decide to
swap out a process (a) which has not been active
for some time, (b) or a process which has a low
priority, (c) or a process which is taking up a large
amount of memory in order to free up main
memory for other processes.
TYPES OF SCHEDULERS

3. Short-term Scheduler – it selects process from among


the processes that are ready to execute and allocates the
CPU to one of them. It is also called the CPU scheduler. It
must select a new process from the memory to be fed into
the CPU. A process may execute for only a few milliseconds
before waiting for an I/O request.
Due to the brief time frame between executions, the
short-term scheduler must be very fast to keep the CPU
form getting idle. This scheduler can be:
▪ preemptive
▪ non-preemptive
HOW SCHEDULER WORKS
CPU SCHEDULING
CPU scheduling decisions take place under one of four
conditions:
1.When a process switches from the running state to the
winning state.
2.When a process switches from the running state to the ready
state.
3.When a process switches from the waiting state to the ready
state.
4.When a process terminates.
➢For conditions 1 and 4 there is no choice – A new process
must be selected.
➢For conditions 2 and 3 there is a choice – To either continue
running the current process, or select a different one.
CPU SCHEDULING
▪ If scheduling takes place only under conditions 1 and 4,
the system is said to be non-preemptive, or cooperative.
Under these conditions, once a process starts running it
keeps running, until it either voluntarily blocks or until it
finishes. Otherwise the system is said to be preemptive.
➢ Non-preemptive scheduler – is capable of forcibly
removing processes from a CPU when it decides to allocate
that CPU to another process. Process remains scheduled
until voluntary relinquishes CPU.
➢ Pre-emptive scheduler – is unable to “force” processes
off the CPU. Process may be rescheduled at any time.
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.
HOW DISPATCHER WORKS
SCHEDULING CRITERIA
▪ There are several different criteria to consider when trying
to select the “best” scheduling algorithm for a particular
situation and environment, including:
1. CPU utilization – This measures how busy is the CPU.
Ideally the CPU would be busy 100% of the time, so as
to waste zero CPU cycles. On a real system CPU usage
should range from 40% (lightly loaded) to 90%(heavily
loaded.)
2. Throughput – This is a measure of work, the number
of processes completed per unit time. For long
processes, this rate may be one process per hour; for
short transactions, it might be 10 processes per second.
SCHEDULING CRITERIA
3. Turnaround time – From the point of view of a particular
process, the important criterion is how long it takes to
execute the process. The interval from the time of
submission of a process to the time of completion is the
turnaround time. It is the sum of the periods spent waiting to
get into memory, waiting in the ready queue, executing in
the CPU, and doing I/O.
4. Waiting time – The CPU scheduling algorithm does not
affect the amount of time during which a process executes or
does I/O; it affects only the amount of time that a process
spends waiting in the ready queue. The waiting time
measures how much time processes spend in the ready
queue waiting their turn to get on the CPU.
SCHEDULING CRITERIA

5. Response time – In an interactive system, turnaround


time may not be the best criterion. Often, a process can
produce some output fairly easy, and can continue
computing new results while previous results are being
output to the user. Thus, another measure is the time
from the submission of a request until the first response
is produced. This measure, called response time, is the
time that it takes to start responding, but not the time
that it takes to output that response. The turnaround
time is generally limited by the speed of output device.
SCHEDULING ALGORITHMS

CPU scheduling deals with the problem of deciding


which of the processes is the ready queue is to be
allocated the CPU.
Scheduling Algorithms

Shortest
First-Come, Shortest-Job- Remaining
First-Served First (SJF) Time First
(FCFS) (SRTF)

Priority Preemptive Round


Scheduling Priority Robin
(P-Prio) (RR)
SCHEDULING ALGORITHMS

1. First-Come, First-Served Scheduling (FCFS) – is the


simplest CPU scheduling algorithm. With this
scheme, the process that requests the CPU first is
allocated the CPU first. The implementation of the
FCFS policy is 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. The running process is then removed from
the queue.
Representation of First-Come, First-Served
SCHEDULING ALGORITHMS

▪ The First-Come, First-Served scheduling algorithm is


a non-preemptive scheme. Once the CPU has been
allocated to a process, that process keeps the CPU
until it releases the CPU, either by terminating or by
requesting I/O. The FCFS algorithm is particularly
troublesome for time-sharing systems, where it is
important that each user get a share of the CPU at
regular intervals. It would be disastrous to allow one
process to keep the CPU for an extended period.
SCHEDULING ALGORITHMS
❖ Advantage of FCFS:
• Very simple implementation
• Fair for process that arrives first
❖ Disadvantage of FCFS:
• Waiting time depends on arrival order
• Potentially long wait for jobs that arrive later
• Convoy effect
▪ Convoy effect is a situation when jobs with
smaller burst have to wait for long job to finish
its long burst.
SCHEDULING ALGORITHMS
▪ Convoy effect is something that is quite annoying when smaller
jobs are of higher priorities than the big-burst job that is
occupying the processor. Since the CPU is tied to a job with a
very long burst, jobs with smaller have no choice but to wait.
First-Come, First-Served Scheduling (FCFS)
Example:
Consider the following set of processes that arrive at time 0,
with the length of the CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3

Supposed that the processes arrive in the order: P1, P2 and P3


The Gantt Chart for the schedule is:
P1 P2 P3

0 24 27 30
First-Come, First-Served Scheduling (FCFS)
Example: cont...
Therefore the waiting time(WT) for each process is:
WT for P1 0-0 =0 Waiting time =
Start time –
WT for P2 24-0 =24 Arrival time
WT for P3 27-0 =27
Average Waiting Time = (0 + 24 + 27)/3 = 17 ms
The turnaround time(TT) for each process would be:
TT for P1 24-0 =24 Turnaround Time
TT for P2 27-0 =27 = Exit Time –
Arrival Time
TT for P3 30-0 =30
Average Turnaround Time = (24 + 27 + 30)/3 = 27 ms
First-Come, First-Served Scheduling (FCFS)
Example:
Supposed that the processes arrive in the order: P2, P3 and P1
The Gantt Chart for the schedule is:
P2 P3 P1

0 3 6 30

Therefore the waiting time(WT) for each process is:


WT for P1 6-0 =6
WT for P2 0-0 =0
WT for P3 3-0 =3
Average Waiting Time = (6 + 3 + 0)/3 = 3 ms
First-Come, First-Served Scheduling (FCFS)

✓ NOTE:
Comparison of the two computations of
waiting time reveals that if the smaller burst jobs
get processed first, then the average wait by each
job is lessened. But because FCFS is a non-
preemptive algorithm, the computation in the
given example must be done using the sequence
P1, P2, P3 not P2, P3, P1.
First-Come, First-Served Scheduling (FCFS)
Here is another example of five processes arriving at different times. Each process
has a different burst time.
Process Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 0) The process begins with P4 which has arrival time 0


First-Come, First-Served Scheduling (FCFS)
Step 1) At time=1, P3 arrives. P4 is still executing.
Hence, P3 is kept in a queue.
First-Come, First-Served Scheduling (FCFS)
Step 2) At time= 2, P1 arrives which is kept in the
queue.
First-Come, First-Served Scheduling (FCFS)
Step 3) At time=3, P4 process completes its
execution.

Step 4) At time=4, P3, which is first in the queue,


starts execution.
First-Come, First-Served Scheduling (FCFS)
Step 5) At time =5, P2 arrives, and it is kept in a queue.

Step 6) At time 11, P3 completes its execution.


First-Come, First-Served Scheduling (FCFS)
Step 7) At time=11, P1 starts execution. It has a burst time of 6. It
completes execution at time interval 17

Step 8) At time=17, P5 starts execution. It has a burst time of 4. It


completes execution at time=21
First-Come, First-Served Scheduling (FCFS)
Step 9) At time=21, P2 starts execution. It has a burst time of 2. It
completes execution at time interval 23

Step 10) Let's calculate the average waiting time for above example.
First-Come, First-Served Scheduling (FCFS)

Waiting time = Start time - Arrival time


Process Burst time Arrival time
P4 = 0-0=0 P1 6 2
P3 = 3-1=2 P2 2 5
P1 = 11-2=9 P3 8 1
P5 = 17-4=13 P4 3 0
P2 = 21-5=16 P5 4 4

Average Waiting Time = 0+2+9+13+16 = 40/5 = 8


SCHEDULING ALGORITHMS

2. Shortest-Job-First Scheduling (SJF) – the idea behind


the SJF algorithm is to pick the quickest fastest little job
that needs to be done, get it out of the way first, and then
pick the next smallest fastest job to do next.
The different approach to CPU scheduling is the
shortest-job-first(SJF) scheduling algorithm. This algorithm
associates with each process the length of the process’s
next CPU burst.
When the CPU is available, it is assigned to the process
that has the smallest next CPU burst. If the next CPU burst
of two processes are the same, FCFS scheduling is used to
break the tie.
Representation of Shortest-Job-First Algorithm
Shortest-Job-First Scheduling Algorithm
▪ The SJF scheduling algorithm is probably optimal, in that
it gives the minimum average waiting time for a given set
of processes. By moving a short process before a long one
the waiting time of the short process decrease more than it
increases the waiting time of the long process.
Consequently, the average waiting time decreases.
❖ Advantage of SJF:
• Shortest jobs finish first
• Low Priority process with shortest burst time are
still the one to be executed first by the CPU
❖ Disadvantage of SJF:
• Not fair for long jobs that arrives early
Shortest-Job-First Scheduling Algorithm
Example:
Consider the following set of processes that arrive at time 0,
with the length of the CPU burst given in milliseconds:
Process Burst Time
P1 6
P2 8
P3 7
P4 3

The Gantt Chart for the schedule is:


P4 P1 P3 P2

0 3 9 16 24
Shortest-Job-First Scheduling Algorithm
Example: cont...
Therefore the waiting time(WT) for each process is:
WT for P1 3-0 =3 Note:
WT for P2 16-0 =16 If two processes
WT for P3 9-0 =9 have the same
_
length of CPU
WT for P4 0-0 =0 burst, FCFS
Average Waiting Time = (3 + 16 + 9 + 0)/4 = 7 ms scheduling is used
The turnaround time(TT) for each process would be: to break the tie by
considering which
TT for P1 9-0 =9
job arrived first.
TT for P2 24-0 =24
TT for P3 16-0 =16
TT for P4 0-0 =0
Average Turnaround Time = (9 + 24 + 16 + 0)/4 = 12.25 ms
Shortest-Job-First Scheduling Algorithm
Non-Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to process, the
process holds it till it reaches a waiting state or terminated.
Process Queue Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 0) At time=0, Process 4 arrives and starts execution.


Shortest-Job-First Scheduling Algorithm
Step 1) At time=1, Process P3 arrives. But, P4 still needs to
complete. It will continue execution.

Proces Burst Arrival


s time time
Queue
P1 6 2
Step 2) At time=2, Process P1 arrives and is added to the waiting P2 2 5
queue. P4 will continue execution. P3 8 1
P4 3 0
P5 4 4
Shortest-Job-First Scheduling Algorithm

Step 3) At time= 3, P4 will finish its execution. The burst time of P3


and P1 is compared. Process P1 is executed because its burst time
is less compared to P3.
Proces Burst Arrival
s time time
Queue
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Shortest-Job-First Scheduling Algorithm
Step 4) At time=4, Process P5 arrives and is added to the waiting
queue. P1 will continue execution.

Proces Burst Arrival


s time time
Queue
P1 6 2
P2 2 5
Step 5) At time=5, Process P2 arrives and is added to the waiting
queue. P1 will continue execution. P3 8 1
P4 3 0
P5 4 4
Shortest-Job-First Scheduling Algorithm
Step 6) At time=9, Process P1 will finish its execution. The burst
time of P3, P5, and P2 are compared. Process P2 is executed
because its burst time is the lowest.

Proces Burst Arrival


s time time
Queue
P1 6 2
P2 2 5
Step 7) At time=10, P2 is executing and P3 and P5 are in the
waiting queue. P3 8 1
P4 3 0
P5 4 4
Shortest-Job-First Scheduling Algorithm

Step 9) At time=15, Process P5 will finish its execution.

Proces Burst Arrival


s time time
Queue
P1 6 2
P2 2 5

Step 10) At time=23, Process P3 will finish its execution. P3 8 1


P4 3 0
P5 4 4
Shortest-Job-First Scheduling Algorithm

Step 11) Let’s calculate the average waiting time for the given example.
Waiting time = Start time - Arrival time
Wait time Process Queue Burst time Arrival time
P4 = 0-0=0
P1 = 3-2=1 P1 6 2

P2 = 9-5=4 P2 2 5

P5 = 11-4=7 P3 8 1

P3 = 15-1=14 P4 3 0
P5 4 4

Average Waiting Time = 0+1+4+7+14/5 = 26/5 = 5.2


SCHEDULING ALGORITHMS

3. Shortest Remaining Time First (SRTF) – It is also


called as Preemptive SJF scheduling algorithm
(PSJF). A new process arriving may have shorter
next CPU burst than what is left of the current
executing process. A pre-emptive SJF or SRTF
algorithm will preempt the currently executing
process, whereas a nonpreemptive SJF algorithm
will allow the currently running process to finish
its CPU burst.
Representation of SRTF/PSJF Algorithm
Shortest Remaining Time First(SRTF)/(PSJF)

❖ Advantage of SRTF:
• Shortest remaining time finish first
• Minimizes average waiting times

❖ Disadvantage of SRTF:
• Not fair for long jobs that arrives early
• Low priority process with shortest burst
time are still the one to be executed first by
the CPU
• Starvation of jobs with long CPU bursts.
Shortest Remaining Time First(SRTF)/(PSJF)
Example:
Consider the following four processes, with the length of the
CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 1
P4 3 5

Preemptive SJF Gantt Chart:


P1 P2 P3 P2 P4 P1

0 1 2 3 6 11 18
Shortest Remaining Time First(SRTF)/(PSJF)
Example: cont...
Therefore the waiting time(WT) for each process is:
WT for P1 11-0-(1) =10
WT for P2 3-1-(1) =1 Waiting time =
Start time –
_
WT for P3 2-2 =0 Arrival time

WT for P4 6-3 =3
Average Waiting Time = (10 + 1 + 0 +3)/4 = 3.5 ms
The turnaround time(TT) for each process would be:
TT for P1 18-0 =18 Turnaround Time
TT for P2 6-1 =5 = Exit Time –
Arrival Time
TT for P3 3-2 =1
TT for P4 11-3 =8
Average Turnaround Time = (18 + 5 + 1 + 8)/4 = 8 ms
Shortest Remaining Time First(SRTF)/(PSJF)
Another Example:
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

SJF Gantt Chart (Non-Preemptive)


P1 P3 P2 P4

0 3 7 8 12 16

Average Waiting Time = (0 + 6 + 3 +7)/4 = 4 ms


Shortest Remaining Time First(SRTF)/(PSJF)
Another Example:
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

SRTF Gantt Chart (Preemptive-SJF)


P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

Average Waiting Time = (9 + 1 + 0 +2)/4 = 3 ms


SCHEDULING ALGORITHMS
4. Priority Scheduling –is a more general case of SJF, in
which each job is assigned a priority and the job with the
highest priority gets scheduled first (SJF uses the inverse of
the next expected burst time as its priority – the smaller the
expected burst, the higher the priority).
Note that in practice, priorities are implemented using
integers within a fixed range, but there is no agreed-upon
convention as to whether “high” priorities use large numbers
or small numbers. We will be using low number for high
priorities, with 0 being the highest possible priority.
A Priority number (integer) is associated with each
process. The CPU is allocated to the process with the highest
priority (smallest integer = highest priority). Equal priority is
scheduled in FCFS order.
Representation of Priority Scheduling
PRIORITY SCHEDULING

Priority scheduling can be either preemptive or


nonpreemptive. When a process arrives at the ready
queue, its priority is compared with the priority of the
currently running process. A preemptive priority
scheduling algorithm will prevent the CPU if the
priority of the newly arrived process is higher than
the priority of the currently running process. A
nonpreemptive priority-scheduling algorithm will
simply put the new process at the head of the ready
queue.
PRIORITY SCHEDULING

❖Advantage of Priority:
• Jobs with high priority will be executed first

❖Disadvantage of Priority:
• Low priority process will be the last to be
executed
• Not fair low priority process that arrives early.
Conceptual and Implementation view of Priority Scheduling
PRIORITY SCHEDULING
Example:
Consider the following example:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

Priority Scheduling Gantt Chart:


P2 P5 P1 P3 P4

0 1 6 16 18 19
PRIORITY SCHEDULING
Example: cont... Therefore the waiting time(WT) for each process is:
WT for P1 6-0 =6
WT for P2 0-0 =0
WT for P3 16-0 =16
WT for P4 18-0 =18
WT for P5 1-0 =1
Average Waiting Time = (6 + 0 + 16 +18 + 1)/5 = 8.2 ms
The turnaround time(TT) for each process would be:
TT for P1 16-0 =16
TT for P2 1-0 =1
TT for P3 18-0 =18
TT for P4 19-0 =19
TT for P5 6-0 =6
Average Turnaround Time = (16 + 1 + 18 + 19 + 6)/5 = 12 ms
SCHEDULING ALGORITHMS
5. Preemptive Priority (P-Prio) Scheduling Algorithm –
will preempt the CPU if the priority of the newly arrived
process is higher than the current running process.
A major problem with priority scheduling algorithm
is indefinite blocking or starvation. A process that is
ready to run but lacking the CPU can be considered
blocked, waiting for the CPU. A priority-scheduling
algorithm can leave some low-priority processes
waiting indefinitely for the CPU. In a heavily loaded
computer system, a steady stream of higher-priority
processes can prevent a low-priority process from
ever getting the CPU.
PREEMPTIVE PRIORITY (P-PRIO)
Generally, one of two things will happen. Either the
process will eventually be run, or the computer system will
eventually crash and lose all unfinished low-priority
processes.
A solution to the problem of indefinite blockage of low-
priority processes is aging. Aging is a technique of gradually
increasing the priority of processes that wait in the system for
a long time. For example, if priorities range from 127 to 0, we
could increment the priority of a waiting process by 1 every
15 minutes. Eventually, even a process with an initial priority
of 127 would have the highest priority in the system and
would be executed. In fact, it would take no more than 32
hours for a priority 127 process to age to priority 0 process.
PREEMPTIVE PRIORITY (P-PRIO)

❖Advantage of P-Prio:
• Jobs with high priority will be executed first
• Jobs with a low priority will be replaced by
new process with a higher priority
❖Disadvantage of P-Prio:
• Not fair for low priority process that arrives
early.
• Indefinite blocking/starvation
• Solution: Aging
PREEMPTIVE PRIORITY (P-PRIO)
Example:
Consider the following example:
Process Arrival Time Burst Time Priority
P1 1 5 5
P2 2 10 4
P3 3 18 3
P4 4 7 2
P5 5 3 1

P-Prio Scheduling Gantt Chart:


PRIORITY SCHEDULING
Example: cont... Therefore the waiting time(WT) for each process is:
WT for P1 40-1-(1) =38
WT for P2 31-2-(1) =28
WT for P3 14-3-(1) =10
WT for P4 8-4-(1) =3
WT for P5 5-5 =0
Average Waiting Time = (38 + 28 + 10 +3 + 0)/5 = 15.8 ms
The turnaround time(TT) for each process would be:
TT for P1 44-1 =43
TT for P2 40-2 =38
TT for P3 31-3 =28
TT for P4 14-4 =10
TT for P5 8-5 =3
Average Turnaround Time = (43 + 38 + 28 + 10 + 3)/5 = 24.4 ms
SCHEDULING ALGORITHMS
6. Round Robin Scheduling – The round-robin (RR)
scheduling algorithm is designed especially for the time –
sharing systems. It is similar to FCFS scheduling but
preemption is added to switch between processes. A small
unit of time, called a time quantum, or time slice, is
defined. A time quantum s generally from 10 to 100
milliseconds. The ready queue is treated as a circular
queue. The CPU scheduler goes around the ready queue,
allocating the CPU o each process for a time interval of up
to 1 time quantum.
To implement RR scheduling we keep the ready queue
as a FIFO queue of processes. New process are added to
the tail of the ready queue.
ROUND ROBIN SCHEDULING

The CPU scheduler picks the first process from the ready
queue, sets a timer to interrupt after 1 time quantum, and
dispatches the process.
One of two things will then happen. The process may
have a CPU burst of less than 1 time quantum. In this case,
the process itself will release the CPU voluntarily. The
scheduler will then proceed to the next process in the ready
queue. Otherwise, if the CPU burst of the currently running
process is longer than 1 time quantum, the timer will go off
and will cause an interrupt to the operating system. A context
switch will be executed, and the process will be put at the tail
of the ready queue. The CPU scheduler will then select the
next process in the ready queue.
Representation of Round Robin Scheduling
ROUND ROBIN SCHEDULING
If there are n processes in the ready queue and the time
quantum is q,
o Each process gets1/n of the CPU time in chunks of at
most q time units at once.
oNo process waits more than (n-1)q time units until its
next time quantum.
• Example: If there are 5 processes, with a time
quantum of 20 ms, then each process will get up
to 20 ms every 100 ms.

o Typically, RR has higher average turnaround than SJF,


but better response, the average waiting time under the
RR policy, however, is often long.
ROUND ROBIN SCHEDULING

The performance of the Round Robin


algorithm depends heavily on the size of the
quantum. At one extreme, if the quantum is
extremely large, the RR policy is the same as the
FCFS policy. If the time quantum is extremely
small (say 1 millisecond), the RR approach is
called processor sharing and appears (in theory)
to users as though each of n process has its own
processor running at 1/n the speed of the real
processor.
ROUND ROBIN SCHEDULING
Turnaround time also depends on the
size of the time quantum. The average
turnaround time of a set of processes does not
necessarily improve as the time-quantum size
increases. In general, the average turnaround
time can be improved if most processes finish
their next CPU burst in a single time quantum.
For example, given three processes of 10 time
units each and a quantum of 1 time, the average
turnaround time drops to 20. If context switches
will be required.
Turnaround time varies with the time quantum
On the other hand, if the time quantum is too large,
RR scheduling degenerates to FCFS policy. A rule of thumb is
that 80 percent of the CPU burst should be shorter than the
time quantum.
ROUND ROBIN SCHEDULING

❖Advantage of RR:
• Jobs get fair share of CPU (no need to
schedule by priority, or by burst time)
• Most fair CPU scheduling algorithm
• Shortest jobs finish relatively quickly
❖Disadvantage of RR:
• Poor average waiting time with similar job
lengths
• Performance depends on length of time-
slice
ROUND ROBIN SCHEDULING
Example of Round-robin Scheduling
Consider this following three processes

Process Queue Burst time


P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 1) The execution begins with process P1, which has burst time 4.
Here, every process executes for 2 seconds. P2 and P3 are still in the
waiting queue.

Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 2) At time =2, P1 is added to the end of the Queue and P2


starts executing

Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 3) At time=4 , P2 is preempted and add at the end of the


queue. P3 starts executing.

Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 4) At time=6 , P3 is preempted and add at the end of the


queue. P1 starts executing.

Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 5) At time=8 , P1 has a burst time of 4. It has completed


execution. P2 starts execution

Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 6) P2 has a burst time of 3. It has already executed for 2


interval. At time=9, P2 completes execution. Then, P3 starts
execution till it completes.

Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING

Step 7) Let’s calculate the average waiting time for the Process Burst
Queue time
given example.
P1 4
P2 3

Waiting Time = TT – BT Turnaround Time = ET -AT P3 5

P1 = 8-4 = 4 P1 = 8-0 = 8
P2 = 9-3 = 6 P2 = 9-0 = 9
P3 = 12-5 = 7 P3 = 12-0 =12
ROUND ROBIN SCHEDULING
Example:
Consider the following example:
Process Burst Time
P1 53
P2 17
P3 68
P4 24

If the system uses a time quantum of 20ms, then the


resulting RR Gantt Chart is:
ROUND ROBIN SCHEDULING
Example: cont... Waiting Time = Turnaround Time – Burst Time
Therefore the waiting time(WT) for each process is:
WT for P1 134-53 =81
WT for P2 37-17 =20
_
WT for P3 162-68 =94
WT for P4 121-24 =97
Average Waiting Time = (81 + 20 + 94 +97)/4 = 73 ms
The turnaround time(TT) for each process would be:
Turnaround Time = Exit Time – Arrival Time
TT for P1 134-0 =134
TT for P2 37-0 =37
TT for P3 162-0 =162
TT for P4 121-0 =121
Average Turnaround Time = (134 + 37 + 162 + 121)/4 = 113.5 ms
This is not the End

You might also like