+ All Categories
Home > Documents > Designed and Presented by Dr. Ayman Elshenawy Elsefy

Designed and Presented by Dr. Ayman Elshenawy Elsefy

Date post: 02-Jan-2022
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
30
Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : [email protected] Reference Operating System Concepts, ABRAHAM SILBERSCHATZ Operating Systems
Transcript
Page 1: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Designed and Presented by

Dr. Ayman Elshenawy Elsefy

Dept. of Systems & Computer Eng..

AL-AZHAR University

Website : eaymanelshenawy.wordpress.com

Email : [email protected]

Reference

Operating System Concepts, ABRAHAM SILBERSCHATZ

Operating Systems

Page 2: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 7: Deadlocks

• The Deadlock Problem

• System Model

• Deadlock Characterization

• Methods for Handling Deadlocks

• Deadlock Prevention

• Deadlock Avoidance

• Deadlock Detection

• Recovery from Deadlock

Page 3: Designed and Presented by Dr. Ayman Elshenawy Elsefy

The Deadlock Problem

• Deadlock:• A process requests resources;

• If the resources are not available at that time, the process enters awaiting state.

• Sometimes, a waiting process can not change its state, because theresources it has requested are held by other waiting processes.

• Example of deadlocks:

1. A system with CD1 and CD2 drives allocated to P1 and P2.

• P1 requested CD2 and P2 requested CD1.

• Each is waiting for the event “CD is released,” which can be caused onlyby one of the other waiting processes(same resource type deadlock).

2. A system with one printer and one DVD.

• Suppose that process Pi is holding the DVD and process Pj is holding the

printer.

• If Pi requests the printer and Pj requests the DVD drive, a deadlock occurs.

Page 4: Designed and Presented by Dr. Ayman Elshenawy Elsefy

System Model• A system have a finite number of resources R1,R2,R3,…Rm.

• These resource was divided into several resource types/classes (CPU cycles,

files, and I/O devices) consisting of some number of identical instances

(CPU1 & CPU2).

• These resources can requested by a number of processes P1,P2,…., Pn.

• If a process requests an instance of a resource type, any instance of the type

should satisfy the request. Else they are not identical.

• A process may utilize a resource in only the following sequence:

• Request: The process requests the resource. If it cannot be granted

immediately, the process must wait until it can acquire the resource.

• Use: The process can operate on the resource.

• Release: The process releases the resource.

• The request and release of resources may be system calls request()

,release() device - open() ,close() file, allocate() free() memory system calls.

Page 5: Designed and Presented by Dr. Ayman Elshenawy Elsefy

System Model

• Resource allocation table:

• Is a system table that records whether each resource is free or

allocated by which process.

• If a process requests a resource type that is currently allocated to

another process, it can be added to a queue of processes waiting for

this resource.

• Each process of a set of processes is in a deadlocked state is waiting

for an event that can be caused only by another process in the set.

• This event is resource acquisition and release.

Page 6: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Bridge Crossing Example

• Traffic only in one direction

• Each section of a bridge can be viewed as a resource

• If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback)

• Several cars may have to be backed up if a deadlock occurs

• Starvation is possible

• Note – Most OS do not prevent or deal with deadlocks

Page 7: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock Characterization

Deadlock can arise if four conditions hold simultaneously.• Mutual exclusion:

• Only one process at a time can use a resource, If another processrequests that resource, the requesting process must be delayed untilthe resource has been released.

• Hold and wait:• A process holding at least one resource is waiting to acquire

additional resources held by other processes

• No preemption:• A resource can be released only optionally by the process holding it

completing 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.

Page 8: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource Allocation Graph• A directed graph used to describe deadlocks, and consisted of :

• A set of vertices V (Active processes in the system P1, P2, ..., Pn).

• All resource types in the system Resource R = (R1, R2, ..., Rm).

• A set of edges E:

• Request Edge (Pi → RJ): Process Pi has requested an instance of resource typeRj and is currently waiting for that resource. points to only the rectangle Rj

• assignment edge (Rj → Pi ): An instance of resource type Rj hasbeen allocated to process Pi . One dot in the rectangular.

Page 9: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource Allocation Graph

• Work Sequence:• Each process Pi as a circle

• Each resource type Rj as a rectangle (each instance as a dot withinthe rectangle).

• When process Pi requests an instance of resource type Rj, A requestedge is inserted in the resource-allocation graph.

• The request edge is instantaneously transformed to an assignmentedge When this request can be fulfilled .

• When the process no longer needs the resource, it releases theresource.

Page 10: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Example of a Resource Allocation Graph

Page 11: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource Allocation Graph With A DeadlockGiven the resource-allocation graph:

• If the graph contains no cycles, no deadlock.

• If the graph contains a cycle, then a deadlock may exist.

• If each resource type has several instances, then a cycle does not

necessarily imply that a deadlock has occurred.

• Each process involved in the cycle is deadlocked.

Graph with a cycle but no deadlockGraph with deadlock

Page 12: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource Allocation Graph

Page 13: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Methods for Handling Deadlocks• Ensure that the system will never enter a deadlock state, the system can use:

A. Deadlock prevention:

• A set of methods to ensure that at least one of the necessary conditionscannot hold.

B. Deadlock avoidance:

• Requires that the OS be given additional information in advanceconcerning which resources a process will request and use during itslifetime. the OS can decide for each request whether or not the processshould wait.

C. Deadlock Detection and recovery:

• Allow the system to enter a deadlock state and then recover, by provide analgorithm that examines the state of the system to determine whether adeadlock has occurred and an algorithm to recover from the deadlock.

D. Ignore the problem and suppose that deadlocks never occur inthe system;

• used by most OS, including UNIX and windows, the application developerto write programs that handle deadlocks

Page 14: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock Prevention• By ensuring that at least one of these conditions cannot hold, we can prevent the

occurrence of a deadlock.

• Mutual Exclusion• Not required for sharable resources.

• must hold for non-sharable resources

• Read-only files are a good example of a sharable resource. If several processesattempt to open a read-only file at the same time, they can be grantedsimultaneous access to the file.

• Hold and Wait• Whenever a process requests a resource, it does not hold any other resources.

• Protocol 1: each process to request and be allocated all its resources before itbegins execution (system calls requesting resources for a process precede allother system calls.

• Protocol 2: allows a process to request resources only when it has none. Aprocess may request some resources and use them. Before it can request anyadditional resources, it must release all the resources that it use.

• Example: A process copies data from a DVD drive to a file on disk, sorts thefile, and then prints the results to a printer.

• Low resource utilization and starvation possible

Page 15: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock Prevention

• No Preemption –• If a process that is holding some resources requests another resource that

cannot be immediately allocated to it, then all resources currently beingheld are released

• Preempted resources are added to the list of resources for which theprocess is waiting

• Process will be restarted only when it can regain its old resources, as wellas the new ones that it is requesting

• Circular waiting• impose a total ordering of all resource types and to require that each

process requests resources in an increasing order of enumeration.

• let R = {R1, R2, ..., Rm} be the set of resource types.

• We assign to each resource type a unique integer number, which allows usto compare two resources and to determine whether one precedes anotherin our ordering.

• For example, if the set of resource types R includes tape drives, diskdrives, and printers, then the function F might be defined as follows:

F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12

Page 16: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock Avoidance

Requires that the system has some additional a priori information available

• Each process declare the maximum number of resources of each type thatit 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 resources (Available ).

• The number of allocated resources ( Max ).

• The maximum demands of the processes (Needs )

Page 17: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Safe State• When a process requests an available resource, system mustdecide if this allocation leaves the system in a safe state

• System is in safe state That is:• If P0 requested resource are not immediately available.

• P0 can wait until all P1 have finished, When P1 is finished, P0 can obtain requestedresources, execute, return allocated resources, and terminate

• When Pi terminates, Pi +1 can obtain its needed resources, and so on

• 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.

• Single instance of a resource type• Use a resource-allocation graph

• Multiple instances of a resource type• Use the banker’s algorithm

Page 18: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource-Allocation Graph Scheme• Used If we have a resource-allocation with only one instance/ resource type.

• A new type of edge called Claim edge Pi Rj indicated that process Pj mayrequest resource Rj ( represented by a dashed line ).

• Claim edge converts to request edge when a process requests a resource

• Request edge converted to an assignment edge when the resource is allocated tothe process

• When a resource is released by a process, assignment edge reconverts to a claimedge

• Resources must be claimed a priori in the system before starting execution of theprocess all its claim edges must appear on the resource allocation graph.

Page 19: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Banker’s Algorithm• Multiple instances per each resource.

• Each process must a priori claim maximum use

• When a process requests a resource it may have to wait

• When a process gets all its resources it must return them in a finite amount oftime.

Data Structures for the Banker’s Algorithm • Let n = number of processes, and m = number of resources types.

• Available[i]: Vector of length m. • If available [j] = k, there are k instances of resource type Rj available

• Max[I,j]: n x m matrix. • If Max [i,j] = k, then process Pi may request at most k instances of type Rj.

• Allocation[I,j]: n x m matrix. • If Allocation[i,j] = k then Pi is currently allocated k instances of Rj

• Need[I,j] 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]

Page 20: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Safety Algorithm

Page 21: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource-Request Algorithm for Process Pi

Page 22: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Example of Bankers Algorithm

• Consider a system with five processes P0 through P4 and three resource types A (10

instances), B (5 instances), and C (7 instances).

• Suppose that, at time T0, the following snapshot of the system:

IF P1 requests additional (A(1),B(0),C(2)). To decide whether this request can be

immediately granted, we first check that Request t1 ≤ Available— (1,0,2) ≤ (3,3,2).

Available = System resources – Allocated by all process

Max is max required for execute process – Need = Max(P) – Allocated (P)

Page 23: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock Detection

•If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, thena deadlock situation may occur.

•In this environment, the system may provide:•Two algorithms are used

• An algorithm must examines the state of the system todetermine whether a deadlock has occurred

• An algorithm to recover from the deadlock

Page 24: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Single Instance of Each Resource Type • Make a small modification on the Resource Allocation Graph ( Wait-For Graph)

• Maintain wait-for graph

• Waiting for process Pj An edge from Pi to Pj in a wait-for graph implies that process Pi is to release a resource that Pi needs.

• An edge Pi → Pj exists in a wait-for graph if and only if the corresponding resource allocation graph contains two edges Pi → Rq and Rq → Pj for some resource

• Nodes are processes

• Pi Pj if Pi is waiting for Pj

• Periodically invoke an algorithm that searches for a cycle in the graph.

• If there is a cycle, there exists a deadlock

• An algorithm to detect a cycle in a graph is O(n2 ), where n is the number of vertices in the graph.

Resource-Allocation Graph Corresponding wait-for graph

Page 25: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Several Instances of a Resource Type• 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, process Pi is requesting k more instances of resource type. Rj.

Page 26: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Several Instances of a Resource Type

Page 27: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Several Instances of a Resource Type

Page 28: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Example of Detection Algorithm• Five processes P0 through P4; three resource types A (7 instances), B (2

instances), and C (6 instances)

• The snapshot of the system at time T0 is:

We claim that the system is not in a

deadlocked state. Indeed, if we

execute

our algorithm, we will find that the

sequence <P0, P2, P3, P1, P4> results

in

Finish[i] == true for all i.

P2 request(0,0,1)

Deadlock have occurred

because the requested is less

than the available

Page 29: Designed and Presented by Dr. Ayman Elshenawy Elsefy

Recovery from Deadlock

2. Process Termination• Abort all deadlocked processes

• Abort one process at a time until the deadlock cycle is eliminated

• In which order should we choose to abort?• Priority of the process

• How long process has computed, and how much longer to completion

• Resources the process has used and Resources process needs to complete

• How many processes will need to be terminated

• Is process interactive or batch?

3. Resource Preemption• Selecting a victim – minimize cost

• Rollback – return to some safe state, restart process for that state

• Starvation – same process may always be picked as victim, include number of rollback in cost factor

Page 30: Designed and Presented by Dr. Ayman Elshenawy Elsefy

End of Chapter 7


Recommended