Chapter 6 Concurrency: Deadlock and Starvation II zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock Detection zStrategies once Deadlock Detected zDeadlock Detection Algorithm zUNIX Concurrency Mechanisms
Deadlock Detection zOperating system checks for deadlock zCheck at resource request yearly detection of deadlock yfrequent checks consume processor time zCheck periodically
Strategies once Deadlock Detected zAbort all deadlocked processes zBack up each deadlocked process to some previously defined checkpoint, and restart all process yoriginal deadlock may occur zSuccessively abort deadlocked processes until deadlock no longer exists zSuccessively preempt resources until deadlock no longer exists
Deadlock Detection Algorithm (1) 1. Mark each process that has a row in the Allocation matrix of all zeros. 2.Initialize a temporary vector W to equal to Available vector. 3. Find an index i such that process i is currently unmarked and the ith row of Q is less than or equal to W. That is, Q ik <= W k, for 1 <= k <= m. If no such row is found, terminate the algorithm. ( Q ik represents the amount of resources of type j requested by process i. m: the amount of resources.) 4. If such a row is found, mark process i and add the corresponding row of the allocation matrix to W. That is, set W k = W k + A ik. Return to step 3. A deadlock exists if and only if there are unmarked processes at the end of the algorithm.
Example for Deadlock Detection (2) - Is there any deadlock? R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P P P P Claim Matrix Allocation Matrix Request Matrix C ik A ik Q ik R k = Resource Vector V k = Available Vector
Example for Deadlock Detection (3) - Apply the deadlock detection algorithm Step 1 Mark P4 & P5, because P4 & P5 have no allocated resources. R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P P *P *P Claim Matrix Allocation Matrix Request Matrix C ik A ik Q ik R k = Resource Vector V k = Available Vector
Example for Deadlock Detection (4) - Apply the deadlock detection algorithm Step 2 and 3 set W = W k = V k = ( ) R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P P *P *P Claim Matrix Allocation Matrix Request Matrix C ik A ik Q ik R k = Resource Vector W =W k = V k = Available Vector
Example for Deadlock Detection (5) - Apply the deadlock detection algorithm Step 4 The request of process P3 is less than or equal to w, so mark P3 and set W = W + A = ( ) + ( ) = ( ) R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P *P *P *P Claim Matrix Allocation Matrix Request Matrix C ik A ik Q ik R k = Resource Vector V k = Available Vector
Example for Deadlock Detection (6) - Apply the deadlock detection algorithm Step 3 Can not find a row of Q is less than or equal to W. Terminate. P1 & P2 unmarked, indicating these processes are deadlocked. R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P *P *P *P Claim Matrix Allocation Matrix Request Matrix C ik A ik Q ik R k = Resource Vector V k = Available Vector
Question 3 - Exercise/Home Work (1) - Is there any deadlock? R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P P P P Claim Matrix Allocation Matrix Request Matrix C ik A ik Q ik R k = Resource Vector V k = Available Vector
UNIX Concurrency Mechanisms zSignals ysoftware mechanism that informs a process of the occurrence of asynchronous events
UNIX Concurrency Mechanisms zPipes ycircular buffer allowing two processes to communicate yqueue written by one process and read by another yoperating system enforces mutual exclusion for writing and reading the pipe ywrite requests are immediately executed if there is room in the pipe, otherwise the process is blocked yread request is blocked if attempts to read more bytes than currently in the pipe
UNIX Concurrency Mechanisms zMessage Queue yblock of text with accompanying type yreceiver can either retrieve messages in FIFO order or by type yprocess suspends when trying to send a message to a full queue yprocess suspends when reading from an empty queue yprocess trying to read a certain type fails, it’s not suspended
UNIX Concurrency Mechanisms zSemaphores ywait and signal yoperating system handles all these requests
UNIX Concurrency Mechanisms zShared memory ycommon block of virtual memory shared by multiple processes yfast form of interprocess communication ymutual exclusion must be provided by the processes, not the operating system
Question 4 - Exercise/Home Work (1) Consider a system with a total of 150 units of memory, allocated to three processes as shown: Process Max Hold Apply the Deadlock Detection and Banker’s algorithm to determine whether it would be safe to grant each of the following requests. If yes, indicate a sequence of termination that could be guaranteed possible. If no, show the reduction of the resulting allocation table. a. A fourth process arrives, with a maximum memory need of 60 and an initial need of 25 units. b. A fourth process arrives, with a maximum memory need of 60 and an initial need of 35 units.