Transcript Lecture9

Advanced Operating Systems - Spring 2009
Lecture 9 – February 9, 2009

Dan C. Marinescu




Email: [email protected]
Office: HEC 439 B.
Office hours: M, Wd 3 – 4:30 PM.
TA: Chen Yu



Email: [email protected]
Office: HEC 354.
Office hours: M, Wd 1.00 – 3:00 PM.
1
Last, Current, Next Lecture

Last time:


Today



Atomic transactions
Three-way handshake
Deadlocks
Next time:

Deadlocks
2
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.
2.
3.
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.
3
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).
4
Client
Process
Server
process
INTERNET
I want to connect to you
SYN=1 SN=117
My SN is 117
Ok, let’s do it
SYN=1 SN=23 AN=118
My SN is 23 and I expect from you a SN=118
Thanks
AK=23 SN=118
I got your SN=23 and I acknowledge it. My new SN is 118
5
Deadlocks
 Deadlocks
 System Model
 Deadlock Characterization
 Safe State
 Resource Allocation Graph
 Methods for Handling Deadlocks
Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection

 Recovery from Deadlock
6
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.
7
A
B
J
J
K
K
A
B
8
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
P0
P1
wait (A);
wait(B)
wait (B);
wait(A)


Aim prevent or avoid deadlocks
9
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.

10
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
11
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.
12
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 Pj have 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.
13
Safe state and deadlocks

Safe state  no deadlocks.

Unsafe state  possibility of
deadlock.
14
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
15
Resource allocation graph (cont’d)

Process

Resource Type with 4 instances

Pi requests an instance of Rj
Pi
Rj

Pi is holds an instance of Rj
Pi
Rj
16
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.
17
Resource allocation graph with a deadlock
18
Graph with a cycle but no deadlock
19
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.
20
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.
21
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.
22
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.
23
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.
24
Avoidance algorithms

Single instance of a resource type  use a resourceallocation graph

Multiple instances of a resource type  use the
banker’s algorithm
25
Resource allocation graph scheme




Assignment edge  directed edge Rj  Pi
Request edge  directed edge Pi  Rj
Claim edge Pi  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.
26
Resource allocation graph
27
Unsafe state in a resource allocation graph
28
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
29
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.
30
Data structures for banker’s algorithm
n  # of processes; m  # of resources types.

Available: Vector of length m:


Max: n x m matrix:


Max [i,j] = k  Pi may request at most k instances of resource type Rj.
Allocation: n x m matrix:


Available [j] = k there are k instances of resource type Rj available.
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 Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j].
31
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) Needi  Work
If no such i exists, go to step 4.
3.
Worki = Worki + Allocationi
Finish[i] = true
go to step 2.
4.
If Finish [i] == true for all i, then the system is in a safe state.
32
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  Needi go to step 2. Otherwise  error
(process has exceeded its maximum claim).
2. If Requesti  Available, go to step 3. Otherwise Pi
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  Pi must wait, and the old resourceallocation state is restored
33
Example


5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances).
Snapshot at time T0:
Allocation
Max
Available
ABC
ABC
ABC
P0
010
753
332
P1
200
322
P2
302
902
P3
211
222
P4
002
433
34
Example (cont’d)

The content of the matrix Need is defined to be Max –
Allocation.
P0
P1
P2
P3
P4

Need
ABC
743
122
600
011
431
The system is in a safe state since the sequence < P1, P3, P4,
P2, P0> satisfies safety criteria.
35
Example: P1 Request (1,0,2)




Check that Request  Available (that is, (1,0,2)  (3,3,2)  true.
Allocation
Need
Available
ABC
ABC
ABC
P0 0 1 0
743
230
P1 3 0 2
020
P2 3 0 1
600
P3 2 1 1
011
P4 0 0 2
431
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?
36