360 likes | 376 Views
Advanced Operating Systems - Spring 2009 Lecture 9 – February 9, 2009. Dan C. Marinescu Email: dcm@cs.ucf.edu Office: HEC 439 B. Office hours: M, Wd 3 – 4:30 PM. TA: Chen Yu Email: yuchen @cs.ucf.edu Office: HEC 354. Office hours: M, Wd 1.00 – 3:00 PM. Last, Current, Next Lecture.
E N D
Advanced Operating Systems - Spring 2009Lecture 9 – February 9, 2009 • Dan C. Marinescu • Email: dcm@cs.ucf.edu • Office: HEC 439 B. • Office hours: M, Wd 3 – 4:30 PM. • TA: Chen Yu • Email: yuchen@cs.ucf.edu • Office: HEC 354. • Office hours: M, Wd 1.00 – 3:00 PM.
Last, Current, Next Lecture • Last time: • Atomic transactions • Today • Three-way handshake • Deadlocks • Next time: • Deadlocks
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: • The client sends a SYN to the server for an active open. • The server replies with a SYN-ACK. • The client sends an ACK (usually called SYN-ACK-ACK) back to the server.
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).
Deadlocks • Deadlocks • System Model • Deadlock Characterization • Safe State • Resource Allocation Graph • Methods for Handling Deadlocks • Deadlock Prevention • Deadlock Avoidance • Deadlock Detection • Recovery from Deadlock
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.
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 P0P1 wait (A); wait(B) wait (B); wait(A) • Aim prevent or avoid deadlocks
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.
System model • Resource types R1, R2, . . ., Rm (CPU cycles, memory space, I/O devices) • Each resource type Ri has Wi instances. • Resource access model: • request • use • release
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 {P0, P1, …, P0} 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 P0 is waiting for a resource that is held by P0.
Safe state • Safe state there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i (ordering of processes). This implies: • If Pi resource needs are not immediately available then Pi can wait until all Pjhave finished. • When Pj finishes Pi can obtain needed resources, execute, return allocated resources, and terminate. • When Pi terminates Pi +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.
Safe state and deadlocks • Safe state no deadlocks. • Unsafe state possibility of deadlock.
Resource allocation graph (V,E) • Directed bipartite graph: two types of nodes in V: • P = {P1, P2, …, Pn} processes. • R = {R1, R2, …, Rm} resource types. • request edge – directed edge P1 Rj • assignment edge– directed edge Rj Pi
Resource allocation graph (cont’d) • Process • Resource Type with 4 instances • Pirequests an instance of Rj • Pi is holds an instance of Rj Pi Rj Pi Rj
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.
Alternatives • Deadlock prevention and avoidance ensure that the system will neverenter a deadlock state. • Allow the system to enter a deadlock state then recover. • Ignore the problem used by most operating systems, including UNIX.
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.
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.
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.
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.
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 scheme • Assignment edge directed edge Rj Pi • Request edge directed edge Pi Rj • Claim edgePi Rj (dashed line) Pj 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.
Resource allocation graph algorithm • Assume process Pi requests a resource Rj • 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
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.
Data structures for banker’s algorithm n # of processes; m # of resources types. • Available: Vector of length m: • Available [j] = k there are k instances of resource type Rjavailable. • Max: n x m matrix: • Max [i,j] = k Pimay request at most k instances of resource type Rj. • Allocation: n x m matrix: • Allocation[i,j] = k Pi is currently allocated k instances of Rj. • Need: n x m matrix: • Need[i,j] = k Pi may need k more instances of Rjto complete its task. Need [i,j] = Max[i,j] – Allocation [i,j].
Safety algorithm 1.Workand 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) Needi Work If no such i exists, go to step 4. • Worki = Worki+ AllocationiFinish[i] = truego to step 2. 4. If Finish [i] == true for all i, then the system is in a safe state.
Resource request algorithm for process Pi If request vector Requesti[j] = k then Pi wants k instances of resource type j (Rj.) 1. If Requesti Needigo to step 2. Otherwise error (process has exceeded its maximum claim). 2. If Requesti Available, go to step 3. OtherwisePi must wait (resources are not available). 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available – Request; Allocationi= Allocationi + Requesti; Needi= Needi – Requesti; • If safe the resources are allocated to Pi • If unsafe Pimust wait, and the old resource-allocation state is restored
Example • 5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances). • Snapshot at time T0: AllocationMaxAvailable A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3
Example (cont’d) • The content of the matrix Need is defined to be Max – Allocation. Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 • The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
Example: P1 Request (1,0,2) • Check that Request Available (that is, (1,0,2) (3,3,2) true. AllocationNeedAvailable A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 1 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 • Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement. • Can request for (3,3,0) by P4 be granted? • Can request for (0,2,0) by P0 be granted?