Scheduling Algorithm: Fifo-Rr-Sjf-P-Mlq-Mlfq

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Scheduling Algorithm

FIFO-RR-SJF-P-MLQ-MLFQ
1) Explain different types of scheduling
algorithms.
Learning
Outcome 2) Differentiate between preemptive and non-
preemptive technique in scheduling.
 The CPU scheduler selects from among the processes in memory that
are ready to execute and allocates the CPU to one of them
 CPU scheduling is affected by the following set of circumstances:
1. (N) A process switches from running to waiting state
2. (P) A process switches from running to ready state
3. (P) A process switches from waiting to ready state
4. (N) A processes switches from running to terminated state
 Circumstances 1 and 4 are non-preemptive; they offer no schedule
choice
CPU Scheduler  Circumstances 2 and 3 are pre-emptive; they can be scheduled
 The 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
Dispatcher program
 The dispatcher needs to run as fast as possible, since it is invoked
during process context switch
 The time it takes for the dispatcher to stop one process and start another
process is called dispatch latency
 Different CPU scheduling algorithms have different properties
 The choice of a particular algorithm may favor one class of processes
over another
 In choosing which algorithm to use, the properties of the various
algorithms should be considered
 Criteria for comparing CPU scheduling algorithms may include the
following
 CPU utilization – percent of time that the CPU is busy executing
a process
Scheduling  Throughput – number of processes that are completed per time
unit
Criteria  Response time – amount of time it takes from when a request was
submitted until the first response occurs (but not the time it takes
to output the entire response)
 Waiting time – the amount of time before a process starts after
first entering the ready queue (or the sum of the amount of time a
process has spent waiting in the ready queue)
 Turnaround time – amount of time to execute a particular
process from the time of submission through the time of
completion
Single  First Come, First Served (FCFS)/ First In First Out (FIFO)

Processor  Shortest Job First (SJF)


 Priority (P)
Scheduling  Round Robin (RR)
Algorithms
 Step 1 : Draw a Gantt Chart.
Calculate Average  Step 2 : Determine waiting time (WT) for each process
Waiting Time (AWT)
 Step 3 : Calculate average waiting time (AWT)
& Average
Turnaround Time  Step 4 : Determine turnaround time (TT) for each process
(ATT)  Step 5 : Calculate average turnaround time (ATT)
Process Burst Time
P1 24
P2 3
P3 3
 With FCFS, the process that requests the CPU first is allocated the CPU
first
 Case #1: Suppose that the processes arrive in the order: P1 , P2 ,
P3

FCFS/FIFO 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
 Average turn-around time: (24 + 27 + 30)/3 = 27
 Case #2: 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
FCFS/FIFO  Waiting time for P1 = 6; P2 = 0; P3 = 3

Scheduling
 Average waiting time: (6 + 0 + 3)/3 = 3 (Much better than
Case #1)
 Average turn-around time: (3 + 6 + 30)/3 = 13
(Cont.)  Case #1 is an example of the convoy effect; all the other processes wait for
one long-running process to finish using the CPU
 This problem results in lower CPU and device utilization; Case #2
shows that higher utilization might be possible if the short processes
were allowed to run first
 The FCFS scheduling algorithm is non-preemptive
 Once the CPU has been allocated to a process, that process keeps the
CPU until it releases it either by terminating or by requesting I/O
 It is a troublesome algorithm for time-sharing systems
 In the round robin algorithm, each process gets a small unit of CPU
time (a time quantum), usually 10-100 milliseconds. After this time has
elapsed, the process is preempted and added to the end of the ready
queue.
 If there are n processes in the ready queue and the time quantum is q,

Round Robin 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.

(RR)  Performance of the round robin algorithm


 q large  FCFS
Scheduling  q small  q must be greater than the context switch time;
otherwise, the overhead is too high
 One rule of thumb is that 80% of the CPU bursts should be shorter than
the time quantum
Process Burst Time
P1 53
P2 17
P3 68
P4 24
 The Gantt chart is:
Example of RR P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

with Time 0 20 37 57 77 97 117 121 134 154 162

Quantum = 20  Typically, higher average turnaround than SJF, but better response time
 Average waiting time
= ( [(0 – 0) + (77 - 20) + (121 – 97)] + (20 – 0) + [(37 – 0) + (97 -
57) + (134 – 117)] + [(57 – 0) + (117 – 77)] ) / 4
= (0 + 57 + 24) + 20 + (37 + 40 + 17) + (57 + 40) ) / 4
= (81 + 20 + 94 + 97)/4
= 292 / 4 = 73
 Average turn-around time = 134 + 37 + 162 + 121) / 4 = 113.5
Time Quantum and
Context Switches
 The SJF algorithm associates with each process the length of its next
CPU burst
 When the CPU becomes available, it is assigned to the process that has
the smallest next CPU burst (in the case of matching bursts, FCFS is
used)

Shortest Job  Two schemes:

First (SJF)  Nonpreemptive – once the CPU is given to the process, it cannot
be preempted until it completes its CPU burst

Scheduling  Preemptive – if a new process arrives with a CPU burst length


less than the remaining time of the current executing process,
preempt. This scheme is know as the Shortest-Remaining-Time-
First (SRTF)
Process Arrival Time Burst Time

P1 0.0 6

P2 0.0 4

Example #1: P3 0.0 1

P4 0.0 5
Non-Preemptive  SJF (non-preemptive, simultaneous arrival)
SJF
(simultaneous
arrival)
 Average waiting time = (0 + 1 + 5 + 10)/4 = 4
 Average turn-around time = (1 + 5 + 10 + 16)/4 = 8
Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1
Example #2: P4 5.0 4
Non-Preemptive  SJF (non-preemptive, varied arrival times)

SJF P1 P3 P2 P4

(varied arrival 0 3 7 8 12 16
times)  Average waiting time
= ( (0 – 0) + (8 – 2) + (7 – 4) + (12 – 5) )/4
= (0 + 6 + 3 + 7)/4 = 4
 Average turn-around time:
= ( (7 – 0) + (12 – 2) + (8 - 4) + (16 – 5))/4
Waiting time : sum of time that a process has spent waiting in the ready queue = ( 7 + 10 + 4 + 11)/4 = 8
Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4
Example #3:  SJF (preemptive, varied arrival times)
Preemptive SJF
P1 P2 P3 P2 P4 P1
(Shortest-remaining-
time-first) 0 2 4 5 7 11 16

 Average waiting time


= ( [(0 – 0) + (11 - 2)] + [(2 – 2) + (5 – 4)] + (4 - 4) + (7 – 5) /4
= 9 + 1 + 0 + 2)/4
=3
 Average turn-around time = (16 + 5 + 1 + 6)/4 = 7
 The SJF algorithm is a special case of the general priority
scheduling algorithm
 A priority number (integer) is associated with each process
 The CPU is allocated to the process with the highest priority
(smallest integer = highest priority)
 Priority scheduling can be either preemptive or non-preemptive
 A preemptive approach will preempt the CPU if the priority
of the newly-arrived process is higher than the priority of
Priority 
the currently running process
A non-preemptive approach will simply put the new

Scheduling process (with the highest priority) at the head of the ready
queue
 SJF is a priority scheduling algorithm where priority is the
predicted next CPU burst time
 The main problem with priority scheduling is starvation, that is,
low priority processes may never execute
 A solution is aging; as time progresses, the priority of a process
in the ready queue is increased
 Non-preemptive.
 Gives preferential treatment to important jobs.
◦ Programs with highest priority are
processed first.
◦ Aren’t interrupted until CPU cycles are
completed or a natural wait occurs.
Priority  If 2+ jobs with equal priority are in READY
Scheduling queue, processor is allocated to one that
arrived first (first come first served within
priority).
 Many different methods of assigning priorities
by system administrator or by Processor
Manager.
Example of
Priority
Scheduling
 Table below shows the priority algorithm. The process are assumed to
give arrived in the order Ts1, Ts2, Ts3 and Ts4 at all time 0. Answer the
following questions based on schedule time below. Assume priority 1 is
greater than 2. Draw gantt chart and calculate average waiting time.

PROCESS BURST TIME(ms) PRIORITY(ms)


Ts1 7 4
Ts2 5 2
Example 1 Ts3 2 3
Ts4 4 1
 By using a non-preemptive priority scheduling algorithm, calculate
average waiting time (AWT) and average turn around time (ATT)
 Note : a LOW numbers is HIGHER priority

PROCESS ARRIVAL PRIORITY CPU BURST


TIME (ms) TIME (ms)
P1 12 2 4
P2 2 1 2
Example 2 P3
P4
0
0
4
3
6
5
P5 7 5 9
 By using a pre emptive priority scheduling algorithm, calculate
average waiting time (AWT) and average turn around time
(ATT)
 Note : a LOW numbers is HIGHER priority
PROCESS ARRIVAL PRIORITY CPU BURST
TIME (ms) TIME (ms)
P1 12 2 4
P2 2 1 2
Example 3 P3
P4
0
0
4
3
6
5
P5 7 5 9
 Table below shows the NON -PRE EMPTIVE priority
algorithm. Answer the following questions based on schedule
time below. Assume small priority number means higher
priority. Draw gantt chart and calculate average waiting time
(AWT) & average turnaround time (ATT)
PROCESS ARRIVAL PRIORITY BURST
TIME(ms) TIME(ms)

Example 4 P
Q
0
0
4
2
9
8
R 7 3 12
S 12 1 7
T 17 5 4
 Table below shows the PRE EMPTIVE priority algorithm. Answer the
following questions based on schedule time below. Assume small
priority number means higher priority. Draw gantt chart and calculate
average waiting time (AWT) & average turnaround time (ATT)

PROCESS ARRIVAL PRIORITY BURST


TIME(ms) TIME(ms)

Example 5 P
Q
0
0
4
2
9
8
R 7 3 12
S 12 1 7
T 17 5 4
 Table below shows the PRE EMPTIVE priority algorithm. Answer
the following questions based on schedule time below. Assume
small priority number means higher priority. Draw gantt chart and
calculate average waiting time (AWT) & average turnaround time
(ATT)
PROCESS ARRIVAL BURST PRIORITY
TIME(ms) TIME(ms)
M 0 16 4

Example 6 E
S
3
3
8
8
3
2
R 8 4 1
A 15 12 5
Z 16 6 2
 Multi-level queue scheduling is used when processes can be classified
into groups
 For example, foreground (interactive) processes and background
(batch) processes
 The two types of processes have different response-time
requirements and so may have different scheduling needs
 Also, foreground processes may have priority (externally defined)
over background processes

Multi-level  A multi-level queue scheduling algorithm partitions the ready queue


into several separate queues

Queue  The processes are permanently assigned to one queue, generally based
on some property of the process such as memory size, process priority,
or process type
Scheduling  Each queue has its own scheduling algorithm
 The foreground queue might be scheduled using an RR algorithm
 The background queue might be scheduled using an FCFS
algorithm
 In addition, there needs to be scheduling among the queues, which is
commonly implemented as fixed-priority pre-emptive scheduling
 The foreground queue may have absolute priority over the
background queue
 In multi-level feedback queue scheduling, a process can move between
the various queues; aging can be implemented this way
 A multilevel-feedback-queue scheduler is defined by the following
parameters:
Multilevel  Number of queues

Feedback Queue  Scheduling algorithms for each queue


 Method used to determine when to promote a process
Scheduling  Method used to determine when to demote a process
 Method used to determine which queue a process will enter when
that process needs service
 Scheduling
 A new job enters queue Q0 (RR) and is placed at the end. When it gains
the CPU, the job receives 8 milliseconds. If it does not finish in 8
milliseconds, the job is moved to the end of queue Q1.
 A Q1 (RR) job receives 16 milliseconds. If it still does not complete, it
is preempted and moved to queue Q2 (FCFS).

Example of
Multilevel
Feedback Queue
Process Burst Time (ms)
A 20
B 4
C 36
K 8
S 12

Calculate AWT & ATT , use SJF


Question 1
Process CPU burst (ms)
P1 24
P2 3
P3 3

Calculate AWT & ATT , use SJF

Question 2
Process Burst Time (ms)
P1 7
P2 14
P3 3
P4 6

Calculate AWT & ATT , use SJF

Question 3
Process Arrival Time (ms) Burst Time (ms)

P1 0 8
P2 1 4
P3 2 9
P4 3 5

Calculate AWT & ATT , use SJF


Question 4
Process Arrival Time (ms) Burst Time (ms)
A 0 8
B 1 6
C 3 1
D 5 2

Calculate AWT & ATT , use SJF

Question 5
Process Arrival Time (ms) Burst Time (ms)
H 8 5
O 2 9
N 18 2
E 4 7
Y 0 8

Question 6 Calculate AWT & ATT , use SJF


Process Arrival Time (ms) Burst Time (ms)
H 8 5
O 2 9
N 18 2
E 4 7
Y 0 8

Question 7 Calculate AWT & ATT , use SJF (preemptive)


 Consider the following four processes in Table below. Show the
result in the Gantt chart and state the average waiting time
(AWT) and average turnaround time (ATT) for SJF algorithm

Process Arrival time CPU burst time


(ms)

Question 8
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Consider the following four processes in Table below. Show the
result in the Gantt chart and state the average waiting time
(AWT) and average turnaround time (ATT) for SJF Preemptive
algorithm

Process Arrival time CPU burst time


(ms)

Question 9
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Consider the following four processes in Table below. Show the
result in the Gantt chart and state the average waiting time
(AWT) and average turnaround time (ATT) for SJF algorithm

Question 9
ARRIVAL BURST
PROCESS
TIME TIME
1 0 8
2 2 5
3 3 1
4 4 2
5 5 8
 Consider the following four processes in Table below. Show the
result in the Gantt chart and state the average waiting time
(AWT) and average turnaround time (ATT) for SJF Preemptive
algorithm

ARRIVAL BURST
PROCESS

Question 10
TIME TIME
1 0 7
2 1 5
3 2 3
4 3 1
5 4 2
6 5 1
 Consider the following four processes in Table below. Show the
result in the Gantt chart and state the average waiting time
(AWT) and average turnaround time (ATT) for SJF Preemptive
algorithm

ARRIVAL BURST
PROCESS

Question 11
TIME TIME
K 19 2
E 14 10
L 4 6
U 4 5
A 0 9
R 8 9
 Consider the following four processes in Table below. Show the
result in the Gantt chart and state the average waiting time
(AWT) and average turnaround time (ATT) for SJF Preemptive
algorithm

ARRIVAL BURST
PROCESS
TIME TIME

Question 12 1
2
3
0
5
7
10
7
2
4 10 10
5 14 5
Process Arrival time CPU burst time

G 6 6

O 8 2

L 0 7

Question 13 D 11 5

Calculate AWT & ATT , use SJF (pre emptive)


PROCESS ARRIVAL TIME(ms) BURST TIME(ms)
S 18 6
C 12 4
O 2 10
R 0 9
E 4 7

Question 14 Calculate AWT & ATT , use SJF (pre emptive)


PROCESS ARRIVAL TIME BURST TIME
A 0 9
B 2 2
C 10 6
D 15 4
E 19 7

Question 15 Calculate AWT & ATT , use SJF (pre emptive)


1. Consider the following set of process in Table 1 which arrive at time 0 with the
order A, B, C, D and the following CPU burst time. Find the Average Waiting
Time (AWT) with RR of quantum : 10 ms

Process CPU burst time


A 20
B 40
C 14
D 6
Table 1

Question 16
1. Consider the following processes in Table 2 which are in the ready queue in the
given order with the relative next CPU bursts. Use FIFO algorithm to calculate
average waiting time (AWT) and average turnaround time (ATT).

Next CPU burst


Process time
P1 7
P2 14
P3 3
P4 6
Table 2

Question 17
1. Based on Table 3, consider the following set of process with the length of CPU-
burst time given in milliseconds:

Process Burst Time


L 9
E 10
N 4
A 12
Table 3
C
Draw Gant Chart and calculate Average Waiting Time (AWT) according to
First in First out Scheduling.

Question 18
1. Based on Table 4, calculate the average waiting time complete with gantt chart
for the question below using Round Robin (RR) scheduling algorithm. Use Time
Quantum = 5ms

Table 1

Process Burst Time (millisecond)


P1 12
P2 7
P3 10
P4 3
P5 4

Question 19

You might also like