Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views77 pages

Unit-2 Merged File

The document discusses CPU scheduling in operating systems, covering various scheduling criteria, algorithms, and multiple-processor scheduling. Key scheduling algorithms include First-Come, First-Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin, each with its advantages and disadvantages. It also highlights optimization criteria for selecting scheduling algorithms based on CPU utilization, throughput, turnaround time, waiting time, and response time.

Uploaded by

raisrinidhi22
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)
5 views77 pages

Unit-2 Merged File

The document discusses CPU scheduling in operating systems, covering various scheduling criteria, algorithms, and multiple-processor scheduling. Key scheduling algorithms include First-Come, First-Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin, each with its advantages and disadvantages. It also highlights optimization criteria for selecting scheduling algorithms based on CPU utilization, throughput, turnaround time, waiting time, and response time.

Uploaded by

raisrinidhi22
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/ 77

UNIT – 2 Part-1

CPU Scheduling

By
Dr Maj R B Kallam
Topics to be covered
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling

A Presentation on Operating systems by Dr Maj RB Kallam 2


CPU Scheduler:
• Preemptive Scheduling:

– The currently running process may be interrupted and moved to the ready
state by the Operating System.
– The decision to preempt may be performed when a new process arrives,
when an interrupt occurs or periodically on the basis of clock interrupt.

• Non Preemptive Scheduling:

– Once a process is in the running state, it continue to execute until it


terminates or blocks itself to wait for I/O or by requesting some OS
services.

A Presentation on Operating systems by


3
Dr Maj RB Kallam
Scheduling Criteria:
• CPU Utilization: We want to keep the CPU as busy as
possible. CPU utilization range from 0 to 100 percent.
• Throughput: It is number of processes that are completed
execution per unit time.
• Turnaround Time (Tr): It is an interval of time between the
process submission and its completion (Finishing time-arrival
time)
• Waiting Time: It is the sum of the time spent by the process
waiting in the ready queue (turnaround time – burst time).
• Response time: It is the time from the submission of a
request until the first response is produced.
• Service Time (Ts): Total CPU time required by a process.
• Normalized Turnaround time( Tr/Ts): It is the ratio of the
turnaround time (Tr) to service time (Ts).
4
A Presentation on Operating systems by Dr Maj RB Kallam
Scheduling Algorithms:
• FCFS
• SJF or SPN
• Priority
• Round Robin
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling

A Presentation on Operating systems by


5
Dr Maj RB Kallam
Optimization Criteria
In choosing which algorithm to use in a
particular situation, we must consider the
properties of the various algorithms. The criteria
include the following:
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time

A Presentation on Operating
systems by Dr Maj RB 6
Kallam
First-Come, First-Served (FCFS) Scheduling
• It is a non preemptive algorithm ,in this CPU is allocated to the processes
based on FCFS.
• Suppose that the processes arrive in the order: P1 , P2 , P3
• Assume that the arrival time is ‘0’ for all processes, calculate Turnaround time
(Tr), Waiting time (Wt), Average Tr and Average Wt.

Process Burst Time


P1 24
P2 3
P3 3
The Gantt Chart for the schedule is:

A Presentation on Operating systems by Dr Maj RB Kallam 7


Process Arrival Burst Finishing Turnaround Waiting Time
ID Time (At) time (Bt) Time (Ft) Time (Tr=Ft-At) (Wt=Tr-Bt)

P1 0 24 24 24 0

P2 0 3 27 27 24

P3 0 3 30 30 27

ATr=81/3=27 AWt=51/3=17

Note: ATr- Average Turnaround Time, AWt- Average Waiting Time

• Advantage: It is a simple scheduling algorithm among all.


• Disadvantage: The FCFS scheduling algorithm is non preemptive. 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.
8
A Presentation on Operating systems by Dr Maj RB Kallam
• Ex Q2: If the burst time for 4 processes are as given below,
apply FCFS and calculate Turnaround time (Tr), Waiting time
(Wt), Average Tr and Average Wt.

A Presentation on Operating systems by


9
Dr Maj RB Kallam
Shortest-Job-First (SJF)or SPN Scheduling
• It is a non preemptive policy, in which the
process with the shortest expected processing
time is selected for next.
Disadvantage:
• It may leads to the starvation of longer
processes.
• Starvation: A condition in which a process is
indefinitely delayed because other processes
are always given preferences.
A Presentation on Operating systems by
10
Dr Maj RB Kallam
Example of Non-Preemptive SJF
Q1: If the arrival time and burst time for 4 processes are as given
below, apply SJF and calculate Turnaround time (Tr), Waiting
time (Wt), Average Tr and Average Wt.

The Gantt Chart for the schedule is:

11
A Presentation on Operating systems by Dr Maj RB Kallam
Process Arrival Burst Finishing Turnaround Waiting Time
ID Time (At) time (Bt) Time (Ft) Time (Tr=Ft-At) (Wt=Tr-Bt)

P1 0 7 7 7 0

P2 2 4 12 10 6

P3 4 1 8 4 3

P4 5 4 16 11 7

ATr=32/4=8 AWt=16/4=4

A Presentation on Operating systems by Dr Maj RB Kallam 12


Ex Q2: If the burst time for 4 processes are as given below, apply
SJF and calculate Turnaround time (Tr), Waiting time (Wt),
Average Tr and Average Wt.

A Presentation on Operating systems by


13
Dr Maj RB Kallam
Shortest-Remaining-Time-First (SRTF) or Shortest-Job-
First (SJF) with Preemption
If a new process arrives with CPU burst length less than
remaining time of current executing process, then the current
process is preempted.
Example of Preemptive SJF/ SRTF:
Q: If the arrival time and burst time for 4 processes are as given below, apply SRTF
and calculate Turnaround time (Tr), Waiting time (Wt), Average Tr and AverageWt.

The Gantt Chart for the schedule is:

14
A Presentation on Operating systems by Dr Maj RB Kallam
Process Arrival Burst Finishing Turnaround Waiting Time
ID Time (At) time (Bt) Time (Ft) Time (Tr=Ft-At) (Wt=Tr-Bt)

P1 0 7 16 16 9

P2 2 4 7 5 1

P3 4 1 5 1 0

P4 5 4 11 6 2

ATr=28/4=7 AWt=12/4=3

A Presentation on Operating systems by


15
Dr Maj RB Kallam
Ex Q2: If the arrival time and burst time for 4 processes are as given below,
apply SRTF and calculate Turnaround time (Tr), Waiting time (Wt), Average
Tr and AverageWt.

A Presentation on Operating systems by Dr Maj RB Kallam 16


Priority Scheduling
• It can be either Preemptive or non preemptive
• A priority number (integer) is associated with each process
• Non preemptive :The CPU is allocated to the process with the
highest priority (smallest integer  highest priority).

Disadvantage:
Starvation – low priority processes may never execute if we
continue to accept highest priority processes into the queue.

A Presentation on Operating systems by


17
Dr Maj RB Kallam
Example of Non Preemptive Priority scheduling:
Q: If the burst time and priority for 5 processes are as given below, apply Non
Preemptive Priority scheduling and calculate Turnaround time (Tr), Waiting time
(Wt), Average Tr and AverageWt.

The Gantt Chart for the schedule is:

A Presentation on Operating systems by Dr Maj RB Kallam 18


Process Burst time Priority Finishing Turnaround Waiting Time
ID (Bt) Time (Ft) Time (Tr=Ft-At) (Wt=Tr-Bt)

P1 10 3 16 16 6

P2 1 1 1 1 0

P3 2 4 18 18 16

P4 1 5 19 19 18

P5 5 2 6 6 1

ATr=60/5=12 AWt=41/5=8.2
A Presentation on Operating systems by Dr Maj RB Kallam 19
• Ex Q2: If the burst time and priority for 5 processes are as
given below, apply Non Preemptive Priority scheduling and
calculate Turnaround time (Tr), Waiting time (Wt), Average Tr
and Average Wt.

Process ID Burst Time Priority


P1 21 2
P2 3 1
P3 9 4
P4 5 3
P5 7 5

A Presentation on Operating systems by


20
Dr Maj RB Kallam
Preemptive Priority Scheduling
• Preemptive Priority Scheduling: a preemptive priority
scheduling algorithm will preempt the CPU if the priority of
the newly arrived process is higher than the priority of the
currently running process.
Example for Preemptive Priority scheduling:
Q: If the arrival time, burst time and priority for 4 processes are as given below,
apply Preemptive Priority scheduling and calculate Turnaround time (Tr), Waiting
time (Wt), Average Tr and AverageWt.

Process ID Arrival Burst Time Priority


Time
P1 0 10 2
P2 1 3 3
P3 2 5 4
P4 3 4 1

21
A Presentation on Operating systems by Dr Maj RB Kallam
The Gantt Chart for the schedule is:

P1 P1 P1 P4 P1 P2 P3

0 1 2 3 7 14 17 22

Process Arrival Burst Priority Finishing Turnaround Waiting Time


ID Time time Time (Ft) Time (Tr=Ft- (Wt=Tr-Bt)
(Bt) At)
P1 0 10 2 14 14 4

P2 1 3 3 17 16 13

P3 2 5 4 22 20 15

P4 3 4 1 7 4 0

ATr=54/4=13.5 Awt=32/4 =8

A Presentation on Operating systems by


22
Dr Maj RB Kallam
• Ex Q2: If the Arrival time, burst time and priority for 5
processes are as given below, apply Preemptive Priority
scheduling and calculate Turnaround time (Tr), Waiting time
(Wt), Average Tr and Average Wt.

Process ID Arrival Burst Time Priority


Time
P1 0 21 2
P2 2 3 1
P3 4 9 4
P4 6 5 3
P5 8 7 5

A Presentation on Operating systems by


23
Dr Maj RB Kallam
Round Robin (RR)
• It is a Preemptive scheduling algorithm.
• Each process gets a small unit of CPU time (time quantum),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready
queue.
• The ready queue is treated as a circular queue.
• The scheduler goes around the ready queue, allocating the
CPU to each process for a time interval of up to 1 time
quantum.
• It improves the response time, but may leads to the wastage
of CPU time with context switching.

A Presentation on Operating systems by Dr Maj RB Kallam 24


• Ex Q1: If the burst time for 4 processes are as given below,
apply Round robin scheduling algorithm (with Quantum =20)
and calculate Turnaround time (Tr), Waiting time (Wt),
Average Tr and Average Wt.

The Gantt Chart for the schedule is:

A Presentation on Operating systems by


25
Dr Maj RB Kallam
Process ID Burst time Finishing Turnaround Time Waiting Time
(Bt) Time (Ft) (Tr=Ft-At) (Wt=Tr-Bt)

P1 20 20 20 0

P2 35 90 90 55

P3 25 95 95 70

P4 15 75 75 60

ATr=280/4=70 AWt=185/4=46.25

A Presentation on Operating systems by Dr Maj RB Kallam 26


• Ex Q2: If the burst time for 5 processes are as given below,
apply Round robin scheduling algorithm (with Quantum =5)
and calculate Turnaround time (Tr), Waiting time (Wt),
Average Tr and Average Wt.

Process ID Burst Time


P1 21
P2 3
P3 9
P4 5
P5 7

A Presentation on Operating systems by


27
Dr Maj RB Kallam
Ex Q3: Consider there are 5 processes and their arrival
time and service times are given in the table. calculate
the finishing time, Turnaround time, waiting time,
average Tr and average Wt using FCFS, SPN, SRTF, RR
algorithms.

Process Arrival Time Service Time


A 0 3
B 2 6
C 4 4
D 6 5
E 8 2
A Presentation on Operating systems by
28
Dr Maj RB Kallam
Multilevel Queue
• Ready queue is partitioned into separate queues based on the
requirements.

• Each queue has its own scheduling algorithm,


Ex: RR, FCFS, Priority, etc

• Scheduling must be done between the queues.

A Presentation on Operating systems by


29
Dr Maj RB Kallam
Multilevel Queue Scheduling

A Presentation on Operating
systems by Dr Maj RB 30
Kallam
Multilevel Feedback Queue
• A process can move between the various queues
• Multilevel-feedback-queue scheduler defined by the following
parameters:
– number of queues
– scheduling algorithms for each queue
– method used to determine when to upgrade a process to
higher priority queue.
– method used to determine when to demote a process to
lower priority queue.
– method used to determine which queue a process will
enter when that process needs service

A Presentation on Operating systems by


31
Dr Maj RB Kallam
Multilevel Feedback Queues

A Presentation on Operating systems by


32
Dr Maj RB Kallam
Multiple-Processor Scheduling
• CPU scheduling is more complex when multiple CPUs are
available.
• A multiprocessor systems typically has two or more
homogeneous processors
• Major advantage is Load sharing : In Symmetric
multiprocessing systems, it is necessary to keep the workload
balanced among all processors to fully utilize the available
processors
There are Two types of load sharing:
• Push migration - a specific task periodically checks the load on
each processor and rebalances the load if necessary .
• Pull migration - an idle processor can pull waiting tasks from a
busy processor
33
A Presentation on Operating systems by Dr Maj RB Kallam
Approaches to Multiple-Processor Scheduling
• Asymmetric multiprocessing – In this approach all scheduling decisions,
I/O processing, and other system activities handled by a single processor—
the master server. The other processors execute only user code. It is simple
because only one processor accesses the system data structures, reducing
the need for data sharing.
• Symmetric Multiprocessing (SMP): In this each processor is self
scheduling. all processes may be in common ready queue or each process
may have its own private queue of ready processes.

Processor Affinity: It is a method of avoiding process migration from one


processor to other or allowing process to run only in a fixed processor.
• Soft affinity - OS will attempt to keep a process running on the same
processor, however there is no guarantee.
• Hard affinity - OS will force a process to continue running on the same
processor.

A Presentation on Operating systems by


34
Dr Maj RB Kallam
System Calls
What is System Call in Operating System?

• Programming interface to the services provided by the OS


---i.e., interface provided to applications
• Typically written in a high-level language (C or C++)
• Are called by a running program to get services
• Even a simple program may make a lot of calls per second.

In computing, a system call is the programmatic way in which a


computer program requests a service from the kernel of the operating
system it is executed on.

A system call is a way for programs to interact with the operating


system. A computer program makes a system call when it makes a
request to the operating system’s kernel. System call provides the
services of the operating system to the user programs via Application
Program Interface (API).

System calls are the only entry points into the kernel system. All
programs needing resources must use system calls.

Fig: System Calls in Operating System


Example of System Call

For example if we need to write a program code to read data from one
file, copy that data into another file. The first information that the
program requires is the name of the two files, the input and output
files.

In an interactive system, this type of program execution requires some


system calls by OS.

 First call is to write a prompting message on the screen


 Second, to read from the keyboard, the characters which define
the two files.

System call sequence to copy the contents of one file to another file

Fig: Example of how system calls are used


Why do you need System Calls in OS?

Following are situations which need system calls in OS:

 Reading and writing from files demand system calls.


 If a file system wants to create or delete files, system calls are
required.
 System calls are used for the creation and management of new
processes.
 Network connections need system calls for sending and
receiving packets.
 Access to hardware devices like scanner, printer, need a system
call.

System Call Implementation and Calling

Typically,

o a number associated with each system call


o Number used as an index to a table: System Call table
o Table keeps addresses of system calls (routines)
o System call runs and returns

Caller does not know system call implementation

o Just knows interface

Fig: The handling of a user application invoking the open() system call
Types of System calls
Here are the five types of system calls used in OS:

 Process Control
 File Management
 Device Management
 Information Maintenance
 Communications

Process Control
The types of system calls provided by an operating system: under process and
job control are:

 end and abort


 load and execute
 create Process and terminate Process
 get process attributes, set process attributes
 wait for time
 wait and signal Event
 allocate and free memory

File Management
The types of system calls provided by an operating system: under File
manipulations are:

 Create a file
 Delete file
 Open and close file
 Read, write, and reposition
 Get and set file attributes

Device Management
The types of system calls provided by an operating system: under device
management are:

 Request and release device


 Read, write, and reposition
 Logically attach/ detach devices
 Get and Set device attributes
Information Maintenance

The types of system calls provided by an operating system: under


Information maintenance are:

 get or set time and date


 get system data, set system data
 get process, file and device attributes
 set process, file and device attributes

Communication:

These types of system calls are specially used for interprocess


communications.

 create, delete communications connections


 send, receive messages
 Help OS to transfer status information
 attach or detach remote devices

Protection: provides a mechanism for controlling access to the


resources.

 get permission
 set permission
 allow user
 deny user

Important System Calls Used in OS


wait()

In some systems, a process needs to wait for another process to


complete its execution. This type of situation occurs when a parent
process creates a child process, and the execution of the parent
process remains suspended until its child process executes.

The suspension of the parent process automatically occurs with a


wait() system call. When the child process ends execution, the control
moves back to the parent process.
fork()

Processes use this system call to create processes that are a copy of
themselves. With the help of this system Call parent process creates a
child process, and the execution of the parent process will be
suspended till the child process executes.

exec()

The exec() system call is used to make the processes. When the exec()
function is used, the currently running process is terminated and
replaced with the newly formed process. In other words, only the new
process persists after calling exec().

kill():

The kill() system call is used by OS to send a termination signal to a


process that urges the process to exit. However, a kill system call does
not necessarily mean killing the process and can have various
meanings.

exit():
The exit() system call is used to terminate program execution.
Specially in the multi-threaded environment, this call defines that the
thread execution is complete. The OS reclaims resources that were
used by the process after the use of exit() system call.
Unit – 2 Part - 3

Deadlocks
By
Prof. RB Kallam
Topics to be covered
• Deadlocks
• System Model
• Deadlocks Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 2


Deadlock :
• In multiprogramming environment several processes may
compete for the same resource.
• If a process request for resources and if the resources are not
available at that time then the process enters into a wait state.
• It may happen that waiting process will never again change the
state, because the resources they have requested are held with
other waiting processes.
• This situation is called a deadlock.
• Deadlock is defined as the permanent blocking
of a set of processes that compete for system
resources.

Resource allocation graph


3 with
a dead lock
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
System Model:
• A system consists of a finite number of resources to be distributed
among a number of competing processes.
• The resources are partitioned into several types, each consisting
of some number of identical instances.
• Reusable: A resource can be safely used by one process at a time
and is not consumed by that use. Processes obtain resources that
they later release for reuse by others (processors, memory, files,
devices, databases, and semaphores).
• Consumable: these can be created and destroyed. When a
resource is acquired by a process, it is consumed (interrupts,
signals, messages, etc).
• A preemptable resource: is one that can be taken away from the
process owning it with no ill effects. Memory (also CPU) is an
example of a preemptable resource.
• A nonpreemptable resource, in contrast, is one that cannot be
taken away from its current owner without causing the
computation to fail (printer, CD-R(W)floppy disk).
4
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
 A process must request a resource before
using it and must release the resource after
using it.
– A process may request as many resources as it requires to carry
out its designated task.
– Obviously, the number of resources requested may not exceed the
total number of resources available in the system.
Under the normal mode of operation, a process may utilize a
Under
resource in only the following sequence:
Request
Use
Release
 The request and release of resources are system
calls. Examples are the request() and release()
device, open() and close() file, and allocate() and
free() memory
A Presentation system
on Operating systems by Dr calls.
Ravindra Babu Kallam, KITS Singapur
5
Deadlock Characterization:

• Necessary Conditions:
• Mutual exclusion: At least one resource must be held in
a non sharable mode; that is only one process at a time
can use the resource.

• Hold and wait: There must exist a process that is


holding at least one resource and is waiting to acquire
additional resources that are currently being held by
other processes.

6
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
• No preemption: Resources cannot be preempted; that
is a resource can be released only voluntarily by the
process holding it, after that process has completed
its task.

• Circular wait: There must exist a set {P0, P1,.., Pn } of


waiting processes such that P0 is waiting for a
resource that is held by P1, P1 is waiting for a
resource that is held by P2,…. Pn, and Pn is waiting
for a resource that is held by P0.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 7


• Resource Allocation graph:

•Deadlocks can be described more precisely in terms


of a directed graph called a system resource allocation
graph.

•This graph consists of a set of vertices V and a set of


edges E.

•The set of vertices V is partitioned into two different


types of nodes P = {P1, P2,…., Pn }, the set consisting of
all the active processes in the system, and R= { R1,
R2,…, Rm}, the set consisting of all resource types in
the system.

•A directed edge Pi Rj is called a request edge; and


a directed edge Rj Pi is called an assignment edge.
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 8
The resource allocation graph shown
below have the following situations:
R1 R3
• The sets P, R and E:
• P={P1,P2,P3}
• R={ R1,R2,R3,R4}
• E={P1 R1, P2 R3, R1 P2,
R2 P2, R2 P1,R3 P3} P1 P2 P3
• Resource instances:
• One instance of resource type
R1
• Two instance of resource type
R2 R2
• One instance of resource type R4
R3
• Three instance of resource type Resource allocation graph
R4

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 9


• Process states:

• Process P1 is holding an instance of resource type R2, and is waiting for


an instance of resource type R1.

• Process P2 is holding an instance of resource type R1and R2, and is


waiting for an instance of resource type R3.
• Process P3 is holding an instance of R3.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 10


R1 R3

P1 R1 P2 R3 P3 R2 P1
P2 R3 P3 R2 P2
P1 P2 P3

R2
R4
Resource allocation graph with a dead lock

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 11


Methods for handling deadlocks:
•Principally there are three different methods
•We can use a protocol to ensure that the system
will never enter a deadlock state.
•We can allow the system to enter a deadlock
state and then recover.
•We can ignore the problem all together, and
pretend that deadlock never occur in the
system.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 12


Deadlock Prevention:
•Deadlock prevention is same as take the
preventive methods before attacking the
deadlock.
•For a deadlock to occur, each of the four
necessary conditions must hold., by ensuring
that at least one of these conditions can’t
hold, we can prevent the occurrence of the
deadlock.

13
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
•Mutual exclusion:
•Mutual exclusion means only one process can
use the resource at a time, it means resources
are not sharable by the number of processes at
a time.
•We can deny this condition with simple
protocol i.e. “Convert the all non sharable
resources to sharable resource". So this
condition is not satisfied by the deadlock,
hence we can prevent the deadlock.
•But it is not possible all the time.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 14


Hold and wait:

• It means each and every process in the dead lock


state, must hold at least one resource and wait for at
least one resource.

• We can deny this condition with a small protocol.


That is “ A process request the resources only when
the process has none”

• But it is not possible all the time.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 15


No preemption:
• It means resources are not released in the
middle of the processing of a resource.
• To ensure that this condition does not hold, we
can use the following protocol.
• If a process that is holding some resources requests
another resource that cannot be immediately
allocated to it, then all resources currently being held
are preempted.
• That is these resources are released and added to
the list of resources for which the process is waiting.
• The process will be restarted only when it regains its
old resources, as well as the new once that it is
requesting

16
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Circular wait:
• One way to ensure that the circular wait condition never
holds is to impose a total ordering of all resource types,
and to require that each process request resources in
an increasing order.
• For this we have two protocols:
• Whenever “ a processes request an instance of resource
type Rj, it has released any resource Ri such that
F(Ri)>F(Rj).
• A process can initially request any number of instances of
a resource type, say Ri. After that, the process can
request the instances of resource type Rj, if and only if
• F(Rj)>F(Ri)”.
• If these two protocols are used, then the circular wait
condition cannot hold.

17
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Deadlock avoidance:
• It is a dynamic approach to escape from deadlock .

• With this, if a process request for resources the


avoidance algorithm checks before the allocation of
resources about the state of the system.

• If the state is safe, the system allocates the resources to


the requesting process otherwise do not allocates the
resources.

• So taking care before the allocation


is said to be deadlock avoidance. Unsafe
Deadlock

Safe

Safe, unsafe, and deadlock state space


A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 18
Banker’s algorithm:
• It is a dead lock avoidance algorithm, the name was chosen because
the bank never allocates more than available cash.
• Available:
• A Vector length m indicates the number of available resources of
each type.
• If Available[ j ] = k, there are k instances of resource type Rj
available.
• Max:
• An n x m matrix defines the maximum demand of each process.
• If Max [i, j] =k, then Pi may request at most k instances of
resource type Rj

19
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
• Allocation:
• An n x m matrix defines the number of resources of
each type currently allocated to each process.
• If Allocation [ i , j ] = k, then process Pi is currently
allocated k instances of resource type Rj.

• Need:
• An n x m matrix indicates the remaining resource need
of each process.
• If Need [ i , j ] = k, then Pi may need k more instances
of resource type Rj to complete its task.
• Note that Need[ i , j ]=Max[ i , j ] –Allocation [i, j ].

20
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Safety Algorithm:
 1. Let Work and Finish be vectors of length m and n, respectively.
Initialize work:= Available. and Finish [ i]:=false; For i= 0,1,2,..n-1.
 2. Find an i such that both
 Finish[i] == false
 Need i <= Work.
If If no such exist, go to step4.
 3. Work:= Work + Allocation i
Finish[i] := true
go to step 2
 4. If Finish [i] == True, for all i, then the system is in safe state.

 Note: This requires an order of mxn2 operations to detect whether the


system is in safe state.

21
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Algorithm – Example:
• Consider a system with 5 processes P0,P1,P2,P3 and P4, and 3
resources A,B, C. Resource type A has 10 instances, Resources type
B has 5 instances and Resources C has 7 instances. Suppose that, at
time, the following snap shot of the system has been taken.

Allocation MAX
Calculate : A B C A B C
•Available matrix
•Need matrix P0 0 1 0 7 5 3
•And find whether the system P1 2 0 0 3 2 2
is in the safe state or not. P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3

22
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Process Allocation Max Need (Max-
ID Allocation)
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
7 2 5

A B C
Total 10 5 7
Execution sequence for the
Allocated -7 -2 -5
system to be in safe state:
P1, P3,P4,P0,P2
Available 3 3 2
23
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
A B C
Available (X) 3 3 2
Resource allocated to P1(Y) 2 0 0
Available (X+Y) 5 3 2
Resource allocated to P3 2 1 1

Available 7 4 3
Resource allocated to P4 0 0 2
Available 7 4 5
Resource allocated to P0 0 1 0
Available 7 5 5
Resource allocated to P2 3 0 2
Available 10 5 7

24
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Ex Q: Consider the following snapshot of a system:
Proces Available
Allocation Max
s ID
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Answer the following questions using the banker’s algorithm:
a. What is the content of the matrix Need?
b. Is the system in a safe state?
c. If a request from the process P1 arrives for (0,4,2,0) can the request be
granted immediately?

25
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Proces
s ID Allocation Max
Need
Matrix
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 0 0 0 0
P1 1 0 0 0 1 7 5 0 0 7 5 0
P2 1 3 5 4 2 3 5 6 1 0 0 2
P3 0 6 3 2 0 6 5 2 0 0 2 0
P4 0 0 1 4 0 6 5 6 0 6 4 2
2 9 10 12
A B C D

Available 1 5 2 0

Allocated 2 9 10 12

Total Resources 3 14 12 12

26
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
A B C D

Available (X) 1 5 2 0

Resource allocated to P0(Y) 0 0 1 2

Available (X+Y) 1 5 3 2

Resource allocated to P2 1 3 5 4

Available 2 8 8 6

Resource allocated to P3 0 6 3 2

Available 2 14 11 8

Resource allocated to P4 0 0 1 4

Available 2 14 12 12

Resource allocated to P1 1 0 0 0

Available
3 14 12 12

Execution sequence: P0, P2,P3,P4 and P1


System is in the safe state
If a request from the process P1 arrives for (0,4,2,0), the request
can be granted immediately.
27
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Deadlock Detection:
•Single instance of each resource type:
• If all resources have a single instance, then we can
define a Deadlock algorithm that uses a variant of the
resource allocation graph, called a wait - for graph.
• We obtain this graph from the resource allocation
graph by removing the nodes of type resource and
collapsing the edges.
• More precisely, an edge from Pi to Pj in a wait - for
graph implies that Pi is waiting for process Pj to
release a resource that Pi needs.
• A Deadlock exist in the system if and only if the wait -
for graph contains a cycle.
• To detect the deadlock, the system needs to maintain
the wait - for graph and periodically to invoke an
algorithm that searches for a cycle in the graph.

28
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
P5

R1 R3 R4 P5

P1 P2 P3 P1 P2 P3

R2 P4 R5 P4
Resource allocation graph Corresponding wait - for graph

29
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
•Several instances of a resource type:
• We need a deadlock detection algorithm with
several time varying data structures that are
similar to those used in banker’s algorithm.
• Available: A vector of length m indicates the
number of available resources of each type.
• Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each
process.
• Request: An n x m matrix indicates the current
request of each process. If Request [ i, j] = k, then
process Pi is requesting k more instances of
resource type Rj.

30
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Banker's algorithm for Deadlock detection
• 1. Let Work and Finish be vectors of length m and n, respectively.
Initialize work:= Available. For i= 1,2,..n, if Allocation = 0, then Finish [
i]:=false; otherwise, Finish[i]:= true.
• 2. Find an index i such that both
• Finish[i] == false
• Request i <= Work. If no
such exist, go to step4.
• 3. Work:= Work + Allocation i
Finish[i] := true go to step 2
• 4. If Finish [i] = false, for some i, 1<= i <= n, then the system is in a
deadlock state. Moreover, Finish [ i]:=false, then process Pi is
deadlocked.
• Note: This requires an order of mxn2 operations to detect whether the
system is in deadlocked state.

31
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Q: Consider a system with 5 processes P0,P1,P2,P3 and P4, and 3
resources A,B, C. Resource type A has 7 instances, Resources type B
has 2 instances and Resources C has 6 instances. Suppose that, at time
T0, we have the following resource allocation state.

Allocation Request Available


ABC ABC ABC
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Answer the following questions:
a. What is the content of a matrix Max?
b. Is the system is in safe state?
c. If a request from process P2 arrives for (0,0,1) is the system is in safe state?

32
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Allocation Request Available Max (Allocation+ Req)
AB C AB C AB C AB C
P0 0 1 0 0 0 0 0 0 0 010
P1 2 0 0 2 0 2 402
P2 3 0 3 0 0 0 303
P3 2 1 1 1 0 0 311
P4 0 0 2 0 0 2 004
7 2 6

A B C

Available 0 0 0

Allocated 7 2 6

Total Resources 7 2 6

33
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
A B C
Available (X) 0 0 0
Resource allocated to P0(Y) 0 1 0
Available (X+Y) 0 1 0
Resource allocated to P2 3 0 3
Available 3 1 3
Resource allocated to P3 2 1 1
Available 5 2 4
Resource allocated to P4 0 0 2
Available 5 2 6
Resource allocated to P1 2 0 0
Available 7 2 6

System is in the safe state and the execution sequence is P0,


P2, P3, P4 and P1
If a request from process P2 arrives for (0,0,1); the system is
not in safe state as the available is < request, i.e (0 1 0 < 0 0 1)
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 34
H/W Q: Consider a snapshot of a system

Allocation Max Available


ABC D ABC D ABC D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

Answer the following questions using Bankers algorithm?


a. What is the content of a matrix Need?
b. Is the system is in safe state?
c. If a request from process P1 arrives for (0,4,2,0) can the
request be granted immediately?

35
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Recovery from Deadlock:
•Process Termination
• Abort all deadlocked processes: This method
clearly will break the deadlock cycle, but at great
expense.
• Abort one process at a time until the deadlock
cycle is eliminated: This method incurs
considerable overhead, since, after each process
is aborted, a deadlock-detection algorithm must
be invoked to determine whether any processes
are still deadlocked.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 36


•Resource Preemption
• Selecting a victim: we must determine a victim
and preempt it’s all resources and restart.
• Rollback: We must roll back the resources of a
process to some safe state and restart it from that
state.
• Starvation problem: In a system where victim
selection is based primarily on cost factors, it may
happen that the same process is always picked
as a victim. As a result, this process never
completes its designated task and leads to the
starvation.

A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur 37

You might also like