Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Operating

System
CH. 4 Services
BY DR. VEEJYA KUMBHAR
Ch. 4 Operating System Services
Syllabus

• Processes ✓
• Process Structure ✓
• Process Scheduling ✓
• Scheduling Algorithms
• Process Synchronization & Deadlocks
• Thread Management
• Memory Management
• System Calls
• File System
Scheduling Algorithms

❑ FCFS (Non-pre-emptive)
❑ SJF ( Pre-emptive and Non-pre-emptive)
❑ Priority ( Pre-emptive and Non-pre-emptive)
❑ Round-Robin ( Pre-emptive)
❑ Multilevel Queue ( Pre-emptive)
❑ Multilevel Feedback Queue( Pre-emptive)
Choosing a scheduling algorithm

❑ No algorithm is “best” all the time.


❑ Algorithms is chosen by the OS developer or programmer.
❑ It is selected on the basis of user’s requirements and algorithm strategies.
For example, Windows NT/XP/Vista uses a multilevel feedback queue, a
combination of fixed-priority preemptive scheduling, round-robin, and first in, first out
algorithms.
Process Attributes for scheduling

 Burst Time
 Every process in a computer system requires some amount of time for its execution. This
time is both the CPU time and the I/O time. The CPU time is the time taken by CPU to
execute the process. While the I/O time is the time taken by the process to perform some
I/O operation. In general, we ignore the I/O time and we consider only the CPU time for a
process. So, Burst time is the total time taken by the process for its execution on the
CPU.

 Arrival time
 Arrival time is the time when a process enters into the ready state and is ready for its
execution.
Here in the above example, the arrival time of all the 3 processes are 0 ms, 1 ms, and 2 ms respectively.

Exit time / Completion Time

Exit time is the time when a process completes its execution and exit from the system.
Gantt Chart

 The chart/diagram/picture which shows scheduling of


processes at different time intervals.
Metrics to evaluate algorithm performance

 Response Time
 Waiting Time
 Turnaround Time
 Throughput
 CPU Utilization
Metrics to evaluate algorithm performance

 Response Time
The first response time when the process gets selected
for execution.
 Waiting Time
The total time process has spent in the system sitting idle.
Metrics to evaluate algorithm performance

 Turnaround Time
 It is the total time a process has spent in the system(including burst
time, CPU time, I/O time , waiting time etc.)

 Throughput
 It is the total no. of processes completed in one unit of time.

 CPU Utilization
 It is measured as how much time the CPU is busy in executing the
processes.
1. First Come First Serve:

FCFS considered to be the simplest of all operating system scheduling algorithms. First
come first serve scheduling algorithm states that the process that requests the CPU
first is allocated the CPU first and is implemented by using FIFO queue.

Characteristics of FCFS:
•FCFS supports non-preemptive CPU scheduling algorithms.
•Tasks are always executed on a First-come, First-serve concept.
•FCFS is easy to implement and use.
•This algorithm is not much efficient in performance, and the wait time is quite high.
1. First Come First Serve:

Advantages of FCFS:
• Easy to implement
• First come, first serve method

Disadvantages of FCFS:
FCFS suffers from Convoy effect.
The average waiting time is much higher than the other algorithms.
FCFS is very simple and easy to implement and hence not much efficient.
Convoy Effect in OS

Convoy Effect is phenomenon associated with the First Come First Serve
(FCFS) algorithm, in which the whole Operating System slows down due
to few slow processes.

FCFS algorithm is non-preemptive in nature, that is, once CPU time has been
allocated to a process, other processes can get CPU time only after the current
process has finished. This property of FCFS scheduling leads to the situation
called Convoy Effect.
Ex. 1 Schedule following processes using FCFS algorithm
and Calculate response time, waiting time, turnaround
time, completion time of all the processes. Also Calculate
average turnaround time, average waiting time ,
throughput, and CPU utilization.
0 ms

P1

0 ms 8 ms

P1 P2

0 ms 8 ms 15 ms

P1 P2 P3

0 ms 8 ms 15 ms 25 ms
Process Arrival Time Burst Time Response Time Competition Turnaround Waiting
Time Time Time
P1 0 8 0 8 8-0 = 8 8-8 = 8
P2 0 7 8 15 15-0= 15 15-7 = 8
P3 2 10 15 25 25-2 = 23 23-10 = 13
Turnaround Time = Completion Time – Arrival Time
Waiting Time = Turnaround Time – Burst Time

Avg. Turnaround Time = Total Turnaround Time / no. of processes


= 46 / 3 = 15.3 ms
Avg. Waiting Time = Total Waiting Time / no. of processes
= 29 / 3 = 9.6 ms
Throughput = no. of processes /(System Competition Time- System Start Time)
=3 / ( 25-0) = 0.08 = 8 % of a process is completed in 1 unit of time
CPU Utilization = 100 * (System Competition Time- System Start Time) / = 100*(25-0) / 25
System Completion Time = 100 %
Ex. 2 Schedule following processes using FCFS algorithm
and Calculate response time, waiting time, turnaround
time, completion time of all the processes. Also Calculate
average turnaround time, average waiting time ,
throughput, and CPU utilization.
Process Arrival Time CPU Burst Time
P0 2 6
P1 5 2
P2 1 8
P3 0 3
P4 4 4
Ex. 3 Schedule following processes using FCFS algorithm
and Calculate response time, waiting time, turnaround
time, completion time of all the processes. Also Calculate
average turnaround time, average waiting time ,
throughput, and CPU utilization.
Process Arrival Time CPU Burst Time
P0 2 1
P1 5 5
P2 5 3
P3 4 2
P4 4 7
Ex. 4 Schedule following processes using FCFS algorithm
and Calculate response time, waiting time, turnaround
time, completion time of all the processes. Also Calculate
average turnaround time, average waiting time ,
throughput, and CPU utilization.
Ex. 5 Schedule following processes using FCFS algorithm
and Calculate response time, waiting time, turnaround
time, completion time of all the processes. Also Calculate
average turnaround time, average waiting time ,
throughput, and CPU utilization.
SJF (Non Pre- emptive )
2. Shortest Job First
(SJF)
2. Shortest Job First (SJF) :

Shortest job first (SJF) is a scheduling process that selects the waiting process with
the smallest execution time to execute next.
This scheduling method may or may not be preemptive.
Shortest remaining time first(SRTF) is the preemptive version of the Shortest job
first
Characteristics of SJF:
• Shortest Job first has the advantage of having a minimum average waiting time among
all operating system scheduling algorithms.
• It is associated with each task as a unit of time to complete.
• It may cause starvation if shorter processes keep coming. This problem can be solved
using the concept of ageing.
2. Shortest Job First

Advantages of SJF:
As SJF reduces the average waiting time thus, it is better than the first come first serve
scheduling algorithm.
SJF is generally used for long term scheduling.

Disadvantages of SJF:
One of the demerit SJF has, is the starvation.
Many times, it becomes complicated to predict the length of the upcoming CPU
request
Ex. 1 Schedule following processes using SJF (Non Pre-
emptive) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
0 ms

P2

0 ms 7 ms

P2 P1

0 ms 7 ms 15 ms

P2 P1 P3

0 ms 7 ms 15 ms 25 ms
Process Arrival Time Burst Time Response Time Competition Turnaround Waiting
Time Time Time
P2 0 7 0 7 7-0 = 7 7-7 = 0
P1 0 8 7 15 15-0= 15 15-8 = 7
P3 2 10 15 25 25-2 = 23 23-10 = 13
Turnaround Time = Completion Time – Arrival Time
Waiting Time = Turnaround Time – Burst Time

Avg. Turnaround Time = Total Turnaround Time / no. of processes


= 45 / 3 = 15 ms
Avg. Waiting Time = Total Waiting Time / no. of processes
= 20 / 3 = 6.6 ms
Throughput = no. of processes /(System Competition Time- System Start Time)
=3 / ( 25-0) = 0.08 = 8 % of a process is completed in 1 unit of time
CPU Utilization = 100 * (System Competition Time- System Start Time) / = 100*(25-0) / 25
System Completion Time = 100 %
Ex. 2 Schedule following processes using SJF (Non Pre-
emptive ) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
Process Arrival Time CPU Burst Time
P0 2 6
P1 5 2
P2 1 8
P3 0 3
P4 4 4
Ex. 3 Schedule following processes using SJF (Non Pre-
emptive ) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
Process Arrival Time CPU Burst Time
P0 2 1
P1 5 5
P2 5 3
P3 4 2
P4 4 7
Ex. 4 Schedule following processes using SJF (Non Pre-
emptive ) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
Ex. 5 Schedule following processes using SJF (Non Pre-
emptive ) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
2. Shortest Job First
(SJF) – Pre-emptive
Ex. 1 Schedule following processes using SJF (Non Pre-
emptive) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
Process Arrival CPU Burst
Time Time
P0 2 1
P1 5 5
P2 5 3
P3 4 2
P4 4 7
Process Arrival CPU Burst
idle Time Time
0 ms 2 ms P0 2 1
idle P0 P1 5 5
0 ms 2 ms 3 ms P2 5 3
P3 4 2
idle P0 idle
4 ms P4 4 7
0 ms 2 ms 3 ms

Idle P0 Idle P3
0 ms 2 ms 3 ms 4 ms 6 ms

Idle P0 Idle P3 P2
0 ms 2 ms 3 ms 4 ms 6 ms 9 ms

Idle P0 Idle P3 P2 P1
0 ms 2 ms 3 ms 4 ms 6 ms 9 ms 14 ms

Idle P0 Idle P3 P2 P1 P4
0 ms 2 ms 3 ms 4 ms 6 ms 9 ms 14 ms 21 ms
Process Arrival Time Burst Time Response Time Competition Turnaround Waiting
Time Time Time
P0 2 1 2 3 3-2 = 1 1-1 = 0
P1 5 5 9 14 14 -5= 9 9-5 = 4
P2 5 3 6 9 9-5 = 4 4-3 = 1
P3 4 2 4 6 6-4 = 2 2-2 = 0
p4 4 7 14 21 21-4 = 17 17-7 = 10
Turnaround Time = Completion Time – Arrival Time
Waiting Time = Turnaround Time – Burst Time
Avg. Turnaround Time = Total Turnaround Time / no. of processes
= 33 / 5 = 6.6 ms
Avg. Waiting Time = Total Waiting Time / no. of processes
= 15 / 6 = 2.6 ms
Throughput = no. of processes /(System Competition Time- System Start Time)-System Idle Time
=5 / ( 21-0-3) = 0.277 = 28 % of a process is completed in 1 unit of time
CPU Utilization = 100 * (System Competition Time- System Start Time- = 100*(21-0-3) / 21
system idle time) / System Completion Time = 0.857 = 86 %
3. Priority Scheduling
Algorithm
3. Priority Algorithm:

Priority Scheduling is a scheduling process that selects the waiting process with the
highest priority (smallest priority number ) for execution.
This scheduling method may or may not be preemptive.

Characteristics of Priority Algorithm:


• Schedules tasks based on priority.
• When the higher priority work arrives while a task with less priority is executed, the
higher priority work takes the place of the less priority one and
• The latter is suspended until the execution is complete.
• Lower is the number assigned, higher is the priority level of a process.
3. Priority Scheduling Algorithm

Advantages of Priority Algorithm:


• The average waiting time is less than FCFS
• Less complex

Disadvantages of Priority Algorithm :


• One of the most common demerits of the Preemptive priority CPU scheduling
algorithm is the Starvation Problem.
This is the problem in which a process has to wait for a longer amount of time to get
scheduled into the CPU. This condition is called the starvation problem.
Ex. 1 Schedule following processes using Priority (Non
Pre-emptive) algorithm and Calculate response time,
waiting time, turnaround time, completion time of all
the processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
Process Priority Burst Time Arrival Time
P1 1(highest) 4 0
P2 2 3 0
P3 1 7 3
P4 3(lowest) 4 11
P5 2 2 12
Burst Arrival
p2 Process Priority
Time Time
0 ms 1 ms 1
P1 4 1
(highest)
p2 p1 P2 3 3 0
P3 1 7 3
0 ms 1 ms 4 ms
4
P4 4 11
(lowest)
p2 p1 p3 P5 2 2 12
0 ms 1 ms 4 ms 11 ms

p2 p1 p3 p2
0 ms 1 ms 4 ms 11 ms 12 ms

p2 p1 p3 p2 p5
0 ms 1 ms 4 ms 11 ms 12 ms 14 ms

p2p4 p1 p3 p2 p5 p4
0 ms 1 ms 4 ms 11 ms 12 ms 14 ms 14 ms
Process Arrival Time Burst Time Response Time Competition Turnaround Waiting
Time Time Time
P1 0 4 0 4 4-0=4 4-4 = 0
P2 0 3 11 14 14 -0= 14 14-3 = 11
P3 3 7 4 11 11-3 = 8 8-7 = 1
p4 11 4 16 20 20-11 = 9 9-4 = 5
p5 12 2 14 16 16-12 = 4 4-2 = 2
Turnaround Time = Completion Time – Arrival Time
Waiting Time = Turnaround Time – Burst Time
Avg. Turnaround Time = Total Turnaround Time / no. of processes
= 39 / 5 = 7.5 ms
Avg. Waiting Time = Total Waiting Time / no. of processes
= 19 / 5 = 3.8 ms
Throughput = no. of processes /(System Competition Time- System Start Time)-System Idle Time
=5 / ( 20-0-0) = 0.25 = 25 % of a process is completed in 1 unit of time
CPU Utilization = 100 * (System Competition Time- System Start Time- = 100*(20-0-0) / 20
system idle time) / System Completion Time = 100 %
3. Priority Scheduling
Algorithm
(Pre-emptive)
Ex. 1 Schedule following processes using Priority (Pre-
emptive) algorithm and Calculate response time, waiting
time, turnaround time, completion time of all the
processes. Also Calculate average turnaround time,
average waiting time , throughput, and CPU utilization.
Process Priority Burst Time Arrival Time
P1 1 (highest) 4 1
P2 4 3 0
P3 1 7 3
P4 3 4 11
P5 2 2 12
Burst Arrival
Process
p2 Time Time
0 ms 1 ms P1 4 1
P2 3 0
p2 p1 P3 7 3
0 ms 1 ms 3 ms P4 4 11
P5 2 12
p2 p1 p3
0 ms 1 ms 3 ms 5 ms

p2 p1 p3 p2
0 ms 1 ms 3 ms 5 ms 7 ms

p2 p1 p3 p2 p1
0 ms 1 ms 3 ms 5 ms 7 ms 9 ms

p2 p1 p3 p2 p1 p3
0 ms 1 ms 3 ms 5 ms 7 ms 9 ms 11 ms

p2 p1 p3 p2 p1 p3 p4 p5
0 1 3 5 7 9 11 13 15
Process Arrival Time Burst Time Response Time Competition Turnaround Waiting
Time Time Time
P1 1 4 1 5 5-1=4 4-4 = 0
P2 0 3 0 20 20 -0= 20 20-3 = 17
P3 3 7 5 12 12-3 = 9 9-7 = 2
p4 11 4 14 18 18-11 = 7 7-4 = 3
p5 12 2 12 14 14-12 = 2 2-2 = 0
Turnaround Time = Completion Time – Arrival Time
Waiting Time = Turnaround Time – Burst Time
Avg. Turnaround Time = Total Turnaround Time / no. of processes
= 42 / 5 = 8.4 ms
Avg. Waiting Time = Total Waiting Time / no. of processes
= 22 / 5 = 4.4 ms
Throughput = no. of processes /(System Competition Time- System Start Time)-System Idle Time
=5 / ( 20-0-0) = 0.25 = 25 % of a process is completed in 1 unit of time
CPU Utilization = 100 * (System Competition Time- System Start Time- = 100*(20-0-0) / 20
system idle time) / System Completion Time = 100 %
4. Round – Robin
Algorithm
4. Round Robin Algorithm:

Round Robin is a CPU scheduling algorithm where each process is cyclically assigned
a fixed time slot.
It is the preemptive version of First come First Serve CPU Scheduling algorithm.
Round Robin CPU Algorithm generally focuses on Time Sharing technique.

Characteristics of Round Robin Algorithm:


• It’s simple, easy to use, and starvation-free as all processes get the balanced CPU
allocation.
• One of the most widely used methods in CPU scheduling as a core.
• It is considered preemptive as the processes are given to the CPU for a very limited
time.
4. Round - Robin Scheduling Algorithm

Advantages of Round Robin Algorithm:


• Round robin seems to be fair as every process gets an equal share of CPU.
• The newly created process is added to the end of the ready queue.

Disadvantages of Round Robin Algorithm :


• If the time slot is short , then more time is wasted in context switch.
Ex. 1 Schedule following processes using Round Robin
(Non Pre-emptive) algorithm and Calculate response
time, waiting time, turnaround time, completion time of
all the processes. Also Calculate average turnaround
time, average waiting time , throughput, and CPU
utilization.( Time quantum= 2 units)
Process Burst Time Arrival Time
P1 4 1
P2 3 0
P3 7 3
P4 4 11
P5 2 12
Burst Arrival
Time Slot = Process
p2 Time Time
2 units
0 ms 2 ms P1 4 1
p2 p1 P2 3 0
P3 7 3
0 ms 2 ms 4 ms
P4 4 11
p2 p1 p3 P5 2 12
0 ms 2 ms 4 ms 6 ms

p2 p1 p3 p2
0 ms 2 ms 4 ms 6 ms 7 ms

p2 p1 p3 p2 p1
0 ms 2 ms 4 ms 6 ms 7 ms 9 ms

p2 p1 p3 p2 p1 p3
0 ms 2 ms 4 ms 6 ms 7 ms 9 ms 11 ms
p2 p1 p3 p2 p1 p3 p4 p5
0 2 4 6 7 9 11 13 15
p2 p1 p3 p2 p1 p3 p4 p5 p3 p4 p3
0 2 4 6 7 9 11 13 15 17 19 20
Process Arrival Time Burst Time Response Time Competition Turnaround Waiting
Time Time Time
P1 1 4 2 7-1=6
7 4-4 = 0
P2 0 3 0 9 -0= 9
9 20-3 = 17
P3 3 7 4 20 -3 = 17 9-7 = 2
20
p4 11 4 11 19 -11 = 7-4 = 3
19
p5 12 2 13 715 -12 = 2-2 = 0
15
2
Turnaround Time = Completion Time – Arrival Time
Waiting Time = Turnaround Time – Burst Time
Avg. Turnaround Time = Total Turnaround Time / no. of processes
= 42 / 5 = 8.4 ms
Avg. Waiting Time = Total Waiting Time / no. of processes
= 22 / 5 = 4.4 ms
Throughput = no. of processes /(System Competition Time- System Start Time)-System Idle Time
=5 / ( 20-0-0) = 0.25 = 25 % of a process is completed in 1 unit of time
CPU Utilization = 100 * (System Competition Time- System Start Time- = 100*(20-0-0) / 20
system idle time) / System Completion Time = 100 %
5. Multiple /Multiple Queue

Processes in the ready queue can be divided into different classes where each class has
its own scheduling needs.
For example, a common division is a foreground (interactive) process and
a background (batch) process.
These two classes have different scheduling needs.
For this kind of situation Multilevel Queue Scheduling is used.
5. Multiple /Multiple Queue

The description of the processes in the above


diagram is as follows:
System Processes: The CPU itself has its process
to run, generally termed as System Process.
Interactive Processes: An Interactive Process is a
type of process in which there should be the same
type of interaction.
Batch Processes: Batch processing is generally a
technique in the Operating system that collects the
programs and data together in the form of
a batch before the processing starts.
5. Multilevel /Multiple Queue

Advantages :
The main merit of the multilevel queue is that it has a low scheduling overhead.

Disadvantages :
• Starvation problem
• It is inflexible in nature
6. Multilevel Feedback Queue

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling is like Multilevel


Queue Scheduling but in this process can move between the queues.
And thus, much more efficient than multilevel queue scheduling.

Characteristics of Multilevel Feedback Queue Scheduling:


• In a multilevel queue-scheduling algorithm, processes are permanently assigned to
a queue on entry to the system, and processes are not allowed to move between
queues.
• As the processes are permanently assigned to the queue, this setup has the
advantage of low scheduling overhead,
• But on the other hand disadvantage of being inflexible.
5. Multilevel Feedback Queue

Advantages :
• It is more flexible
• It allows different processes to move between different queues

Disadvantages :
• It also produces CPU overheads
• It is the most complex algorithm.
Algorithm Average waiting
Allocation is Complexity Preemption Starvation Performance
time (AWT)
According to the arrival
Simple and easy to
FCFS time of the processes, the
implement
Large. No No Slow performance
CPU is allocated.

Based on the lowest CPU Minimum Average Waiting


SJF burst time (BT).
More complex than FCFS Smaller than FCFS No Yes
Time

Same as SJF the allocation


Depending on some
of the CPU is based on the The preference is given to
SRTF lowest CPU burst time (BT).
More complex than FCFS measures e.g., arrival time, Yes Yes
the short jobs
process size, etc
But it is preemptive.

According to the order of


The complexity depends on Large as compared to SJF Each process has given a
RR the process arrives with
Time Quantum size and Priority scheduling.
Yes No
fairly fixed time
fixed time quantum (TQ)

Priority According to the priority. Well performance but


Pre- The bigger priority task This type is less complex Smaller than FCFS Yes Yes contain a starvation
executes first problem
emptive
Priority According to the priority
non- with monitoring the new This type is less complex Preemptive Smaller than Most beneficial with batch
No Yes
preemptive incoming higher priority than Priority preemptive FCFS systems
jobs

According to the process More complex than the Good performance but
MLQ that resides in the bigger priority scheduling Smaller than FCFS No Yes contain a starvation
queue priority algorithms problem

It is the most Complex but


According to the process of Smaller than all scheduling
MFLQ a bigger priority queue.
its complexity rate depends
types in many cases
No No Good performance
on the TQ size
Thank
You

You might also like