Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Concurrency: Deadlock and Starvation Chapter 6.

Similar presentations


Presentation on theme: "1 Concurrency: Deadlock and Starvation Chapter 6."— Presentation transcript:

1 1 Concurrency: Deadlock and Starvation Chapter 6

2 2 Outline n Principles of Deadlock u Introduction and conditions for deadlock u Deadlock prevention u Deadlock Avoidance u Deadlock detection u An Integrated deadlock strategy n Concurrency Mechanisms in UNIX, Linux, Solaris and Windows

3 3 Deadlock n Permanent blocking of a set of processes that either compete for system resources or communicate with each other n Involves conflicting needs for resources by two or more processes n There is no satisfactory solution in the general case n Some OS (ex: Unix SVR4) ignore the problem and pretend that deadlocks never occur...

4 4 Potential Deadlock I need quad A and B I need quad B and C I need quad C and B I need quad D and A

5 5 Actual Deadlock HALT until B is free HALT until C is free HALT until D is free HALT until A is free

6 6 Two Processes P and Q n Let’s look at this with two processes P and Q. n Each needing exclusive access to a resource A and B for a period of time. n Deadlock could happen. u Whether or not deadlock occurs depends on both the dynamics of the execution and on the details of the application.

7 7 Example where deadlock can occur

8 8 Alternative logic n Suppose that P does not need both resources at the same time so that the two processes have this form

9 9 Example where deadlock cannot occur

10 10 The Conditions for Deadlock n These 3 conditions of policy must be present for a deadlock to be possible: 1: Mutual exclusion F only one process may use a resource at a time 2: Hold-and-wait F a process may hold allocated resources while awaiting assignment of others 3: No preemption F no resource can be forcibly removed from a process holding it

11 11 The Conditions for Deadlock n We also need the occurrence of a particular sequence of events that result in: 4: Circular wait F a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

12 12 The Conditions for Deadlock n Deadlock occurs if and only if the circular wait condition is unresolvable n The circular wait condition is unresolvable when the first 3 policy conditions hold n Thus the 4 conditions taken together constitute necessary and sufficient conditions for deadlock

13 13 Resource Categories Two general categories of resources: n Reusable u can be safely used by only one process at a time and is not depleted by that use. u Example? n Consumable u one that can be created (produced) and destroyed (consumed). u Example?

14 14 Reusable Resources n Such as: u Processors, I/O channels, main and secondary memory, devices, and data structures such as files, databases, and semaphores n Deadlock may occur if each process holds one resource and requests the other

15 15 Example of Reuse Deadlock n Consider two processes that compete for exclusive access to a disk file D and a tape drive T. n Deadlock occurs if each process holds one resource and requests the other.

16 16 Reusable Resources Example 1: Device Request

17 17 Reusable Resources Example 2: Memory Request n Space is available for allocation of 200Kbytes, and the following sequence of events occur n Deadlock occurs if both processes progress to their second request (assuming no- preemption) P1... Request 80 Kbytes; Request 60 Kbytes; P2... Request 70 Kbytes; Request 80 Kbytes;

18 18 Consumable Resources n Such as Interrupts, signals, messages, and information in I/O buffers n Deadlock may occur if a Receive message is blocking n May take a rare combination of events to cause deadlock

19 19 Example of Deadlock n Consider a pair of processes, in which each process attempts to receive a message from the other process and then send a message to the other process

20 20 Resource Allocation Graphs n Directed graph that depicts a state of the system of resources and processes

21 21 Methods for handling deadlocks n Deadlock prevention u disallow 1 of the 4 necessary conditions of deadlock occurrence n Deadlock avoidance u do not grant a resource request if this allocation might lead to deadlock n Deadlock detection u always grant resource request when possible. But periodically check for the presence of deadlock and then recover from it

22 22 Deadlock Prevention n The OS is design in such a way as to exclude a priori the possibility of deadlock n Two main methods: u Indirect - disallow 1of the 3 policy conditions or prevent all three of the necessary conditions occurring at once u Direct - prevent the occurrence of circular wait

23 23 Indirect methods of deadlock prevention n Mutual Exclusion u cannot be disallowed u ex: only 1 process at a time can write to a file n Hold-and-Wait u can be disallowed by requiring that a process request all its required resources at one time u block the process until all requests can be granted simultaneously n Issues of Hold-&-Wait: u process may be held up for a long time waiting for all its requests u resources allocated to a process may remain unused for a long time. These resources could be used by other processes u an application would need to be aware of all the resources that will be needed

24 24 Indirect methods of deadlock prevention n No preemption u Process must release resource and request again. u But whenever a process must release a resource who’s usage is in progress, the state of this resource must be saved for later resumption. u Hence: practical only when the state of a resource can be easily saved and restored later, such as the processor.

25 25 Direct methods of deadlock prevention n A protocol to prevent circular wait: u define a strictly increasing linear ordering O() for resource types. Ex: F R1: tape drives: O(R1) = 2 F R2: disk drives: O(R2) = 4 F R3: printers: O(R3) = 7 u A process initially request a number of instances of a resource type, say Ri. A single request must be issued to obtain several instances. u After that, the process can request instances for resource type Rj if and only if O(Rj) > O(Ri)

26 26 Prevention of circular wait n Circular wait cannot hold under this protocol. Proof: u Processes {P0, P1..Pn} are involved in circular wait iff Pi is waiting for Ri which is held by Pi+1 and Pn is waiting for Rn held which is held by P0 (circular waiting)

27 27 Prevention of circular wait u under this protocol, this means: F O(R0) < O(R1) <.. < O(Rn) < O(R0) impossible! n This protocol prevents deadlock but will often deny resources unnecessarily (inefficient) because of the ordering imposed on the requests

28 28 Deadlock Prevention: Summary n We disallow 1of the 3 policy conditions or use a protocol that prevents circular wait n This leads to inefficient use of resources and inefficient execution of processes

29 29 Deadlock Avoidance n We allow the 3 policy conditions but dynamically make a decision whether the current resource allocation request will, if granted, potentially lead to a deadlock. If so, two approaches: u Process initial denial: do not start a process if it’s demand might lead to deadlock u Resource allocation denial: do not grant an incremental resource request if this allocation might lead to deadlock n Assumptions: u Maximum requirements of each resource must be stated in advance u Requires knowledge of future process requests n Allows more concurrency than prevention

30 30 Resource types n Resources in a system are partitioned in resources types n Each resource type in a system exists with a certain amount. Let R(i) be the total amount of resource type i present in the system. Ex: u R(main memory) = 128 MB u R(disk drives) = 8 u R(printers) = 5 n The partition is system specific (ex: printers may be further partitioned...)

31 31 Process initiation denial n A process is only started if the maximum claim of all current processes plus those of the new process can be met. n Let C(k,i) be the amount of resource type i claimed by process k. n To be admitted in the system, process k must show C(k,i) for all resource types i n C(k,i) is the maximum value of resource type i permitted for process k. n Let U(i) be the total amount of resource type i unclaimed (available) in the system:  U(i) = R(i) -  _ k C(k,i)

32 32 Process initiation denial n A new process n is admitted in the system only if C(n,i) <= U(i) for all resource type I n This policy ensures that deadlock is always avoided since a process is admitted only if all its requests can always be satisfied (no matter what will be their order) n A sub optimal strategy since it assumes the worst: that all processes will make their maximum claims together at the same time

33 33 Resource allocation denial: the banker’s algorithm n Processes are like customers wanting to borrow money (resources) from a bank... n A banker should not allocate cash when it cannot satisfy the needs of all its customers n Consider a system with a fixed number of resources u State of the system is the current allocation of resources to process: F The values of R(i), C(j,i) for all resource type i and process j and the values of other vectors and matrices. u Safe state is where there is at least one sequence that does not result in deadlock u Unsafe state is a state that is not safe

34 34 Determination of Safe State n A system consisting of four processes and three resources. n Allocations are made to processors n Is this a safe state? u Can any of the four processes be run to completion with the resources available? Amount of Existing Resources Resources available after allocation

35 35 After P2 runs to completion Can any of the remaining processes be completed? Note P2 is completed

36 36 After P1 completes

37 37 P3 Completes Finally, we can complete P4. At this point, all of the processes have been run to completion. Thus, the state defined originally is a safe state.

38 38 Determination of an Unsafe State This time Suppose that P1 makes the request for one additional unit each of R1 and R3. Is this safe?

39 39 Deadlock Avoidance n When a process makes a request for a set of resources, u assume that the request is granted, u Update the system state accordingly, n Then determine if the result is a safe state. u If so, grant the request and, u if not, block the process until it is safe to grant the request.

40 40 The banker’s algorithm n We also need the amount allocated A(j,i) of resource type i to process j for all (j,i) The total amount available of resource i is given by: V(i) = R(i) -  _k A(k,i) n We also use the need N(j,i) of resource type i required by process j to complete its task: N(j,i) = C(j,i) - A(j,i) n To decide if a resource request made by a process should be granted, the banker’s algorithm test if granting the request will lead to a safe state: u If the resulting state is safe then grant request u Else do not grant the request

41 41 The banker’s algorithm n A state is safe iff there exists a sequence {P1..Pn} where each Pi is allocated all of its needed resources to be run to completion u i.e.: we can always run all the processes to completion from a safe state n The safety algorithm is the part that determines if a state is safe n Initialization: u all processes are said to be “unfinished” u set the work vector to the amount resources available: W(i) = V(i) for all i;

42 42 The banker’s algorithm n REPEAT: Find a unfinished process j such that N(j,i) <= W(i) for all i. u If no such j exists, goto EXIT u Else: “finish” this process and recover its resources: W(i) = W(i) + A(j,i) for all i. Then goto REPEAT n EXIT: If all processes have “finished” then this state is safe. Else it is unsafe.

43 43 The banker’s algorithm n Let Q(j,i) be the amount of resource type i requested by process j. n To determine if this request should be granted we use the banker’s algorithm: u If Q(j,i) <= N(j,i) for all i then continue. Else raise error condition (claim exceeded). u If Q(j,i) <= V(i) for all i then continue. Else wait (resource not yet available) u Pretend that the request is granted and determine the new resource-allocation state:

44 44 The banker’s algorithm F V(i) = V(i) - Q(j,i) for all i F A(j,i) = A(j,i) + Q(j,i) for all i F N(j,i) = N(j,i) - Q(j,i) for all i u If the resulting state is safe then allocate resource to process j. Else process j must wait for request Q(j,i) and restore old state.

45 45 Banker’s algorithm: comments n A safe state cannot be deadlocked. But an unsafe state is not necessarily deadlocked. u Ex: P1 from the previous (unsafe) state could release temporarily a unit of R1 and R3 (returning to a safe state) u some processes may need to wait unnecessarily u sub optimal use of resources

46 46 Deadlock Avoidance Restrictions n Maximum resource requirement must be stated in advance n Processes under consideration must be independent and with no synchronization requirements u Example of using shared memory (SM) among multiple processes: SM is counted multiple times n There must be a fixed number of resources to allocate n No process may exit while holding resources

47 47 Deadlock Detection n Resource access are granted to processes whenever possible. The OS needs: u an algorithm to check if deadlock is present u an algorithm to recover from deadlock n The deadlock check can be performed at every resource request n Such frequent checks consume CPU time

48 48 A deadlock detection algorithm n Makes use of previous resource-allocation matrices and vectors n Marks each process not deadlocked. Initially all processes are unmarked. Then perform: u Mark each process j for which: A(j,i) = 0 for all resource type i. (since these are not deadlocked) u Initialize work vector: W(i) = V(i) for all i u REPEAT: Find a unmarked process j such that Q(j,i) <= W(i) for all i. Stop if such j does not exists. u If such j exists: mark process j and set W(i) = W(i) + A(j,i) for all i. Goto REPEAT u At the end: each unmarked process is deadlocked

49 49 Deadlock detection: comments n Process j is not deadlocked when Q(j,i) <= W(i) for all i. n Then we are optimistic and assume that process j will require no more resources to complete its task. n It will thus soon return all of its allocated resources. Thus: W(i) = W(i) + A(j,i) for all i. n If this assumption is incorrect, a deadlock may occur later. n This deadlock will be detected the next time the deadlock detection algorithm is invoked.

50 50 Deadlock detection: example n What is R? n Mark P4 since it has no allocated resources n Set W = (0,0,0,0,1) n P3’s request <= W. So mark P3 and set W = W + (0,0,0,1,0) = (0,0,0,1,1) n Algorithm terminates. P1 and P2 are deadlocked. Why? R1 R2 R3 R4 R5 P1 P2 P3 P4 Request Allocated Available R1 R2 R3 R4 R5 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1

51 51 Deadlock Recovery n Needed when deadlock is detected. The following approaches are possible: u Abort all deadlocked processes (one of the most common solutions adopted in OS!!) u Rollback each deadlocked process to some previously defined checkpoint and restart them (original deadlock may reoccur) u Successively abort deadlocked processes until deadlock no longer exists (each time we need to invoke the deadlock detection algorithm)

52 52 Deadlock Recovery (cont.) u Successively preempt some resources from processes and give them to other processes until deadlock no longer exists F a process that has a resource preempted must be rolled back prior to its acquisition n For the 2 last approaches: a victim process needs to be selected according to: u least amount of CPU time consumed so far u least total resources allocated so far u least amount of “work” produced so far u Lowest priority u…u…

53 53 An integrated deadlock strategy n We can combine the previous approaches into the following way: u Group resources into a number of different classes and order them. Ex: F Swappable space (secondary memory) F Process resources (I/O devices, files...) F Main memory... u Use prevention of circular wait to prevent deadlock between resource classes u Use the most appropriate approach for each class for deadlocks within each class

54 54 Summary - Deadlock Detection, Prevention, and Avoidance

55 55 Roadmap n Principals of Deadlock u Deadlock prevention u Deadlock Avoidance u Deadlock detection u An Integrated deadlock strategy n Concurrency Mechanisms in UNIX and Linux

56 56 UNIX / Linux Concurrency Mechanisms n UNIX/ Linux provides a variety of mechanisms for interprocess communications and synchronization including: u Pipes (FIFOs) u Messages u Shared memory u Semaphores u Signals

57 57 Pipes n A circular buffer allowing two processes to communicate on the producer-consumer model u first-in-first-out queue, written by one process and read by another. n Two types: u Named: u Unnamed

58 58 Messages n A block of bytes with an accompanying type. n UNIX provides msgsnd and msgrcv system calls for processes to engage in message passing. n Associated with each process is a message queue, which functions like a mailbox.

59 59 Shared Memory n A common block of virtual memory shared by multiple processes. n Permission is read-only or read-write for a process, u determined on a per-process basis. n Mutual exclusion constraints are not part of the shared-memory facility but must be provided by the processes using the shared memory.

60 60 Semaphores n SVR4 uses a generalization of the semWait and semSignal primitives defined in Chapter 5; n Associated with the semaphore are queues of processes blocked on that semaphore.

61 61 Signals n A software mechanism that informs a process of the occurrence of asynchronous events. u Similar to a hardware interrupt, without priorities n A signal is delivered by updating a field in the process table for the process to which the signal is being sent.

62 62 Linux Kernel Concurrency Mechanism n Includes all the mechanisms found in UNIX plus u Atomic operations u Spinlocks u Semaphores (slightly different from SVR4) u Barriers

63 63 Atomic Operations n Atomic operations execute without interruption and without interference n Two types: u Integer operations – operating on an integer variable u Bitmap operations – operating on one bit in a bitmap

64 64 Linux Atomic Operations

65 65 Linux Atomic Operations

66 66 Spinlock n Only one thread at a time can acquire a spinlock. u Any other thread will keep trying (spinning) until it can acquire the lock. n A spinlock is an integer u If 0, the thread sets the value to 1 and enters its critical section. u If the value is nonzero, the thread continually checks the value until it is zero. n Disadvantage? n Why using spinlocks?

67 67 Semaphores n User level:  Linux provides a semaphore interface corresponding to that in UNIX SVR4  Internally:  implemented as functions within the kernel and are more efficient than user-visable semaphores  Three types of kernel semaphores: u binary semaphores u counting semaphores u reader-writer semaphores

68 68 Linux Semaphores


Download ppt "1 Concurrency: Deadlock and Starvation Chapter 6."

Similar presentations


Ads by Google