Overview Assignment 8: hints Assignment 7: solution Deadlocks See sample solution on the web
A8 Ex1 – Barrier Objects synchronize processes “checkpoint”: continue only when all processes are there checkpoint checkpoint checkpoint Barrier = OBJECT Init(count); Sync; PROCESS: FOR i := 0 TO Max DO DoPhase(i); barrier.Sync END
Barrier, Example P1 P2 P3 // code before the barrier Barrier.synchronize() // code after the barrier
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.
A8 Ex2 – Deadlocks Coffman conditions Solutions 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)
Deadlocks Transactions in a bank Deadlock avoidance: 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?
Deadlocks: example Example: p1: lock(x, y); p2: lock(y, x); Scenario: p1 locks x p2 locks y p1 tries to lock y and waits p2 tries to lock x and waits DEADLOCK
A8 Ex3 – Baboons Problem Part a): avoid deadlocks using semaphores. 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.