Scheduling Algorithm: Fifo-Rr-Sjf-P-Mlq-Mlfq
Scheduling Algorithm: Fifo-Rr-Sjf-P-Mlq-Mlfq
Scheduling Algorithm: Fifo-Rr-Sjf-P-Mlq-Mlfq
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)
P1 P2 P3
0 24 27 30
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.
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)
First (SJF) Nonpreemptive – once the CPU is given to the process, it cannot
be preempted until it completes its CPU burst
P1 0.0 6
P2 0.0 4
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
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.
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)
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
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
Example of
Multilevel
Feedback Queue
Process Burst Time (ms)
A 20
B 4
C 36
K 8
S 12
Question 2
Process Burst Time (ms)
P1 7
P2 14
P3 3
P4 6
Question 3
Process Arrival Time (ms) Burst Time (ms)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
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 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
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
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).
Question 17
1. Based on Table 3, consider the following set of process with the length of CPU-
burst time given in milliseconds:
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
Question 19