08.Semaphore-Monitors

08.Semaphore-Monitors - Semaphores and Monitors: High-level...

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

View Full Document Right Arrow Icon
1 Semaphores and Monitors: High-level Synchronization Constructs
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 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks: provide mutual exclusion Condition variables: provide conditional synchronization Today: Historical perspective Monitors Alternate high-level language constructs Semaphores Introduced by Dijkstra in 1960s Main synchronization primitives in early operating systems
Background image of page 2
3 Separate the concerns of mutual exclusion and conditional synchronization What is a monitor? One lock, and Zero or more condition variables for managing concurrent access to shared data General approach: Collect related shared data into an object/module Define methods for accessing the shared data Monitors first introduced as programming language construct Calling a method defined in the monitor automatically acquires the lock Examples: Mesa, Java (synchronized methods) Monitors also define a programming convention Can be used in any language (C, C++, … ) Introducing Monitors
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Critical Section: Monitors Basic idea: Restrict programming model Permit access to shared variables only within a critical section General program structure Entry section “Lock” before entering critical section Wait if already locked Key point: synchronization may involve wait Critical section code Exit section “Unlock” when leaving the critical section Object-oriented programming style Associate a lock with each shared object Methods that access shared object are critical sections Acquire/release locks when entering/exiting a method that defines a critical section
Background image of page 4
5 Remember Condition Variables Locks Provide mutual exclusion Support two methods Lock::Acquire() – wait until lock is free, then grab it Lock::Release() – release the lock, waking up a waiter, if any Condition variables Support conditional synchronization Three operations Wait(): Release lock; wait for the condition to become true; reacquire lock upon return (Java wait()) Signal(): Wake up a waiter, if any (Java notify()) Broadcast(): Wake up all the waiters (Java notifyAll()) Two semantics for implementation of wait() and signal() Hoare monitor semantics Hansen (Mesa) monitor semantics
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Coke Machine – Example Monitor Class CokeMachine{     …     Lock lock;     int count = 0;     Condition notFull, notEmpty; } Class CokeMachine{     …     Lock lock;     int count = 0;     Condition notFull, notEmpty; } CokeMachine::Deposit(){     lock acquire();     while (count == n) { notFull.wait(&lock); }     Add coke to the machine;     count++;     notEmpty.signal();     lock release(); } CokeMachine::Deposit(){     lock acquire();      while (count == n) { notFull.wait(&lock); }     Add coke to the machine;
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 03/09/2012.

Page1 / 24

08.Semaphore-Monitors - Semaphores and Monitors: High-level...

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

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