CPU Scheduling and Deadlock Management
CPU Scheduling and Deadlock Management
Shyam B. Verma
(Assistant Professor)
Syllabus [Unit-3]
CPU Scheduling: Scheduling Concepts, Performance Criteria, Process States, Process
Transition Diagram, Schedulers, Process Control Block (PCB), Process address space,
Process identification information, Threads and their management, Scheduling Algorithms,
Multiprocessor Scheduling.
Deadlock: System model, Deadlock characterization, Prevention, Avoidance and detection,
Recovery from deadlock.
CPU Scheduling
Basic Concepts
Scheduling Criteria
Process States
Process Transition Diagram
Schedulers
Process Control Block (PCB)
Process address space
Process identification information
Threads and their management
Scheduling Algorithms
Multiple-Processor Scheduling
Objectives
To introduce CPU scheduling, which is the basis for multiprogramming operating
systems
To describe various CPU-scheduling algorithms
To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular
system
To examine the scheduling algorithms of several operating systems
Introduction
In the uniprogrammming systems like MS DOS, when a process waits for any I/O
operation to be done, the CPU remains idol. This is an overhead since it wastes the time and
causes the problem of starvation. However, In Multiprogramming systems, the CPU doesn't remain
idle during the waiting time of the Process and it starts executing other processes. Operating
System has to decide which process the CPU will be given.
CPU scheduling is the basis of multiprogramming O/S. By switching the CPU among
processes, the O/S can make the computer more productive.
Multiple processes can be stored and executed simultaneously by context switching.
Rest of the processes will wait until the CPU is free and can be rescheduled.
Basic Concepts
Maximum CPU utilization obtained with
multiprogramming.
CPU–I/O Burst Cycle – Process
execution consists of a cycle of CPU
execution and I/O wait
CPU burst followed by I/O burst
Process States
As a process executes, it changes it’s state
New: The process is being created
Ready: The process is waiting to be assigned to a processor
Running: Instructions are being executed
Block or Waiting: The process is waiting for some event to occur (I/O completion or
reception of signal() )
Exit/Terminated: The process has finished [Link] PCB will be deleted.
Suspended Ready: To make room for new processes in main memory, some processes will be
shifted to secondary memory.
Suspended Wait or Suspended Block: To make room in Block or Waiting state, some
processes will be shifted to secondary memory.
Process Scheduling
The process of handling the removal of an active process from the CPU and selecting
another process based on a specific strategy.
Process Scheduling is an integral part of Multi-programming applications. Such operating
systems allow more than one process to be loaded into memory at a time and the
loaded shared CPU process uses repetition time.
There are three types of process schedulers:
Long term or Job Scheduler
Short term or CPU Scheduler
Medium-term Scheduler
Long term Scheduler
Long term scheduler is also known as job scheduler. It chooses the processes from the
pool (secondary memory) and keeps them in the ready queue maintained in the
primary memory.
Long Term scheduler mainly controls the degree of Multiprogramming. The purpose
of long-term scheduler is to choose a perfect mix of I/O bound and CPU bound
processes among the jobs present in the pool.
If the job scheduler chooses more I/O bound processes then all of the jobs may reside in
the blocked state all the time and the CPU will remain idle most of the time. This will
reduce the degree of Multiprogramming. Therefore, the Job of long term scheduler is
very critical and may affect the system for a very long time.
Short-term Scheduler
Short-term scheduler also known as CPU scheduler, selects a job among many
processes in ready queue, and allocates the CPU
Queue may be ordered in various ways.
CPU scheduling decisions may take place when a process:
1. Process admitted from new to ready state
2. Switches from suspended ready to ready state
3. Switches from running to ready state
4. Switches from waiting to ready state
5. When a process terminates after completion
Scheduling under 1 and 5 are nonpreemptive
All other scheduling is preemptive
Consider interrupts occurring during crucial OS activities
Preemptive Scheduling
Preemptive Scheduling is an approach where tasks are assigned based on their priorities.
In this type of scheduling, the execution of a higher priority task takes precedence over a
lower priority one, even if the latter is already running. The lower priority task is put on
hold until the higher priority task is completed.
Non-Preemptive Scheduling
Non-preemptive Scheduling is a method of CPU scheduling in which a process takes
control of a resource and retains it until the process is terminated or transitions to a
waiting state.
Medium Term Scheduler
Medium term scheduler takes care of the swapped out processes. If the running state
processes needs some I/O time for the completion then there is a need to change its state
from running to waiting.
It reduces the degree of multiprogramming. The swapping is necessary to have a perfect
mix of processes in the ready queue.
This MTS removes processes from memory and placed in secondary memory.
This technique is called swapping.
Swapping is necessary to:
Improve the process mix
To improve memory utilization
To make memory available for other processes
Dispatcher
A dispatcher is a special program which comes into play after the scheduler. When the
scheduler completes its job of selecting a process, it is the dispatcher which takes that process to
the desired state/queue. The dispatcher is the module that gives a process control over the CPU
after it has been selected by the short-term [Link] function involves the following:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
Dispatch latency – time taken by dispatcher to stop one process and start another process. The dispatcher
should be fast as possible because it is involved during every process switch.
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – No. of processes that complete their execution per time unit
Turnaround time – amount of time to execute a particular process
Waiting time – amount of time a process has been waiting in the ready queue
Response time – amount of time it takes from when a request was submitted until the
first response is produced, not output (for time-sharing environment)
Goals of Scheduling Algorithms
Utilization of CPU at maximum level. Keep CPU as busy as possible.
Allocation of CPU should be fair.
Throughput should be Maximum. i.e. Number of processes that complete their
execution per time unit should be maximized.
Minimum turnaround time (TAT), i.e. time taken by a process to finish execution
should be the least.
There should be a minimum waiting time (WT) and the process should not starve in
the ready queue.
Minimum response time (RT). It means that the time when a process produces the
first response should be as less as possible.
Process Concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost interchangeably
A Program does nothing unless its instructions are executed by a CPU. A program
in execution is called a process. In order to accomplish its task, process needs the
computer resources.
There may exist more than one process in the system which may require the same
resource at the same time. Therefore, the operating system has to manage all the
processes and the resources in a convenient and efficient way.
Program is passive entity stored on disk (executable file), process is active
Program becomes process when executable file loaded into memory
Execution of program started via GUI mouse clicks, command line entry of its name, etc
One program can be several processes
Consider multiple users executing the same program
The task of O/S is to:
Schedule processes and threads on the CPUs.
Create and delete both user and system processes.
Suspend and resume processes.
Provide mechanisms for process synchronization.
Provide mechanisms for process communication.
Process in Memory
•Every process in memory has a process
boundary.
•The data section contains global variables.
•The text field contains the program code
•The Heap is used for dynamic allocation of
memory
•The stack contains temporary data such as:
• Method parameters
• Return address
• Local variables
Attributes of a Process
1. Process ID: When a process is created, a unique id is assigned to the process which is used for unique
identification of the process in the system.
2. Program Counter: A program counter stores the address of the last instruction of the process on which the
process was [Link] CPU uses this address when the execution of this process is resumed.
3. Process State: The Process, from its creation to the completion, goes through various states which are new,
ready, running and waiting.
4. Priority: Every process has its own priority. The process with the highest priority among the processes gets
the CPU [Link] is also stored on the PCB.
5. General Purpose Registers: Every process has its own set of registers which are used to hold the data
which is generated during the execution of the process.
6. List of open files: During the Execution, Every process uses some files which need to be present in the
main memory. OS also maintains a list of open files in the PCB.
7. List of open devices: OS also maintain the list of all open devices which are used during the execution of
the process.
Operations on a Process
Creation of Process
Scheduling of Process
Execution of Process
Killing/Deletion of Process
Process Control Block (PCB)
Information associated with each process
(also called task control block)
Process state – running, waiting, etc
Program counter – location of next instruction to
execute
CPU registers – contents of all process-centric registers
CPU scheduling information- priorities, scheduling
queue pointers
Memory-management information – memory
allocated to the process
Accounting information – Information about the
resources used by the process, such as CPU time and
memory usage
I/O status information – I/O devices allocated to
process, list of open files
Process Control Block (PCB) is an important data structure used by the OS to manage
and control the execution of processes. It is also known as Task Control Block (TCB) in
some OS. A PCB contains all the necessary information about a process, including its
process state, program counter, memory allocation, open files, and CPU
scheduling information.
The main purpose of a PCB is to enable the OS to manage multiple processes efficiently by
keeping track of the state of each process and allocating system resources accordingly.
When a process is created, the OS creates a PCB for that process and stores all the
necessary information about the process in it. The OS then uses the information in the PCB
to manage the process and ensure that it runs efficiently.
PCBs are used by the OS for many tasks, including process scheduling, process
synchronization, and process communication.
CPU Switch From Process to Process
Scheduling Queue
As the process enter the system, they are put into a job queue.
The processes that are ready and waiting to execute are kept in ready queue,
generally stored as a linked-list.
The header of the queue contains pointer to the first and last PCBs in the list.
Each PCB contains a pointer to store the address of the next PCB in the ready
queue.
Each device has it’s own queue to handle the request of many processes.
Scheduling Algorithms
A Process Scheduler schedules different processes to be assigned to the CPU based
on particular scheduling algorithms. There are several popular CPU scheduling
algorithms:
First-Come, First-Served (FCFS) scheduling
Shortest-Job-First (SJF) scheduling
Priority scheduling
Shortest RemainingTime First (SRTF) scheduling
Round Robin (RR) scheduling
Highest Response Ratio Next (HRRN) scheduling
Multiple-Level Queues scheduling
Multilevel Feedback Queue Scheduling (MLFQ)
These algorithms are either non-preemptive or preemptive.
Non-preemptive algorithms are designed so that once a process enters the
running state, it cannot be preempted until it completes its allotted time.
Preemptive scheduling is based on priority where a scheduler may preempt a
low priority running process anytime when a high priority process enters into the
ready state.
First- Come, First-Served (FCFS) Scheduling
FCFS considered to be the simplest of all 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 and 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.
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
FCFS serves as a non-preemptive scheduling algorithm where the CPU is managing a
process until it is completed, which implies that other processes will have to wait, which
leads to slowing down of the operating system.
The Convoy Effect is a phenomenon in which the entire Operating System slows down
owing to a few slower processes in the system. Here, resource utilization is low because the
CPU or I/O modules must sit idle.
Example FCFS Scheduling
P1 A 0 6
P2 B 2 2
P3 C 3 1
P4 D 4 9
P5 E 5 8
Average CT = ( 6 + 8 +9 + 18 + 26 ) / 5 = 13.4
Average TAT = ( 6 + 8 + 9 +18 +26 ) / 5 = 13.4
Average WT = ( 0 + 6 + 8 + 9 + 18 ) / 5 = 8.2
Example on FCFS Scheduling
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
=0
n+1 = n
Recent history does not count
=1
n+1 = tn
Only the actual last CPU burst counts
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
Since both and (1 - ) are less than or equal to 1, each successive term has less weight
than its predecessor
Prediction of the Length of the Next CPU Burst
Example of Shortest-Remaining-Time-First
Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeTBurst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
The CPU is allocated to the process with the highest priority (smallest integer highest priority)
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU burst time
Scheduling
A new job enters queue Q0 which is served using RR scheduling
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to queue Q1
At Q1 job is again served using RR scheduling and receives 16
additional milliseconds
If it still does not complete, it is preempted and moved to queue
Q2 using FCFS scheduling
Multiple-Processor Scheduling
A Multi-processor is a system that has more than one processor but shares the same
memory, bus, and input/output devices. In multi-processor scheduling, more than one
processors(CPUs) share the load to handle the execution of processes smoothly.
The scheduling process of a multi-processor is more complex than that of a single processor system
because of the following reasons.
Load balancing is a problem since more than one processors are present.
Processes executing simultaneously may require access to shared data.
Cache affinity should be considered in scheduling. Cache affinity enables the binding and unbinding
of a process to a CPU or a range of CPUs, so that the process will execute only on the designated CPU
or CPUs rather than any CPU.
Approaches to Multiple Processor Scheduling
The CPUs may be of the same kind (homogeneous) or different types (heterogeneous). There are
two approaches to multi-processor scheduling:
Asymmetric Multi-processor scheduling
Symmetric Multi-processor scheduling
Asymmetric Multiprocessing: In asymmetric multi-processor scheduling, there is a master
server, and the rest of them are slave servers. The master server handles all the scheduling
processes and I/O processes, and the slave servers handle the users' processes. If the master server
goes down, the whole system comes to a halt. However, if one of the slave servers goes down, the
rest of the system keeps working.
In symmetric multi-processor scheduling, the processors are self-scheduling. The
scheduler for each processor checks the ready queue and selects a process to execute. Each of
the processors works on the same copy of the operating system and communicates with each
other. If one of the processors goes down, the rest of the system keeps working.
Symmetrical Scheduling with global queues: If the processes to be executed are in a
common queue or a global queue, the scheduler for each processor checks this global-ready queue
and selects a process to execute.
Symmetrical Scheduling with one queue per CPU: If the processors in the system have
their own private ready queues, the scheduler for each processor checks their own private queue
to select a process.
Symmetric multiprocessor
There are three types of conflict that may arise in a symmetric multi-processor system. These are as
follows.
Locking system: The resources in a multi-processor are shared among the processors. To
ensure safe access of these resources, a locking system is required. This is done to serialize the
access of the resources by the processors.
Shared data: Since multiple processors are accessing the same data at any given time, the data
may not be consistent across all of these processors. To avoid this, we must use some kind of
strategy or locking scheme.
Cache Coherence: When the data is stored in multiple local caches and shared by many
clients, it may be rendered invalid if one of the clients changes the memory block. This can be
resolved by maintaining a consistent view of the data.
Master-Slave Multiprocessor
In a master-slave multi-processor, one CPU works as a master while all others work as
slave processors.
This means the master processor handles all the scheduling processes and the I/O processes
while the slave processors handle the user's processes.
The memory and I/O devices are shared among all the processors, and all the processors
are connected to a common bus. It uses asymmetric multiprocessing to schedule processes.
Processor Affinity
A process has an affinity for a processor on which it runs. This is called processor affinity. When a process runs
on a processor, the recent data accessed by the process is populated in the cache memory of this processor.
However, if this process is migrated to another processor for some reason, the content of the cache memory of the
first processor is invalidated, and the second processor's cache memory has to be repopulated.
To avoid the cost of invalidating and repopulating the cache memory, the Migration of processes from one
processor to another is avoided.
There are two types of processor affinity.
Soft Affinity: The system has a rule of trying to keep running a process on the same processor but does not
guarantee [Link] is called soft affinity.
Hard Affinity: The system allows the process to specify the subset of processors on which it may run, i.e., each
process can run only on some of the processors. Systems such as Linux implement soft affinity, but they also
provide system calls such as sched_setaffinity() to support hard affinity.
Load Balancing
In a multi-processor system, all processors may not have the same workload. Some may have a long ready
queue, while others may be sitting idle. To solve this problem, load balancing comes into the picture. Load
Balancing is the phenomenon of distributing workload so that the processors have an even workload in a
symmetric multi-processor system.
In symmetric multiprocessing systems which have a global queue, load balancing is not required. In such a
system, a processor examines the global ready queue and selects a process as soon as it becomes idle.
There are two ways to solve this.
Push Migration: In push migration, a task routinely checks the load on each processor. If the workload
is unevenly distributed, it will push the load from the overloaded processor and assign the load to an
idle or a less busy processor.
Pull Migration: In pull migration, an idle processor will extract the load from an overloaded processor
itself.
GATE QUESTIONS [CPU SCHEDULING]
SRTF=6, NP-SJF=7.5
GATE QUESTIONS
A, C, D
GATE QUESTIONS
12
GATE QUESTIONS
5.25
GATE QUESTIONS
2
Deadlocks
Introduction
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Introduction
In multiprogramming environment, several processes may compete for a finite no. of
resources.
If the resources are not available, the process enters a wait state.
If the waiting process may never get a chance to change the state because the requested
resources are held by other waiting processes, then this situation is called deadlock.
A process must request a resource before using it and must release after use.
The no. of resources may not exceed the total no. of resources available in the system.
Request and release of other resources can be accomplished through wait and signal
operations on semaphores.
System Model
In a computer system a deadlock is where two or more processes are unable to proceed because
each process is waiting for the other to release a resource that it needs to continue execution.
In other words, a deadlock occurs when two or more processes are in a circular wait state, and
none of them can release the resources they hold until they receive the resources they are waiting
for.
Resources − The system has a set of different types of resources that are shared among
processes. These resources can be hardware or software components, such as memory, files,
printers, or network connections. Each resource is identified by a unique name or identifier
R1, R2, . . ., Rm. Each resource type Ri has Wi instances.
Processes − The system has a set of processes that request and release
resources. Processes are units of execution that can be started, suspended, resumed, and
terminated. Each process is identified by a unique process ID P0,P1,….,Pn.
Resource Allocation − Each resource can be in one of two states, allocated or available. A
resource that is allocated to a process cannot be used by any other process until it is released.
Each process utilizes a resource as follows:
Request
Use
Release
Resource Dependency − Some processes may require multiple resources to complete their
tasks. A resource dependency graph can be used to represent the relationships between
processes and resources and to detect potential deadlocks.
Resource preemption is a technique used by the operating system to preempt resources from
one or more processes involved in the deadlock and allocate them to the processes that need them.
Preemption can be done either selectively or globally. In selective preemption, only the
resources that are required to resolve the deadlock are preempted, while in global preemption, all
the resources held by the deadlocked processes are preempted.
When a process is terminated, all the resources held by the process are released, and other
processes can proceed.
Deadlock Characteristics
Necessary Conditions:
Mutual exclusion: Only one process at a time can use a resource. Another process will
wait until the resource is released by the process who is using it.
Hold and wait: A process holding at least one resource is waiting to acquire additional
resources held by other processes.
No preemption: Resources can’t be preempted. A resource can be released only
voluntarily by the process holding it, after that process has completed its task.
Circular wait: There exists 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–1 is
waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.
R = {R1, R2, …, Rm}, the set consisting of all the resource types in the system
(process Pi requested an instance of resource type Rj and waiting for that resource)
assignment edge – directed edge Rj Pi
(an instance of resource type Rj has been allocated to process Pi)
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
Mutual Exclusion – The Mutual Exclusion condition must hold for non-sharable
resources. E.g printers can not be shared. But sharable resources do not require mutually
exclusive access. So, process will never wait for sharable resources (e.g., read-only files);
Hold and Wait – must guarantee that whenever a process requests a resource, it does not
hold any other resources
Protocol-1: It require processes to request all resources before execution.
Protocol-2: Allow process to request resources only when the process has none allocated to it.
The system must keep track of resources currently available, currently allocated and
future request and release of each process to decide whether current request can be
satisfied or not.
Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need.
The deadlock-avoidance algorithm dynamically examines the resource-allocation state
to ensure that there can never be a circular-wait condition.
Resource-allocation state is defined by the number of available and allocated resources, and
the maximum demands of the processes.
Safe State
When a process requests an available resource, system must decide if immediate allocation
leaves the system in a safe state.
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes in
the systems such that for each Pi, the resources that Pi can still request can be satisfied by
currently available resources + resources held by all the Pj, with j < i
That is:
If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished
When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and
terminate
When Pi terminates, Pi +1 can obtain its needed resources, and so on
If no such sequence exists, then the system is in unsafe state.
Basic Facts
If a system is in safe state no deadlocks
If a system is in unsafe state possibility of deadlock
Avoidance ensure that a system will never enter an unsafe state.
Consider a system with 12 magnetic tape drive:
Process Max. Needs Current Allocated Need
P0 10 5 5
P1 4 2 2
P2 9 2 7
Total allocated=5+2+2=9
Currently available=12-9=3
The sequence <P1,P0,P2> satisfies the safety condition.
Let after some time P2 request one more resource and allocated.
Process Max. Needs Current Allocated Need
P0 10 5 5
P1 4 2 2
P2 9 3 6
Currently available=12-10=2
P1 requires 2 tape drive for completion. After completion of P1 available tape drives=4
But P0 and P2 requires 5 and 6 tape drives respectively. So, the system is in unsafe state.
Avoidance Algorithms
Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj
available
Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of
Rj
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete
its task
Need [i,j] = Max[i,j] – Allocation [i,j]
Let X and Y be vectors of length n. We say that, X<=Y if and only if:
X[i]<=Y[i] for all i=1,2,3…n
For example:
X=[1,7,3,2] and Y=[0,3,2,1], we say that Y<=X.
We say,Y<X if Y<=X and Y!=X
For example:
X=[1,2,3,4] and Y=[1,2,3,1], we say that Y<X.
Safety Algorithm
[Link] Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
When a request for resources is made by process Pi, the following actions are taken:
Requesti = request vector for process Pi.
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not
available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi – Requesti
If safe the resources are allocated to Pi
If unsafe Pi must wait, and the old resource-allocation state is restored
Example of Banker’s Algorithm
5 processes P0 through P4; 3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
Example (Cont.)
The content of the Need matrix is defined to be Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
Example: P1 Request (1,0,2)
Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle,
there exists a deadlock
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
State of system?
Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
Detection-Algorithm Usage
When, and how often, to invoke deadlock-detection algorithm depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
If detection algorithm is invoked frequently, then there may be many cycles in the
resource graph and so we would not be able to tell which processes “caused” the deadlock.
Frequent invocation of deadlock-detection algorithm may incur overhead on system
performance.
Less frequent invocation may leads to many cycles in the resource allocation graph and
it will be difficult to tell which processes caused the deadlock.
Recovery from Deadlock
Recovery from Deadlock
There are two alternative to deal with deadlock:
Inform the operator that the deadlock occurred and let the operator deal with it.
Let the system recover from the deadlock automatically.
There are four options for breaking a deadlock:
Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is eliminated
Preempt some resources from one or more deadlocked processes
The operating system can roll back to the previous safe state using check pointing at every
state.
Process Termination
Aborting one or more processes will break the [Link] are two options;
Abort all deadlocked processes: It causes great expense because these processes have
computed for a long time.
Abort one process at a time until the deadlock cycle is eliminated: It causes overhead on system
performance because every time we abort a process a deadlock-detection algorithm must be invoked
to detect any further deadlock in the system.
In which order should we choose to abort processes?
1. Priority of the process
2. How long process has computed, and how much longer to completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
Resource Preemption
Preempt some resources from processes and give these resources to other processes until
the deadlock cycle is broken.
Selecting a victim – Which resources and which processes are to be preempted? The
order of preemption should minimize the cost. Cost can be computed by:
No. of resources the process is holding
Execution time so far a process has consumed
Rollback –When we preempt a resource from a process, this process must roll backed to
some safe state, restart process from that state.
Starvation – If same process may always be picked as victim, then that process may
starved.
GATE QUESTIONS [DEADLOCK]
A, D
GATE QUESTIONS
2
GATE QUESTIONS
B
GATE QUESTIONS
D
GATE QUESTIONS
4
GATE QUESTIONS
B
GATE QUESTIONS
P2