Lec6 - monitors & condition synchronization Monitors...

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

View Full Document Right Arrow Icon
1 Monitors Abhik Roychoudhury CS 3211 National University of Singapore Modified from Kramer and Magee’s lecture notes. Reading material: Chapter 5 of Textbook. 1 CS3211 2009-10 by Abhik monitors & condition synchronization Concepts : monitors: encapsulated data + access procedures mutual exclusion + condition synchronization single access procedure active in the monitor nested monitors Models :g u a r d e d a c t i o n s Practice : private data and synchronized methods (exclusion). wait(), notify() and notifyAll() for condition synch. single thread active in the monitor at a time 2 CS3211 2009-10 by Abhik The concept of monitors Brings in the concept of protected or private data. The protected data is accessed by several threads via operations Protected data cannot be accessed without invoking the operations. Each operation is executed atomically. A monitor thus represents a passive object, whose operations are invoked by 3 various active objects --- the threads. CS3211 2009-10 by Abhik A schematic monitor monitor X{ int n = 0; operation increment{ int tmp; tmp = n ; n = tmp+1; } n 4 } Process p Process q X.increment X.increment 0 Diagrammatic view of monitor X The critical section code is encapsulated inside monitor operations, not replicated inside processes. CS3211 2009-10 by Abhik Extending monitors with conditions Monitor operations may involve waiting on conditions (these are simple boolean expressions). When such conditions become true, the waiting threads are notified (using wait, notify feature of Java). Thus, each such condition has a waiting queue of blocked processes. The schematic for conditional wait / notify are: 5 wait_on_cond(Cond) append p, the current proc. to queue for Cond p.state = blocked monitor.lock = released signal_to_cond(Cond){ if queue for Cond != empty{ remove head of queue, let it be process x; x.state = ready } CS3211 2009-10 by Abhik Producer consumer problem ……………………. Finite buffer Consumer Producer 6 Producer: blocks if buffer is full. Consumer: blocks if buffer is empty. 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 Schematic Producer-Consumer monitor PC{ buffer = empty; condition notFull, notEmpty; operation produce(v){ operation consume(){ if buffer is full{ if buffer is empty{ wait_on_cond(notFull) wait_on_cond(notEmpty); } } add v to tail of buffer; remove w from head of buffer 7 remove w from head of buffer; signal_to_cond(notEmpty) signal_to_cond(notFull); } return w; } Producer Consumer while (1){ d = get_new_item; PC.produce(d); } while (1){ d = PC.consume(); put_item(d); } CS3211 2009-10 by Abhik Monitors in Java Not a default construct. Need to be programmed as a new class with private data (the data being protected) and synchronized methods. Blocking of processes is supported by wait() Unblocking of processes is supported by notify(), notifyAll() i () h i ill dd d h h 8 wait() can throw exceptions, so we will add code to catch them.
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.

Page1 / 9

Lec6 - monitors & condition synchronization Monitors...

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