1 Overview Assignment 8: hints Deadlocks Assignment 7: solution See sample solution on the web.

8
1 Overview Assignment 8: hints Deadlocks Assignment 7: solution See sample solution on the web

Transcript of 1 Overview Assignment 8: hints Deadlocks Assignment 7: solution See sample solution on the web.

Page 1: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

1

Overview Assignment 8: hints

Deadlocks Assignment 7: solution

See sample solution on the web

Page 2: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

2

A8 Ex1 – Barrier Objects synchronize processes “checkpoint”: continue only when all

processes are there

checkpoint checkpoint checkpoint

PROCESS:FOR i := 0 TO Max DO

DoPhase(i);barrier.Sync

END

Barrier = OBJECTInit(count);Sync;

Page 3: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

3

Barrier, Example

P1

P2

P3

Barrier.synchronize()

// code after the barrier

// code before the barrier

Page 4: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

4

A8 Ex1 - Task Write a barrier implementation in the

(reasonable) language of your choice. The Barrier object should have the

following methods: Initialize: to specify how many process are to

be synchronized by this barrier.Synchronize: wait until all processes reached

the barrier.

Page 5: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

5

A8 Ex2 – Deadlocks Coffman conditions

Mutual exclusion in resource usage Holding a resource and waiting No preemption of resources possible Circular wait

Solutions use protocol that ensures the system will never

deadlock detect deadlock and recover ignore (...and reboot)

Page 6: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

6

Deadlocks Transactions in a bank

move $ from x to y concurrent system acquire lock on both accounts, then perform transfer

Deadlock avoidance: mutual exclusion (accounts are locked) no preemption (once locked, unlock only after

transfer) hold and wait (first account locked, waiting for second

one) must avoid circular wait..... HOW?

Page 7: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

7

Deadlocks: example Example:

p1: lock(x, y);p2: lock(y, x);

Scenario:p1 locks xp2 locks yp1 tries to lock y and waitsp2 tries to lock x and waitsDEADLOCK

Page 8: 1 Overview Assignment 8: hints  Deadlocks Assignment 7: solution  See sample solution on the web.

8

A8 Ex3 – Baboons Problem

more than one baboon can cross (but only in one direction)

if two (or more) baboons cross in different directions we have a deadlock.

Part a): avoid deadlocks using semaphores.

Part b): avoid starvation.