Overview n Assignment 8 hints Deadlocks n Assignment

  • Slides: 8
Download presentation
Overview n Assignment 8: hints ¨ Deadlocks n Assignment 7: solution ¨ See sample

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

A 8 Ex 1 – Barrier Objects synchronize processes n “checkpoint”: continue only when

A 8 Ex 1 – Barrier Objects synchronize processes n “checkpoint”: continue only when all processes are there n checkpoint Barrier = OBJECT Init(count); Sync; checkpoint PROCESS: FOR i : = 0 TO Max DO Do. Phase(i); barrier. Sync END 2

Barrier, Example P 1 P 2 P 3 // code before the barrier Barrier.

Barrier, Example P 1 P 2 P 3 // code before the barrier Barrier. synchronize() // code after the barrier 3

A 8 Ex 1 - Task Write a barrier implementation in the (reasonable) language

A 8 Ex 1 - Task Write a barrier implementation in the (reasonable) language of your choice. n The Barrier object should have the following methods: n ¨ Initialize: to specify how many process are to be synchronized by this barrier. ¨ Synchronize: wait until all processes reached the barrier. 4

A 8 Ex 2 – Deadlocks n Coffman conditions ¨ Mutual exclusion in resource

A 8 Ex 2 – Deadlocks n Coffman conditions ¨ Mutual exclusion in resource usage ¨ Holding a resource and waiting ¨ No preemption of resources possible ¨ Circular wait n Solutions ¨ use protocol that ensures the system will never deadlock ¨ detect deadlock and recover ¨ ignore (. . . and reboot) 5

Deadlocks n Transactions in a bank ¨ move $ from x to y ¨

Deadlocks n Transactions in a bank ¨ move $ from x to y ¨ concurrent system ¨ acquire lock on both accounts, then perform transfer n 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? 6

Deadlocks: example Example: p 1: lock(x, y); p 2: lock(y, x); n Scenario: n

Deadlocks: example Example: p 1: lock(x, y); p 2: lock(y, x); n Scenario: n ¨ p 1 locks x ¨ p 2 locks y ¨ p 1 tries to lock y and waits ¨ p 2 tries to lock x and waits ¨ DEADLOCK 7

A 8 Ex 3 – Baboons n Problem ¨ more than one baboon can

A 8 Ex 3 – Baboons n 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. n n Part a): avoid deadlocks using semaphores. Part b): avoid starvation. 8