Lec8 - Deadlock Concepts Deadlocks system deadlock no...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Deadlocks Abhik Roychoudhury CS 3211 National University of Singapore Modified from Kramer and Magee’s lecture notes. Reading material: Chapter 6 of Textbook. 1 CS3211 2009-10 by Abhik Deadlock Concepts : system deadlock : no further progress four necessary & sufficient conditions Models : deadlock - no eligible actions Practice :b l o c k e d t h r e a d s Aim : deadlock avoidance - to design systems where deadlock cannot occur. 2 CS3211 2009-10 by Abhik Deadlock: four necessary and sufficient conditions A. Serially reusable resources: the processes involved share resources which they use under mutual exclusion. B. Incremental acquisition: processes hold on to resources already allocated to them while waiting to acquire additional resources. C. No pre-emption: once acquired by a process, resources cannot be pre-empted (forcibly withdrawn) but are only released voluntarily. D. Wait-for cycle: a circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire. 3 CS3211 2009-10 by Abhik A. Serially re-usable resources public class Semaphore { private int value; public Semaphore (int initial) {value = initial;} synchronized public void up() { ++value; notify(); A monitor encapsulates resources which are accessed using mutual exclusion 4 } synchronized public void down() throws InterruptedException { while (value== 0) wait(); --value; } } CS3211 2009-10 by Abhik B. Incremental acquisition class SemaBuffer implements Buffer { Semaphore full; //counts number of items Nested Monitors --- Implement a bounded buffer as a monitor. Use semaphores ( another monitor ) to control access when buffer is full or empty. 5 Semaphore empty; //counts number of spaces SemaBuffer(int size) { this.size = size; buf = new Object[size]; full = new Semaphore( 0 ); empty= new Semaphore( size ); } } CS3211 2009-10 by Abhik Nested monitors – Incr. acquisition synchronized public void put(Object o) throws InterruptedException { empty.down(); buf[in] = o; ++count; in=(in+1)%size; full.up(); } synchronized public Object get() throws InterruptedException{ full down(); 6 full.down(); Object o =buf[out]; buf[out]=null; --count; out=(out+1)%size; empty.up(); return (o); } synchronized public void down() throws InterruptedException { while (value== 0) wait(); --value; } CS3211 2009-10 by Abhik
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 What was the deadlock scenario? Initially buffer does not contain anything, Integer protected by semaphore full is 0, And integer protected by semaphore empty is non-zero Consumer executes get() Inside get(), the first line is full.down() 7 Inside down, the first line is while (value == 0) wait() // value is the integer protected by the semaphore monitor Since is 0, wait() is executed Since wait() is encountered in a method for the semaphore – it releases the lock for The lock for the buffer whose get() called full.down() is not released!!
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

Page1 / 5

Lec8 - Deadlock Concepts Deadlocks system deadlock no...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online