Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced Operating Systems - Spring 2009 Lecture 9 – February 9, 2009 Dan C. Marinescu Office: HEC 439 B. Office.

Similar presentations

Presentation on theme: "1 Advanced Operating Systems - Spring 2009 Lecture 9 – February 9, 2009 Dan C. Marinescu Office: HEC 439 B. Office."— Presentation transcript:

1 1 Advanced Operating Systems - Spring 2009 Lecture 9 – February 9, 2009 Dan C. Marinescu Email: Office: HEC 439 B. Office hours: M, Wd 3 – 4:30 PM. TA: Chen Yu Email: Office: HEC 354. Office hours: M, Wd 1.00 – 3:00 PM.

2 2 Last, Current, Next Lecture Last time: Atomic transactions Today Three-way handshake Deadlocks Next time: Deadlocks

3 3 Three-way handshake TCP uses a three-way handshake between a client and a server process: Passive open: The server must first bind to a port to open it up for connections. A client may initiate an active open to this port. The three-way handshake: 1. The client sends a SYN to the server for an active open. 2. The server replies with a SYN-ACK. 3. The client sends an ACK (usually called SYN-ACK-ACK) back to the server.

4 4 Tri-way handshake example The client sends a Synchronization packet to initiate a connection. A SYN packet has a Sequence Number (SN). (SN is 32-bit field in TCP segment header), e.g., SN=x. The server receives the packet, records the SN and replies with an Acknowledgment and Synchronization (SYN-ACK). The Acknowledgment Number (AN) is a 32-bit field in TCP segment header. It contains the next sequence number that this host is expecting to receive (x + 1). The server also initiates a return session. This includes a TCP segment with its own initial SN, e.g., SN=y. The client responds with the next SN= (x+1) and an AN=y + 1 (the SN of the server + 1).

5 5

6 6 Deadlocks System Model Deadlock Characterization Safe State Resource Allocation Graph Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock

7 7 Deadlocks Happen quite often in real life and the proposed solutions are not always logical: “ When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.” a pearl from Kansas legislation. Deadlock jury. Deadlock legislative body.

8 8

9 9 Deadlocks in computer systems Deadlocks  prevent sets of concurrent processes from completing their tasks. How does a deadlock occur  a set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example semaphores A and B, initialized to 1 P 0 P 1 wait (A);wait(B) wait (B);wait(A) Aim  prevent or avoid deadlocks

10 10 Example of a deadlock Traffic only in one direction. Solution  one car backs up (preempt resources and rollback). Several cars may have to be backed up. Starvation is possible.

11 11 System model Resource types R 1, R 2,..., R m (CPU cycles, memory space, I/O devices) Each resource type R i has W i instances. Resource access model: request use release

12 12 Simultaneous conditions for deadlock Mutual exclusion: only one process at a time can use a resource. 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 voluntarily by the process holding it (presumably after that process has finished). Circular wait: there exists a set {P 0, P 1, …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P 0 is waiting for a resource that is held by P 0.

13 13 Safe state Safe state  there exists a sequence of ALL the processes is the systems such that for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i (ordering of processes). This implies: If P i resource needs are not immediately available  then P i can wait until all P j have finished. When P j finishes  P i can obtain needed resources, execute, return allocated resources, and terminate. When P i terminates  P i +1 can obtain its needed resources, and so on. When a process requests an available resource, the system must decide if immediate allocation leaves the system in a safe state.

14 14 Safe state and deadlocks Safe state  no deadlocks. Unsafe state  possibility of deadlock.

15 15 Resource allocation graph (V,E) Directed bipartite graph: two types of nodes in V: P = {P 1, P 2, …, P n }  processes. R = {R 1, R 2, …, R m }  resource types. request edge – directed edge P 1  R j assignment edge – directed edge R j  P i

16 16 Resource allocation graph (cont’d) Process Resource Type with 4 instances P i requests an instance of R j P i is holds an instance of R j PiPi PiPi RjRj RjRj

17 17 Cycles in a resource allocation graph No cycles  no deadlock. A cycle  only one instance per resource type  deadlock. multiple instances of each resource type  possibility of deadlock.

18 18 Resource allocation graph with a deadlock

19 19 Graph with a cycle but no deadlock

20 20 Alternatives Deadlock prevention and avoidance  ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state  then recover. Ignore the problem  used by most operating systems, including UNIX.

21 21 Deadlock prevention vs. deadlock avoidance Deadlock Prevention: Ensure that at least one of the necessary conditions for deadlock can never hold. Constraining how requests for resources can be made and how they are handled (system design). Deadlock Avoidance: The system requires additional apriori information regarding the overall potential use of each resource for each process. The system dynamically considers every request and decides whether it is safe to grant it at this point, Allows more concurrency. Similar to the difference between a traffic light and a police officer directing traffic.

22 22 Deadlock prevention  restrict how requests can be made. Mutual Exclusion  not required for sharable resources; must hold for nonsharable resources. Hold and Wait  guarantee that whenever a process requests a resource, it does not hold any other resources. A process must request and be allocated all resources before it begins execution, or allow a process to request resources only when it has none. Low resource utilization; starvation possible.

23 23 Deadlock prevention (Cont.) No Preemption If a process holding some resources requests another resource that cannot be immediately allocated to it, then release all resources currently held by the process. Preempted resources added to the list of resources for which the process is waiting. Process restarted only when it can regain its old resources, as well as the new ones that it is requesting. Circular Wait  impose a total ordering of all resource types, and each process requests resources in an increasing order of enumeration.

24 24 Deadlock avoidance Requires that the system has some additional a priori information available Resource-allocation state  defined by the number of available and allocated resources, and the maximum demands of the processes. Each process declares 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..

25 25 Avoidance algorithms Single instance of a resource type  use a resource- allocation graph Multiple instances of a resource type  use the banker’s algorithm

26 26 Resource allocation graph scheme Assignment edge  directed edge R j  P i Request edge  directed edge P i  R j Claim edge P i  R j (dashed line) P j may request resource. Dynamics: A process requests a resource: Claim edge  Request edge A resource is allocated to the process: Request edge  Assignment edge. A resource is released: Assignment edge  Claim edge. Resources must be claimed a priori in the system.

27 27 Resource allocation graph

28 28 Unsafe state in a resource allocation graph

29 29 Resource allocation graph algorithm Assume process P i requests a resource R j The request can be granted  if and only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph

30 30 Banker’s algorithm Multiple resource instances. Each process must a priori claim maximum use. When a process requests a resource it may have to wait gets all its resources it must return them in a finite amount of time.

31 31 Data structures for banker’s algorithm Available: Vector of length m: Available [j] = k  there are k instances of resource type R j available. Max: n x m matrix: Max [i,j] = k  P i may request at most k instances of resource type R j. Allocation: n x m matrix: Allocation[i,j] = k  P i is currently allocated k instances of R j. Need: n x m matrix: Need[i,j] = k  P i may need k more instances of R j to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j]. n  # of processes; m  # of resources types.

32 32 Safety algorithm 1. Work and Finish are vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, …, n- 1. 2.Find i such that: (a) Finish [i] = false (b) Need i  Work If no such i exists, go to step 4. 3. Work i = Work i + Allocation i Finish[i] = true go to step 2. 4.If Finish [i] == true for all i, then the system is in a safe state.

33 33 Resource request algorithm for process P i If request vector Request i [j] = k then P i wants k instances of resource type j (R j. ) 1.If Request i  Need i go to step 2. Otherwise  error (process has exceeded its maximum claim). 2.If Request i  Available, go to step 3. Otherwise  P i must wait (resources are not available). 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available – Request; Allocation i = Allocation i + Request i ; Need i = Need i – Request i ; l If safe  the resources are allocated to P i l If unsafe  P i must wait, and the old resource- allocation state is restored

34 34 Example 5 processes P 0 through P 4 ; 3 resource types: A (10 instances), B (5instances), and C (7 instances). Snapshot at time T 0 : AllocationMaxAvailable A B CA B C A B C P 0 0 1 07 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 P 4 0 0 24 3 3

35 35 Example (cont’d) The content of the matrix Need is defined to be Max – Allocation. Need A B C P 0 7 4 3 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1 The system is in a safe state since the sequence satisfies safety criteria.

36 36 Example: P 1 Request (1,0,2) Check that Request  Available (that is, (1,0,2)  (3,3,2)  true. AllocationNeedAvailable A B CA B CA B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 20 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Executing safety algorithm shows that sequence satisfies safety requirement. Can request for (3,3,0) by P 4 be granted? Can request for (0,2,0) by P 0 be granted?

Download ppt "1 Advanced Operating Systems - Spring 2009 Lecture 9 – February 9, 2009 Dan C. Marinescu Office: HEC 439 B. Office."

Similar presentations

Ads by Google