0% found this document useful (0 votes)
25 views20 pages

Deadlocks 2

Thank You
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

Deadlocks 2

Thank You
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Deadlock

• A process requests resources for its execution,


• if the resources are not available at that time,
the process enters a waiting state.
• Sometimes, the resources it has requested are
held by other waiting processes, hence the
current process never changes its (waiting)
state.
• This situation is called a Deadlock.
Deadlock Ignorance – Ostrich Algorithm

• If there is deadlock occurs in the system, then the OS will just ignore the deadlock and
reboot the system in order to function well.

• This method of solving any problem is called Ostrich Algorithm.


Ostrich Algorithm

• Based on the amazing behavior of the bird Ostrich.


• During the storm, the Ostrich stick his's head in the sand, and pretend there is no storm.
• This Ostrich algorithm is the most widely used technique in order to ignore the
deadlock, and also it used for the single end-users systems.
Ostrich Algorithm...

• The ostrich algorithm pretends there is no deadlock in the system.


• It is simply assumed, that the deadlock will never occur
• It is reasonable to use if
• deadlocks occur very rarely
• the cost of their prevention would be high.
• The UNIX and Windows operating systems are utilizing this approach
Methods for handling deadlocks.

• Using the ostrich algorithm is one of the methods of dealing with deadlocks.
• This will not be a good idea to solve the deadlock problem, hence the drawbacks are
more, without finding solutions to deadlock problems.
• The methods for handling deadlocks
• Deadlock detection,
• Deadlock recovery,
• Deadlock avoidance,
• Deadlock prevention.
Deadlock Avoidance

• Avoiding deadlocks is to require additional information about how resources are to be


requested.
• This model requires that each process declare the maximum number of resources of
each type that it may need.
• With this information, it is possible to construct an algorithm that ensures that the
system will never enter a deadlocked state.
• The 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.
• Assume a system consists of a sequence of processes <P1, P2. .., Pn> and some of
resources are allocated to that.
• Then,
• 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
• System is in safe state
Safe State, Unsafe state and Deadlock state Spaces

• A safe state is not a deadlocked state.


• Conversely, a deadlocked state is an unsafe state.
• Not all unsafe states are deadlocks, but an unsafe state may lead to a deadlock
Avoidance Algorithms

• Single instance of a resource type


• Use a resource-allocation graph
• Multiple instances of a resource type
• Use the Banker's Algorithm
Resource-Allocation-Graph Algorithm...

• We check for safety by using a cycle-detection algorithm.


• If a cycle is found, then the allocation will put the system in an unsafe state.
• In that case, process Pi will have to wait for its requests to be satisfied.
• P2 requests R2.
• Although R2 is currently free, we cannot allocate it to P2, since this action will create a
cycle in the graph.

Resource allocation graph for deadlock avoidance An unsafe state in a resource allocation graph
Banker's Algorithm

• If a system has multiple instances of each resource type, then resource-allocation-


graph algorithm is not applicable, hence we use another powerful algorithm to avoid
deadlock.
• This algorithm is commonly known as the banker's algorithm.
Banker's Algorithm

• Available. A vector of length m indicates the number of available resources of each


type.
• Max. defines the maximum demand of each process.
• Allocation. defines the number of resources of each type currently allocated to each
process.
• Need. indicates the remaining resource need of each process.
• Need = Max - Allocation.
Safety Algorithm

• 1. Initialize Work = Available and Finish(i] = false for i = 0, 1, ..., n - 1.


• 2. Find an process i such that both
• a. Finish[i] == false
• b. Needi ≤ Work
• 3. Work = Work + Allocationi
.Finishf{i} = true
• Go to step 2.
• 4. If Finish[i] == true for all i, then the system is in a safe state.
Banker's Algorithm – Example

• Consider a system with five processes P0 through P4 and three resource types A, B,
and C.
• Resource type A has ten instances, resource type B has five instances, and resource
type C has seven instances
• Suppose that, at time T0, the following snapshot of the system has been taken:

Allocation Max Available


ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Banker's Algorithm – Example

The need is defined to be Max – Allocation and is as follows:

Allocation Max Available Need


ABC ABC ABC ABC
P0 010 753 332 P0 743
P1 200 322 P1 122
P2 302 902 P2 600
P3 211 222 P3 011
P4 002 433 P4 431

• A B C
• 10 5 7
• Work = Available and Finish[i] = false
• for i = 0, 1, ..., n - 1.
• P0: 1. work = available
• Work = 3 3 2
• 2. Need <= work
• 7 4 3 <= 3 3 2 (false)
• P1: work = available
• Work = 3 3 2
• 2. Need <= work1
• 2 2 <= 3 3 2 (true)
• 3. work = work + allocation
• = 332 + 200=532
• P2: 1. work = available
• Work = 5 3 2
• 2. Need <= work
• 600 <= 5 3 2 (false)
• P3: work = available
• Work = 5 3 2
• 2. Need <= work
• 011 <= 53 2 (true)
• 3. work = work + allocation
•=532+211=743
• P4: work = available
• Work = 7 4 3
• 2. Need <= work
• 4 3 1 <= 7 4 3 (true)
• 3. work = work + allocation
• =743 + 002=745
• P0: work = available
•Work = 745
• 2. Need <= work
• 743 <= 745 (true)
• 3. work = work + allocation
•=745+010=755
• P2: work = available
• Work=755
• 2. Need <= work
• 600 <= 745 (true)
• 3. work = work + allocation
• = 755+302=10 5 7
Banker's Algorithm – Example

• Result - if the process execution is in following order, then the system will be
safe.

You might also like