operating systems, 142 practical session 8 deadlocks 1

43
Operating Systems, 142 Practical Session 8 Deadlocks 1

Upload: aaliyah-ellison

Post on 14-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems, 142 Practical Session 8 Deadlocks 1

Operating Systems, 142

Practical Session 8 Deadlocks

1

Page 2: Operating Systems, 142 Practical Session 8 Deadlocks 1

Deadlocks

The ultimate form of starvation.

A set of processes is deadlocked if each processin the set is waiting for an event that onlyanother process in the set can cause.

Page 3: Operating Systems, 142 Practical Session 8 Deadlocks 1

Conditions for Deadlock

1. Mutual exclusion – resources may be used by only one process

2. Hold and wait – a process can request a resource while holding one

3. No preemption - only the process holding a resource can release it

4. Circular wait - two or more processes each waiting for a resource held by the other

Page 4: Operating Systems, 142 Practical Session 8 Deadlocks 1

Solving the Deadlock Problem

1. The Ostrich ‘Algorithm’ – Ignore the problem2. Deadlock Detection & Recovery – Detect a

deadlock by finding a cyclic graph of processes and resources, and recover

3. Deadlock Avoidance – Detect safe and unsafe states; Banker’s algorithm

4. Deadlock Prevention – Ensure that at least one of the four conditions for a deadlock is never satisfied

Page 5: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 1

Assume resources are ordered as: R1, R2,...Rn

Prove formally that if processesalways request resources by order (i.e. if a process requests Rk after Rj then k>j) then a

deadlock will not occur.

For simplicity assume resources are unique. That is, there is a single unit of each resource.

Page 6: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 1 – a simple example

There are 2 processes P1 ,P2 and resource R1 ,R2.

P1 has R1 and P2 has R2. P1 requests R2 and is now waiting for P2 to

release it.

For a deadlock to occur P2 needs to request R1.

However, this is in contrast to the assumption that resources can only be requested in ascending order.

That is, condition 4 (Circular wait) is prevented if resources are requested in ascending order.

Page 7: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 1 – a more formal proof

Let P1,...,Pn denote the processes in the system and R1,…Rk the resources.

Falsely assume that a deadlock may occur in this system. Specifically, for a deadlock to occur a subset Pi1,...,Pim of {P1,...,Pn} which satisfies the following condition must exist:

Pi1 -> (Rj,1)->Pi2 - (Rj,2)->...-> (Rj,m-1)->Pim -> (Rj,m)->Pi1 (*)

Where Pi -> Rk->Pj is used to describe the following relation: Pi requests a resource Rk held by Pj.

Page 8: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 1 – a more formal proof

Each process Pi,s , (s≠1) holds resource Rj,s-1 and requests resource Rj,s. This means that j,s-1<j,s for any s (since the resources are numbered, and are requested in an ascending order). We receive the following inequality which defines the resource ordering: j,1<j,2<...<j,m.

In a deadlock, eq. (*) must hold. Thus we conclude that j,m<j,1 and j,1<j,m.

That is, a circular wait is not possible and the system is deadlock- free.

Page 9: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 1 – Simple ExplanationLets assume we have a deadlock. A circular wait must therefore exist:Pi1 - (Rj,1)->Pi2 - (Rj,2)->Pi3 - (Rj,3)->...- (Rj,m-1)->Pim - (Rj,m)->Pi1 (*)

One of these resources’ index is the largest of all resources indices listed. It doesn’t matter which one. Lets assume its Rj,2 (since we deal with a circle it doesn’t matter which one we pick). Pi2 -> (Rj,2)->Pi3 -> (Rj,3)->… (*) This resource is held by process Pi3, process Pi2 is asking for it, and Pi3 requires Rj3 at the same time.Lets look at Rj3:•If j3 > j2 : this contradicts Rj2 being the largest index resource.•If j3 = j2 : it makes no sense Pi3 is asking for a resource it already has? It breaks the waiting circle.•If j3 < j2 : this contradicts the fact that processes ask for resources only in ascending order.Contradictions in every scenario means that our initial assumption of deadlock is false!

Page 10: Operating Systems, 142 Practical Session 8 Deadlocks 1

The Dining Philosophers Problem

10

• Philosophers think and eat every now and then

• 2 forks are required for eating

• There aren’t enough forks

Page 11: Operating Systems, 142 Practical Session 8 Deadlocks 1

Dining Philosophers:Monitor-based implementationmonitor diningPhilosophers

condition self[N];integer state[N]; procedure pick_sticks(i)

state[i] := HUNGRY; test(i); if state[i] <> EATING then wait(self[i]);

procedure put_sticks(i) state[i] := THINKING; test(LEFT); test(RIGHT);

procedure test(i)if (state[LEFT] <> EATING &&

state[RIGHT] <> EATING &&

state[i] = HUNGRY) then { state[i] :=

EATING; signal(self[i]);

}

end monitor

11

Page 12: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2 (from exam 2003 moed b , q.1)

We extend the monitor-based implementation for 5 philosophers and add to the table a center plate, that is filled by a waiter. The plate holds at most 5 courses.

Every philosopher, after taking 2 forks, takes exactly 3 courses from the central plate (one after the other). After taking the courses he eats and returns the forks.

a. Write the code for the philosophers and the waiter (based on the previous implementation). Write the required code inside and outside the monitor.

Page 13: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.aWe add 2 condition variables: full , empty and an integer: inPlate = 0We add 2 procedures to the monitor:

procedure fillPlate

if (inPlate<5){ inPlate++ empty.signal()}else full.wait()

procedure takeFromPlate

if(inPlate = = 0)empty.wait()

inPlate--full.signal()

Page 14: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.aupdate the philosopher’s function:

procedure philosopher(int i) while(TRUE){

think();pick_sticks(i);for(j = 1 to 3) takeFromPlate()

eat() put_sticks(i); }

and add the waiter’s function:

Procedure waiter()while(TRUE)

fillPlate()

Page 15: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.b

b. In the following sections assume that the center plate starts as full and that the waiter is quick (brings 3 courses each time). Also, the waiter fills the center plate only after a philosopher finished eating.

Update the code of section a accordingly.

Page 16: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.b

We add another condition varaiable: still_eatingand integer: finished_eating = 0.

Since we want the waiter to fill the plate only when a philosopher has finished eating we have to assume that the plate is full in the beginning, i.e inPlate = 5 (we don’t need the full conditionvariable anymore).

Page 17: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.b Cont’procedure fillPlate

if (finished_eating==0)still_eating.wait()

inPlate+=3finished_eating--empty.signal()

procedure takeFromPlate if(inPlate = = 0){

empty.wait()}inPlate --

/*the only change is that we don’t use full anymore */

Page 18: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.b Cont’We also add two lines to the put_sticks function :

procedure put_sticks(i)state[i] := THINKING; test(LEFT); test(RIGHT);finished_eating ++still_eating.signal()

Page 19: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.c

Is a deadlock possible? (assuming a quick waiter)

Page 20: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.cIn the case of 5 philosophers this cannot happen: at any given moment there may be at most 2 philosophers that picked up 2 forks. Each one asks for 3 dishes and there are 5 available dishes. At least one of the philosophers will manage 3 dishes on her plate and will eventually wake up the waiter.

Page 21: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.d

Is it possible to have a deadlock with 6 philosophers?

Page 22: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 2.dIn the case of 6 philosophers, 3 philosophers may pick up 2 forks at the same time.In this case, one possible scenario is that the first takes 2 dishes to her plate, the second takes 2 as well, and the third grabs the last one. As a result, no one will reach the put_sticks phase and the waiter will not be woken up.

Page 23: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 3Six philosophers sit around a table, with a large plate of food in the middle. Forks (represented as ‘F’) and knives (represented as ‘K’) are arranged as shown below. Each philosopher obeys the following algorithm:1) Grab the closest knife.2) Grab the closest fork.3) Carve and devour a piece of broccoli [yummy] .4) Put down the knife and fork back to where they were.

Can a deadlock occur? Indicate why this algorithm satisfies our deadlock conditions, or which one(s) it avoids. If you said that the system could deadlock, describe a reasonable deadlock avoidance scheme. If you said otherwise, specify which scheme is used in this solution.

Page 24: Operating Systems, 142 Practical Session 8 Deadlocks 1

Answer 3

A deadlock will not occur since the algorithm defines a partial ordering, removing the ability to have circular requests. The easiest way to see the partial order is to label knives 1,2, 3 and forks 4,5,6. Clearly we only acquire in ascending order, and as shown in the previous questions this prevents circularwaiting.

1

2

3

4

5

6

Page 25: Operating Systems, 142 Practical Session 8 Deadlocks 1

Banker’s Algorithm

Safe – a state is said to be safe if a. It is not deadlocked.b. There is some scheduling order in which every

process can run to completion even if all of them suddenly request their maximum number of resources immediately.

Page 26: Operating Systems, 142 Practical Session 8 Deadlocks 1

Banker’s Algorithm

Resources:A. Vectors:

1. E - Number of Existing resources of each type.2. P – Number of resources of each type in Possession by

the processes.3. A – Number of Available resources of each type.

B. Matrices: (rows are processes and columns are resources)

1. C – Current allocation matrix2. R – Request matrix

Page 27: Operating Systems, 142 Practical Session 8 Deadlocks 1

Banker’s Algorithm

1. Look for a row in matrix R whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system may eventually deadlock.

2. Assume the process of the row chosen finishes (which is possible). Mark that process as terminated and add all its resources to the A vector

3. Repeat steps 1 and 2 until either all processes are marked terminated, which means safe, or until a deadlock occurs, which means unsafe.

Page 28: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

current allocation max demand still needsProcess r1 r2 r3 r4 r1 r2 r3 r4 r1 r2 r3 r4

p1 0 0 1 2 0 0 1 2p2 2 0 0 0 2 7 5 0p3 0 0 3 4 6 6 5 6p4 2 3 5 4 4 3 5 6p5 0 3 3 2 0 6 5 2

r1 r2 r3 r42 1 0 0

Consider the following snapshot of a system with five processes (p1, ... p5) and four resources (r1, ... r4). currently Available resources

Page 29: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

a. Compute what each process still might request and fill in the “still needs” columns.

b. Is this system currently deadlocked, or will

any process become deadlocked? Use the baker’s algorithm to support your answer

Page 30: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

a)

current allocation

max demand still needs

Process r1 r2 r3 r4 r1 r2 r3 r4 r1 r2 r3 r4

p1 0 0 1 2 0 0 1 2p2 2 0 0 0 2 7 5 0p3 0 0 3 4 6 6 5 6p4 2 3 5 4 4 3 5 6p5 0 3 3 2 0 6 5 2

Page 31: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

a)

Not deadlocked and will not become deadlocked.Using the Banker’s algorithm, we determine the process execution order: p1, p4, p5, p2, p3. r1 r2 r3 r4

2 1 0 0

b)

currently available resources

current allocation

max demand still needs

Process r1 r2 r3 r4 r1 r2 r3 r4 r1 r2 r3 r4

p1 0 0 1 2 0 0 1 2 0 0 0 0p2 2 0 0 0 2 7 5 0 0 7 5 0p3 0 0 3 4 6 6 5 6 6 6 2 2p4 2 3 5 4 4 3 5 6 2 0 0 2p5 0 3 3 2 0 6 5 2 0 3 2 0

Page 32: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

c. If a request from p3 arrives for (0, 1, 0, 0), can that request be safely granted immediately? In what state (deadlocked, safe, unsafe) would immediately granting the request leave the system? Which processes, if any, are or may become deadlocked if this whole request is granted immediately?

Page 33: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

r1 r2 r3 r42 0 0 0

currently available resources

current allocation

max demand still needs

Process r1 r2 r3 r4 r1 r2 r3 r4 r1 r2 r3 r4

p1 0 0 1 2 0 0 1 2 0 0 0 0p2 2 0 0 0 2 7 5 0 0 7 5 0p3 0 1 3 4 6 6 5 6 6 5 2 2p4 2 3 5 4 4 3 5 6 2 0 0 2p5 0 3 3 2 0 6 5 2 0 3 2 0

Page 34: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 4

c) Change available to (2, 0, 0, 0) and p3’s row of “still needs” to (6, 5, 2, 2). Now p1, p4, and p5 can finish. Available will now be (4, 6, 9, 8) meaning that neither p2 nor p3’s “still needs” can be satisfied. So, it is not safe to grant p3’s request. Correct answer NO. Processes p2 and p3 may deadlock.

Page 35: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 5 (7.6 from Silberschats)

If deadlocks are controlled (avoided) by applying the banker‘s algorithm, which of the following changes can be made safely and under what circumstances: 1.Increase Available (add new resources) 2.Decrease Available (remove resources) 3.Increase Max for one process 4.Increase the number of processes

Page 36: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 5

1. Increasing the number of resources available can't create a deadlock since it can only decrease the number of processes that have to wait for resources.

2. Decreasing the number of resources can cause a deadlock.

Page 37: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 5

3. From the Banker’s algorithm point of view, increasing the maximum claim of a process may turn a safe state into unsafe.

4. If the number of processes is increased, the state remains safe, since we can first run the “old” processes, until they terminate, and release all their resources. Now , when all the system’s resources are free, we can choose a “new” process and give it all its demands. It will finish and again all the system’s resources will be free. Again, we choose a new process and give it all its demands , etc.

Page 38: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 6 (7.9 from Silberschats)

Consider a system consisting of m resources of the same type, being shared by n processes. Resources can be requested and released by processes only one at a time. Show that the system is deadlock free if the following two conditions hold:1. Each process needs between 1 and m

resources. 2. The sum of maximum needs is less than m+n.

Page 39: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 6

By contradiction, assume that the 4 conditions for deadlock exist in the system and thus there is a group of processes involved in a circular wait. Let these processes be P1,...,Pk, k≤n, their current demands be D1,...,Dk and the number of resources each of them holds be H1,...,Hk.

The circular wait condition should look like: P1->P2->...->Pk->P1, but in fact it is simpler: Let M1,...,Mn be total (maximum) demands of processes P1,...,Pn.Then a circular wait can occur only if all resources are in use and everyprocess hasn't acquired all its resources: H1+...+Hk=m and Di>=1 for 1≤ i≤k.

Page 40: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 6

Since Mi=Hi+Di, the sum of maximum demands of the processes involved in a circular wait is: M1+..+Mk≥m+k.

Note that the remaining processes’ Pk+1,...,Pn maximum demandsare at least 1: Mi ≥ 1, k+1 ≤ i ≤ n and thus Mk+1+...+Mn ≥ n-k .

The total sum of maximum demands is thus: M1+...+Mn = M1+...+Mk+Mk+1+...+Mn ≥ m+k+(n-k)=m+n.

It is defined that sum of all maximal needs is strictly less than m+n, thus we have a contradiction.

Page 41: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 6 – Intuitive Explanation• If we have a deadlock all resources are held by the various

processes, otherwise some process can take a resource and “advance” and we are not in a deadlock . Therefore:

• Every process needs at least 1 resource more, or else we don’t have a deadlock (it is free to “advance”). Therefore:

• Total sum of maximum demands is:

• This contradicts the assumption total sum of maximum demands is less than m+n.

nmDHDHMn

i

n

iii

n

iii

n

ii

1 111

mHn

ii

1

nDn

ii

1

Page 42: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 7

True or FalseMonitors prevent deadlocks from ever occurring.

Page 43: Operating Systems, 142 Practical Session 8 Deadlocks 1

Question 7

If monitor code has a circular dependency, it candeadlock. Assume there are two monitors a,b and two processes A,B. A holds a and waits for b. B holds b and waits for a.