51
Yet another synchronization Yet another synchronization problem problem The dining philosophers problem Deadlocks o Modeling deadlocks o Dealing with deadlocks Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 1

Yet another synchronization problem

  • Upload
    mikkel

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

Yet another synchronization problem. The dining philosophers problem Deadlocks Modeling deadlocks Dealing with deadlocks. The Dining Philosophers Problem. Philosophers think take forks (one at a time) eat put forks (one at a time) Eating requires 2 forks Pick one fork at a time - PowerPoint PPT Presentation

Citation preview

Page 1: Yet another synchronization problem

Yet another synchronization problemYet another synchronization problem

The dining philosophers problem

Deadlockso Modeling deadlocks

o Dealing with deadlocks

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 1

Page 2: Yet another synchronization problem

The Dining Philosophers ProblemThe Dining Philosophers Problem

Philosopherso thinko take forks (one at a time)o eato put forks (one at a time)

Eating requires 2 forks Pick one fork at a time How to prevent deadlock? What about starvation? What about concurrency?

Slide taken from a presentation by Gadi Taubenfeld, IDC

2Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels

Page 3: Yet another synchronization problem

Dining philosophers: definitionDining philosophers: definition

Each process needs two resources Every pair of processes compete for a specific resource A process may proceed only if it is assigned both A process may proceed only if it is assigned both

resourcesresources Every process that is waiting for a resource should sleep

(be blocked) Every process that releases its two resources must wake-

up the two competing processes for these resources, if they are interested

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 3

Page 4: Yet another synchronization problem

Slide taken from a presentation by Gadi Taubenfeld, IDC

An incorrect naïve solutionAn incorrect naïve solution

( means “waiting for this fork”)

4Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels

Page 5: Yet another synchronization problem

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels

The solutiono A philosopher first

getso only then it tries to

take the 2 forks.

Dining philosophers: textbook solution Dining philosophers: textbook solution

Slide taken from a presentation by Gadi Taubenfeld, IDC

5

Page 6: Yet another synchronization problem

Dining philosophers: textbook solution codeDining philosophers: textbook solution code

#define N 5#define LEFT (i-1) % N#define RIGHT (i+1) % N#define THINKING 0#define HUNGRY 1#define EATING 2int state[N];semaphore mutex = 1;semaphore s[N]; // per each philosophervoid philosopher(int i) {

while(TRUE) {think();pick_sticks(i);eat();put_sticks(i);

}}

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 6

Page 7: Yet another synchronization problem

Dining philosophers: textbook solution code Dining philosophers: textbook solution code ΠΠvoid pick_sticks(int i) {

down(&mutex);state[i] = HUNGRY;test(i);up(&mutex);down(&s[i]);

}

void put_sticks(int i) {down(&mutex);state[i] = THINKING;test(LEFT);test(RIGHT);up(&mutex);

}

void test(int i){if(state[i] == HUNGRY && state[LEFT] != EATING

&& state[RIGHT] != EATING) {state[i] = EATING;up(&s[i]); }

}

Is the algorithm deadlock-free? What about starvation?

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 7

Page 8: Yet another synchronization problem

Textbook solution code: starvation is possibleTextbook solution code: starvation is possible

Eat

Eat

BlockStarvatio

n!

Slide taken from a presentation by Gadi Taubenfeld, IDC

8Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels

Page 9: Yet another synchronization problem

Monitor-based implementationMonitor-based implementation

monitor diningPhilosopherscondition 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]); }

}for i := 0 to 4 do state[i] := THINKING;

end monitor

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 9

Page 10: Yet another synchronization problem

Text-book solution disadvantagesText-book solution disadvantages

An inefficient solution

o reduces to mutual exclusion

o not enough concurrency

o Starvation possible

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 10

Page 11: Yet another synchronization problem

The LR Solution

If the philosopher acquires one fork and the other fork is not immediately available, she holds the acquired fork until the other fork is free.

Two types of philosophers:o L -- The philosopher first obtains

its left fork and then its right fork.o R -- The philosopher first obtains

its right fork and then its left fork.

The LR solution: the philosophers are assigned acquisition strategies as follows: philosopher i is R-type if i is even, L-type if i is odd.

R

L

R

L

R

L

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 11

Slide taken from a presentation by Gadi Taubenfeld, IDC

Page 12: Yet another synchronization problem

Theorem: The LR solution is starvation-free

Assumption: “the fork is fair”.

( means “first fork taken”)

R

L

L

RR

L

0

1

23

4

6

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 12

Slide taken from a presentation by Gadi Taubenfeld, IDC

Page 13: Yet another synchronization problem

DeadlocksDeadlocks

The dining philosophers problem

Deadlocks

oModeling deadlocks

o Dealing with deadlocks

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 13

Page 14: Yet another synchronization problem

Synchronization: DeadlocksSynchronization: Deadlocks

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 14

Page 15: Yet another synchronization problem

Deadlocks

Deadlock of Resource Allocation:o Process A requests and gets Tape driveo Process B requests and gets Fast Modem o Process A requests Fast Modem and blockso Process B requests Tape drive and blocks

Deadlock situation: Neither process can make progress and no process can release its allocated device (resource)

Both resources (devices) require exclusive access

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 15

Page 16: Yet another synchronization problem

ResourcesResources

Resources - Tapes, Disks, Printers, Database Records, semaphores, etc.

Some resources are non-preemptable (i.e. tape drive) It is easier to avoid deadlock with preemptable resources (e.g.,

main memory, database records) Resource allocation procedure

o Requesto Useo Release only at the end – and leave

Block process while waiting for Resources

Iterate

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 16

Page 17: Yet another synchronization problem

Defining Deadlocks Defining Deadlocks

A set of processes is deadlocked if each process is waiting for

an event that can only be caused by another process in the

set

Necessary conditions for deadlock:

1. Mutual exclusion: exclusive use of resources

2. Hold and wait: process can request resource while holding another

resource

3. No preemption: only holding process can release resource

4. Circular wait: there is an oriented circle of processes, each of which is

waiting for a resource held by the next in the circle

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 17

Page 18: Yet another synchronization problem

Modeling deadlocksModeling deadlocks

modeled by a directed graph (resource graph)o Requests and assignments as directed edgeso Processes and Resources as vertices

Cycle in graph means deadlock

Q

A

Process A holds

resource Q

P

B

Process B requests

resource Q

R S

F

M

Deadlock

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 18

Page 19: Yet another synchronization problem

Different possible runs: an exampleDifferent possible runs: an example

A

R

B C

S T

A

Request RRequest SRelease RRelease S

B

Request SRequest TRelease SRelease T

C

Request TRequest RRelease TRelease R

Round-robin scheduling:

1. A requests R

2. B requests S

3. C requests T

4. A requests S

5. B requests T

6. C requests R

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 19

Page 20: Yet another synchronization problem

Different possible runs: an exampleDifferent possible runs: an example

A

R

B C

S T

A

Request RRequest SRelease RRelease S

B

Request SRequest TRelease SRelease T

C

Request TRequest RRelease TRelease R

An alternative scheduling:1. A requests

R

2. C requests T

3. A requests S

4. C requests R

5. A releases R

6. A releases S

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 20

Page 21: Yet another synchronization problem

Multiple Resources of each TypeMultiple Resources of each Type

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 21

Page 22: Yet another synchronization problem

A Directed Cycle But No DeadlockA Directed Cycle But No Deadlock

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 22

Page 23: Yet another synchronization problem

Resource Allocation Graph With A DeadlockResource Allocation Graph With A Deadlock

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 23

Page 24: Yet another synchronization problem

Basic FactsBasic Facts

If graph contains no cycles no deadlock

If graph contains a cycle

o if only one instance per resource type, then deadlock

o if several instances per resource type, deadlock

possible

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 24

Page 25: Yet another synchronization problem

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 25

Dealing with DeadlocksDealing with Deadlocks

The dining philosophers problem

Deadlocks

oModeling deadlocks

o Dealing with deadlocks

Page 26: Yet another synchronization problem

Dealing with DeadlocksDealing with Deadlocks

Possible Strategies:o Preventionstructurally negate one of the four necessary conditions

o Avoidanceallocate resources carefully, so as to avoid deadlocks

o Detection and recoveryo Do nothing (The “ostrich algorithm’’)deadlocks are rare and hard to tackle... do nothing Example: Unix - process table with 1000 entries and 100 processeseach requesting 20 FORK calls... Deadlock.users prefer a rare deadlock over frequent refusal of FORK

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 26

Page 27: Yet another synchronization problem

Deadlock preventionDeadlock prevention

Attack one of the 4 necessary conditions:1. Mutual exclusion

o Minimize exclusive allocation of deviceso Use spooling: only spooling process requests access (not good for all

devices - Tapes; Process Tables); may fill up spools (disk space deadlock)...

2. Hold and Waito Request all resources immediately (before execution)Problem: resources not known in advance, inefficientoro to get a new resource, free everything, then request everything again

(including new resource)

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 27

Page 28: Yet another synchronization problem

Attack one of the 4 necessary conditions (cont'd)Attack one of the 4 necessary conditions (cont'd)

3. No preemptiono Not always possible (e.g., printer, tape-drive)

4. Circular wait conditiono Allow holding only a single resource (too restrictive)o Number resources, allow requests only in ascending order: Request only resources numbered higher than anything currently

held

Impractical in general

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 28

Page 29: Yet another synchronization problem

Deadlock AvoidanceDeadlock Avoidance

System grants resources only if it is safe basic assumption: maximum resources required by each

process is known in advance

Safe state: Not deadlocked There is a scheduling that satisfies all possible

future requests

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 29

Page 30: Yet another synchronization problem

Safe states: exampleSafe states: example

p q l1 l2 l4l3

l5

l6

l7

l8

rs

t

A

B

Printer

Plotter

Plotter

Printer

Both have printer

Both have plotter

Both have both

Unsafe state

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 30

Page 31: Yet another synchronization problem

Safe and Unsafe states Safe and Unsafe states (single resource)(single resource)

Fig. 6-9. Three resource allocation states: (a) Safe. (b) Safe. (c) Unsafe.

(a) (b) (c)

Safe state:o Not deadlockedo There is a way to satisfy all possible future requests

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 31

Page 32: Yet another synchronization problem

Banker's Algorithm, Dijkstra 1965 (single resource)Banker's Algorithm, Dijkstra 1965 (single resource)

Checks whether a state is safe

1. Pick a process that can terminate after fulfilling the rest of its requirements (enough free resources)2. Free all its resources (simulation)3. Mark process as terminated4. If all processes marked, report “safe”, halt5. If no process can terminate, report “unsafe”, halt6. Go to step 1

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 32

Page 33: Yet another synchronization problem

Multiple resources of each kindMultiple resources of each kind

Assume n processes and m resource classes

Use two matrixes and two vectors:

o Current allocation matrix Cn x m

o Request matrix Rn x m (remaining requests)

o Existing resources vector Em

o Available resources vector Am

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 33

Page 34: Yet another synchronization problem

Banker’s Algorithm for multiple resourcesBanker’s Algorithm for multiple resources

1. Look for a row of R whose unmet resource needs are all smaller

than or equal to A. If no such row exists, the system will

eventually deadlock.

2. Otherwise, assume the process of the row chosen finishes

(which will eventually occur). Mark that process as terminated

and add the i’th row of C 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.

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 34

Page 35: Yet another synchronization problem

deadlock avoidance – an example with deadlock avoidance – an example with 4 resource types, 5 processes4 resource types, 5 processes Tape-drives Plotters Scanners CD-ROMs

E = (6 3 4 2)A = (1 0 2 0)

C = R =

A3011

B0100

C1110

D1101

E0000

T P S C A1100

B0112

C3100

D0010

E2110

T P S C

Is the current state safe?

Yes, let’s see why…

We let D run until it finishes

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 35

Page 36: Yet another synchronization problem

deadlock avoidance – an example with deadlock avoidance – an example with 4 resource types, 5 processes4 resource types, 5 processes Tape-drives Plotters Scanners CD-ROMs

E = (6 3 4 2)A = (2 1 2 1)

C = R =

A3011

B0100

C1110

D0000

E0000

T P S C A1100

B0112

C3100

D0000

E2110

T P S C

We now let E run until it finishesNext we let A run until it finishes

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 36

Page 37: Yet another synchronization problem

deadlock avoidance – an example with deadlock avoidance – an example with 4 resource types, 5 processes4 resource types, 5 processes Tape-drives Plotters Scanners CD-ROMs

E = (6 3 4 2)A = (5 1 3 2)

C = R =

A0000

B0100

C1110

D0000

E0000

T P S C A0000

B0112

C3100

D0000

E0000

T P S C

Finally we let B and C run.

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 37

Page 38: Yet another synchronization problem

Back to original stateBack to original state

Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2)A = (1 0 2 0)

C = R =

A3011

B0100

C1110

D1101

E0000

T P S C A1100

B0112

C3100

D0010

E2110

T P S C

If B now requests a Scanner, we can allow it.

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 38

Page 39: Yet another synchronization problem

This is still a safe state…This is still a safe state…

Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2)A = (1 0 1 0)

C = R =

A3011

B0110

C1110

D1101

E0000

T P S C A1100

B0102

C3100

D0010

E2110

T P S C

If E now requests a Scanner, granting the If E now requests a Scanner, granting the request leads to an unsafe staterequest leads to an unsafe state

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 39

Page 40: Yet another synchronization problem

This state is unsafeThis state is unsafe

Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2)A = (1 0 0 0)

C = R =

A3011

B0110

C1110

D1101

E0010

T P S C A1100

B0102

C3100

D0010

E2100

T P S C

We must not grant E’s requestWe must not grant E’s request

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 40

Page 41: Yet another synchronization problem

Deadlock Avoidance is not practicalDeadlock Avoidance is not practical

Maximum resource request per process is unknown

beforehand

Resources may disappear

New processes (or resources) may appear

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 41

Page 42: Yet another synchronization problem

Deadlock Detection and RecoveryDeadlock Detection and Recovery

Find if a deadlock exists

if it does, find which processes and resources it involes

Detection: detect cycles in resource graph

Algorithm: DFS + node and arc marking

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 42

Page 43: Yet another synchronization problem

Find cycles:

For each node, N, in the graph, perform the following 5 steps with N as the starting node

1. Initialize L to the empty list and designate all arcs as unmarked2. Add the current node to the end of L and check if the node appears twice

in L. If it does, the graph contains a cycle, terminate.

3. If there are any unmarked arcs from the given node, go to 4., if not go to 5.

4. Pick an unmarked outgoing arc and mark it. Follow it to the new current node and go to 2.

5. We have reached a deadend. Go back to the previous node, make it the current node and go to 3. If all arcs are marked and the node is the initial node, there are no cycles in the graph, terminate

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 43

Page 44: Yet another synchronization problem

Detection - extract a cycleDetection - extract a cycle1. Process A holds R and requests S2. Process B holds nothing and requests T3. Process C holds nothing and requests S4. Process D holds U and requests S and T5. Process E holds T and requests V6. Process F holds W and requests S7. Process G holds V and requests U

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 44

Page 45: Yet another synchronization problem

When should the system check for deadlock ?When should the system check for deadlock ?

Whenever a request is made - too expensive

every k time units...

whenever CPU utilization drops bellow some threshold

(indication of a possible deadlock..)

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 45

Page 46: Yet another synchronization problem

RecoveryRecovery Preemption - possible in some rare cases temporarily take a resource away from its current owner Rollback - possible with checkpointing Keep former states of processes (checkpoints) to

enable release of resources and going back Killing a process - easy way out, may cause problems in

some cases, depending on process being rerunable… Bottom line: hard to recover from deadlock, avoid it

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 46

Page 47: Yet another synchronization problem

Example - deadlocks in DBMSsExample - deadlocks in DBMSs

For database records that need locking first and

then updating (two-phase locking)

Deadlocks occur frequently because records are

dynamically requested by competing processes

DBMSs, therefore, need to employ deadlock

detection and recovery procedures

Recovery is possible - transactions are

“checkpointed” - release everything and restart

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 48

Page 48: Yet another synchronization problem

Additional deadlock issuesAdditional deadlock issues

Deadlocks may occur with respect to actions of processes, not resources - waiting for semaphores

Starvation can result from a bad allocation policy (such as smallest-file-first, for printing) and for the “starved” process will be equivalent to a deadlock (cannot finish running)

Summary of deadlock treatment:o Ignore problemo Detect and recovero Avoid (be only in safe states)o Prevent by using an allocation policy or conditions

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 51

Page 49: Yet another synchronization problem

The Situation in PracticeThe Situation in Practice

Most OSs in use, and specifically Windoes, Linux…, ignore

deadlock or do not detect it

Tools to kill processes but usually without loss of data

In Windows NT there is a system call WaitForMultipleObjects

that requests all resources at onceo System provides all resources, if free

o There is no lock of resources if only few are free

o Prevents Hold & Wait, but difficult to implement!

Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 52

Page 50: Yet another synchronization problem

Linux: the Big Kernel LockLinux: the Big Kernel Lock

Linux was first designed with Coarse-Grained LockingThe whole kernel was wrapped in a giant lock around it

to avoid deadlocks (kernel / interrupt handlers / user threads) introduced in Linux 2.0.

Work began in 2008 to remove the big kernel lock: http://kerneltrap.org/Linux/Removing_the_Big_Kernel_Lock

It was carefully replaced with fine-grained locks until it was removed in Linux 2.6.39 (in 2011!)https://lwn.net/Articles/424657/

Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 53Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 53

Page 51: Yet another synchronization problem

Xv6 and deadlocksXv6 and deadlocks Xv6 uses a few coarse data-structure specific locks; for example, xv6

uses a single lock protecting the process table and its invariants, which are described in Chapter 5.

A more fine-grained approach would be to have a lock per entry in the process table so that threads working on different entries in the process table can proceed in parallel.

However, it complicates operations that have invariants over the whole process table, since they might have to take out several locks.

To avoid such deadlocks, all code paths must acquire locks in the same order. Deadlock avoidance is another example illustrating why locks must be part of a function’s specification: the caller must invoke functions in a consistent order so that the functions acquire locks in the same order

Because xv6 uses coarse-grained locks and xv6 is simple, xv6 has few lock-order chains. The longest chain is only two deep. For example, ideintr holds the ide lock while calling wakeup, which acquires the ptable lock.

Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 54Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels 54