0% found this document useful (0 votes)
98 views17 pages

Osy Chapter 4 Notes

Uploaded by

Astha Madekar
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)
98 views17 pages

Osy Chapter 4 Notes

Uploaded by

Astha Madekar
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/ 17

Unit IV – CPU Scheduling and Algorithm

4.1 Scheduling types

CPU and I/O Burst Cycle:


 Process execution consists of a cycle of CPU execution and I/O wait.
 Processes alternate between these two states.
 Process execution begins with a CPU burst, followed by an I/O burst, then
another CPU burst ... etc
 The last CPU burst will end with a system request to terminate execution rather
than with another I/O burst.
 The duration of these CPU burst have been measured.
 An I/O-bound program would typically have many short CPU bursts; A CPU-
bound program might have a few very long CPU bursts.
 This can help to select an appropriate CPU-scheduling algorithm.
CPU scheduling is divided into two types:
1. Preemptive Scheduling
2. Non-Preemptive Scheduling
Preemptive Scheduling:
a scheduling method that interrupts the processing of a process and transfers CPU to
another process is called Preemptive CPU Scheduling. The process switches from running
state to ready state and waiting state to ready state.

Non-Preemptive Scheduling:
In this scheduling, once the system has allocated CPU to a process, the system cannot
remove that processor from the process. The process switches from running to waiting
state and termination of process.
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

The main objective of any CPU scheduling algorithm is to keep the CPU as busy as
possible. Theoretically, CPU utilization can range from 0 to 100 but in a real-time system,
it varies from 40 to 90 percent depending on the load upon the system.

2. Throughput

A measure of the work done by the CPU is the number of processes being executed and
completed per unit of time. This is called throughput. The higher is the number, the more
work is done by the system.

3. Turnaround Time

For a particular process, an important criterion is how long it takes to execute that
process. The turnaround time start from the time of submission of a process to the time
of completion of a process.

Turn Around Time = Completion Time - Arrival Time.

4. Waiting time

How much time processes spend in the ready queue waiting their turn to get on the CPU.

Waiting Time = Turnaround Time - Burst Time.

5. Response Time

The time taken from submission of the process of the request until the first response is
produced is called response time.

Response Time = CPU Allocation Time (when the CPU was allocated for the first) -
Arrival Time

In brief:
Arrival Time: Time at which the process arrives in the ready queue.
Completion Time: Time at which process completes its execution.
Burst Time: Time required by a process for CPU execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time (W.T): Time Difference between turnaround time and burst time.
Waiting Time = Turn Around Time – Burst Time
4.2 Types of Scheduling Algorithm
1. First Come First Serve (FCFS)
2. Shortest Job First (SJF)
3. Round Robin Scheduling
4. Priority Scheduling
5. Multilevel Queue Scheduling

1. First Come First Serve (FCFS)


 In FCFS Scheduling
 The process which arrives first in the ready queue is firstly assigned the CPU.
 In case of a tie, process with smaller process id is executed first.
 It is always non-preemptive in nature.
 Jobs are executed on first come, first serve basis.
 Easy to understand and implement.
 Poor in performance as average wait time is high.
Advantages-
 It is simple and easy to understand.
 It does not lead to starvation.
Disadvantages-
 It does not consider the priority or burst time of the processes.
 It suffers from convoy effect i.e. processes with higher burst time arrived before the
processes with smaller burst time.

Problem 1:
Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting
time (WT), Response time (RT), Average Turn-around time and Average Waiting time using FCFS
algorithm.
Process ID Arrival time Burst time
P1 2 2
P2 5 6
P3 0 4
P4 0 7
P5 7 4

Solution

Gantt chart
Process ID Arrival time Burst time CT TAT=CT-AT WT=TAT-BT RT

P1 2 2 13 13-2= 11 11-2= 9 9

P2 5 6 19 19-5= 14 14-6= 8 8

P3 0 4 4 4-0= 4 4-4= 0 0

P4 0 7 11 11-0= 11 11-7= 4 4

P5 7 4 23 23-7= 16 16-4= 12 12

Average Waiting time = (9+8+0+4+12)/5 = 33/5 = 6.6 time unit


Average Turn-around time = (11+14+4+11+16)/5 = 56/5 = 11.2 time unit

Problem 2:

Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting
time (WT), Response time (RT), Average Turn-around time and Average Waiting time.

Process ID Arrival time Burst time

P1 2 2

P2 0 1

P3 2 3

P4 3 5

P5 4 5

Solution

Gantt chart −
For this problem CT, TAT, WT, RT is shown in the given table –

Process ID Arrival time Burst time CT TAT=CT-AT WT=TAT-BT RT

P1 2 2 4 4-2= 2 2-2= 0 0

P2 0 1 1 1-0= 1 1-1= 0 0

P3 2 3 7 7-2= 5 5-3= 2 2

P4 3 5 12 12-3= 9 9-5= 4 4

P5 4 5 17 17-4= 13 13-5= 8 8

Average Waiting time = (0+0+2+4+8)/5 = 14/5 = 2.8 time unit


Average Turn-around time = (2+1+5+9+13)/5 = 30/5 = 6 time unit

2. Shortest Job First (SJF)


 Process which have the shortest burst time are scheduled first.
 If two processes have the same bust time, then FCFS is used to break the tie.
 This is a non-pre-emptive, pre-emptive scheduling algorithm.
 Pre-emptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF).
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not known.
Advantages-
 Maximum throughput
 Minimum average waiting and turnaround time

Disadvantages-
1. May suffer with the problem of starvation
2. It is not implementable because the exact Burst time for a process can't be known in
advance.
Problem 1:

Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting
time (WT), Response time (RT), Average Turn-around time and Average Waiting time using SJF.

PID Arrival Time Burst Time


P1 1 7
P2 3 3
P3 6 2
P4 7 10
P5 9 8

Solution

Gantt chart −

PID Arrival Time Burst Time CT TAT=CT-AT WT=TAT-BT RT


P1 1 7 8 7 0 0
P2 3 3 13 10 7 7
P3 6 2 10 4 2 2
P4 7 10 31 24 14 14
P5 9 8 21 12 4 4

Average Waiting time = (0+7+2+14+4)/5 = 27/5 = 5.4 time unit


Average Turn-around time = (7+10+4+24+12)/5 = 57/5 = 11.4 time unit
Problem 2:

Consider the set of 5 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is SJF non-preemptive, calculate the average waiting time and average
turnaround time.
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
Gantt Chart-

Process Arrival Burst


Id CT TAT=CT-AT WT=TAT-BT RT
time time
P1 3 1 7 7–3=4 4–1=3 3
P2 1 4 16 16 – 1 = 15 15 – 4 = 11 11
P3 4 2 9 9–4=5 5–2=3 3
P4 0 6 6 6–0=6 6–6=0 0
P5 2 3 12 12 – 2 = 10 10 – 3 = 7 7

Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit
Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit

3. Shortest Remaining Time First (SRTF)

This Algorithm is the preemptive version of SJF scheduling. In SRTF, the execution of the
process can be stopped after certain amount of time. At the arrival of every process, the short term
scheduler schedules the process with the least remaining burst time among the list of available
processes and the running process.
Problem 1:

Consider the set of 5 processes whose arrival time and burst time are given below- If the CPU
scheduling policy is SRTF (SJF pre-emptive), calculate the average waiting time and average
turnaround time.

Process Id Arrival time Burst time


P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
Gantt Chart-

Process Arrival Burst


Id CT TAT=CT-AT WT=TAT-BT RT
time time
P1 3 1 4 4–3=1 1–1=0 3
P2 1 4 6 6–1=5 5–4=1 1
P3 4 2 8 8–4=4 4–2=2 6
P4 0 6 16 16 – 0 = 16 16 – 6 = 10 0
P5 2 3 11 11 – 2 = 9 9–3=6 8

Average Turn Around time = (1 + 5 + 4 + 16 + 9) / 5 = 35 / 5 = 7 unit


Average waiting time = (0 + 1 + 2 + 10 + 6) / 5 = 19 / 5 = 3.8 unit
Problem 2:

Consider the set of 6 processes whose arrival time and burst time are given below- If the CPU
scheduling policy is shortest remaining time first, calculate the average waiting time and average
turnaround time.

Process Id Arrival time Burst time


P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1
Solution-
Gantt Chart-

Process Arrival Burst


Id CT TAT=CT-AT WT=TAT-BT RT
time time
P1 0 7 19 19 – 0 = 19 19 – 7 = 12 0
P2 1 5 13 13 – 1 = 12 12 – 5 = 7 1
P3 2 3 6 6–2=4 4–3=1 2
P4 3 1 4 4–3=1 1–1=0 3
P5 4 2 9 9–4=5 5–2=3 7
P6 5 1 7 7–5=2 2–1=1 6

Average Turn Around time = (19 + 12 + 4 + 1 + 5 + 2) / 6 = 43 / 6 = 7.17 unit
Average waiting time = (12 + 7 + 1 + 0 + 3 + 1) / 6 = 24 / 6 = 4 unit

4. Round Robin Scheduling

 CPU is assigned to the process on the basis of FCFS for a fixed amount of time.
 This fixed amount of time is called as time quantum or time slice.
 After the time quantum expires, the running process is preempted and sent to the
ready queue.
 Then, the processor is assigned to the next arrived process.
 It is always preemptive in nature.
Advantages-
 It gives the best performance in terms of average response time.
 It is best suited for time sharing system, client server architecture and interactive
system.

Disadvantages-
 It leads to starvation for processes with larger burst time as they have to repeat the
cycle many times.
 Its performance heavily depends on time quantum.
 Priorities cannot be set for the processes.

Problem 1:

Consider the set of 5 processes whose arrival time and burst time are given below. If the
CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average
waiting time and average turnaround time.

Process Id Arrival time Burst time


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3

Solution-
Ready Queue- P5, P1, P2, P5, P4, P1, P3, P2, P1
Gantt Chart-

Process Arrival Burst


Id CT TAT=CT-AT WT=TAT-BT RT
time time
P1 0 5 13 13 – 0 = 13 13 – 5 = 8 0
P2 1 3 12 12 – 1 = 11 11 – 3 = 8 2
P3 2 1 5 5–2=3 3–1=2 4
P4 3 2 9 9–3=6 6–2=4 7
P5 4 3 14 14 – 4 = 10 10 – 3 = 7 9
Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6 unit
Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit

Problem-02:

Consider the set of 6 processes whose arrival time and burst time are given below-If the
CPU scheduling policy is Round Robin with time quantum = 2, calculate the average waiting
time and average turnaround time.
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3

Solution-
Ready Queue- P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
Gantt chart-

Process Arrival Burst


Id CT TAT=CT-AT WT=TAT-BT RT
time time
P1 0 4 8 8–0=8 8–4=4 0
P2 1 5 18 18 – 1 = 17 17 – 5 = 12 2
P3 2 2 6 6–2=4 4–2=2 4
P4 3 1 9 9–3=6 6–1=5 8
P5 4 6 21 21 – 4 = 17 17 – 6 = 11 9
P6 6 3 19 19 – 6 = 13 13 – 3 = 10 13

Average Turn Around time = (8 + 17 + 4 + 6 + 17 + 13) / 6 = 65 / 6 = 10.84 unit
Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 = 7.33 unit
5. Priority Scheduling

o Out of all the available processes, CPU is assigned to the process having the
highest priority.
o In case of a tie, it is broken by FCFS Scheduling.
o Priority Scheduling can be used in both preemptive and non-preemptive
mode.
o The waiting time for the process having the highest priority will always be
zero in preemptive mode.
o The waiting time for the process having the highest priority may not be zero
in non-preemptive mode.
Advantages-
o It considers the priority of the processes and allows the important processes
to run first.
o Priority scheduling in pre-emptive mode is best suited for real time
operating system.

Disadvantages-
o Processes with lesser priority may starve for CPU.
o There is no idea of response time and waiting time.

Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-If
the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Solution-
Gantt Chart-
Process Arrival Burst Priority
CT TAT=CT-AT WT=TAT-BT RT
Id time time
P1 0 4 2 4 4–0=4 4–4=0 0
P2 1 3 3 15 15 – 1 = 14 14 – 3 = 11 12
P3 2 1 4 12 12 – 2 = 10 10 – 1 = 9 11
P4 3 5 5 9 9–3=6 6–5=1 4
P5 4 2 5 11 11 – 4 = 7 7–2=5 9

Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 unit
Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 unit

Problem-02
Consider the set of 5 processes whose arrival time and burst time are given below-If the
CPU scheduling policy is priority preemptive, calculate the average waiting time and
average turn around time. (Higher number represents higher priority).

Process Id Arrival time Burst time Priority


P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5

Solution-
Gantt Chart-

Process Arrival Burst Priority


CT TAT=CT-AT WT=TAT-BT RT
Id time time
P1 0 4 2 15 15 – 0 = 15 15 – 4 = 11 0
P2 1 3 3 12 12 – 1 = 11 11 – 3 = 8 1
P3 2 1 4 3 3–2=1 1–1=0 2
P4 3 5 5 8 8–3=5 5–5=0 3
P5 4 2 5 10 10 – 4 = 6 6–2=4 8

Average Turn Around time = (15 + 11 + 1 + 5 + 6) / 5 = 38 / 5 = 7.6 unit
Average waiting time = (11 + 8 + 0 + 0 + 4) / 5 = 23 / 5 = 4.6 unit
6. Multilevel Queue Scheduling
A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues. 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. Each queue
has its own scheduling algorithm.
Let us consider an example of a multilevel queue-scheduling algorithm with five queues:
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes

Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactive editing processes were all empty. If an interactive editing process entered the
ready queue while a batch process was running, the batch process will be pre-empted.

4.3 Deadlock

 Deadlock is a situation where a set of processes are blocked because each process is holding
a resource and waiting for another resource acquired by some other process.
 For example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
 Necessary conditions for deadlock

Deadlock can arise if following four necessary conditions hold simultaneously.

1. Mutual Exclusion: One or more than one resource are non-sharable means Only one process can
use at a time.
2. Hold and Wait: A process is holding at least one resource and waiting for another resource.
3. No Pre-emption: A resource cannot be taken from a process unless the process releases the
resource means the process which once scheduled will be executed till the completion and no other
process can be scheduled by the scheduler meanwhile.
4. Circular Wait: A set of processes are waiting for each other in circular form means All the
processes must be waiting for the resources in a cyclic manner so that the last process is waiting for
the resource which is being held by the first process.

 Deadlock Handling
The various strategies for handling deadlock are-
1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
4. Deadlock Ignorance

1. Deadlock Prevention
Deadlocks can be prevented by preventing at least one of the four required conditions:

 Mutual Exclusion
o Shared resources such as read-only files do not lead to deadlocks.
o Unfortunately, some resources, such as printers and tape drives, require
exclusive access by a single process.

 Hold and Wait


o To prevent this condition processes must be prevented from holding one or
more resources while simultaneously waiting for one or more others.

 No Preemption
o Preemption of process resource allocations can prevent this condition of
deadlocks, when it is possible.

 Circular Wait
o One way to avoid circular wait is to number all resources, and to require that
processes request resources only in strictly increasing (or decreasing) order.
2. Deadlock Avoidance
 In deadlock avoidance, the operating system checks whether the system is in safe
state or in unsafe state at every step which the operating system performs.
 The process continues until the system is in safe state.
 Once the system moves to unsafe state, the OS has to backtrack one step.
 In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.

3. Deadlock detection and recovery


 This strategy involves waiting until a deadlock occurs.
 After deadlock occurs, the system state is recovered.
 The main challenge with this approach is detecting the deadlock.

4. Deadlock Ignorance
 This strategy involves ignoring the concept of deadlock and assuming as if it does
not exist.
 This strategy helps to avoid the extra overhead of handling deadlock.
 Windows and Linux use this strategy and it is the most widely used method.

You might also like