Download presentation
Presentation is loading. Please wait.
Published byBritney Bryant Modified over 6 years ago
1
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Dr. Frank Li
2
Review: where are we going with synchronization?
Hardware Higher-level API Programs Shared Programs Locks Semaphores Monitors Send/Receive Load/Store Disable Ints Test&Set Swap We are going to implement various higher-level synchronization primitives using atomic operations Everything is pretty painful if only atomic primitives are load/store Need to provide primitives useful at user-level
3
Review: Three Requirements for Critical-Section Problem
Mutual Exclusion If process Pi is executing in its critical section, then no other processes can be executing in their critical sections Progress If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted
4
Review: Three Classical Problems of Synchronization
Producer Consumer Buffer Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem For each problem, you need to know … The context of the problem The solution and discussion how it works? The purpose of each statement What would happen if we remove / shuffle certain statement What are the potential problems? Any remedy? R W These requirements are also valid for other schemes and algorithms introduced.
5
Goals for Today Monitors and condition variables Synchronization in Some OS Programming language support for synchronization
6
Monitors Monitor is a high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time programmer does NOT need to code this synchronization constraint explicitly. monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } }
7
Schematic view of a Monitor
8
Condition Variables The monitor construct is NOT sufficiently powerful for modeling some synchronization schemes Need to define additional synchronization mechanism: conditional construct A programmer can define one or more variables of type condition e.g. condition x, y; Two operations on a condition variable: x.wait () – a process that invokes the operation is suspended. x.signal () – resumes one of processes (if any) that invoked x.wait ()
9
Monitor with Condition Variables
10
Solution to Dining Philosophers Using Monitors
monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING)) { state[i] = EATING ; self[i].signal () ; } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; (Section are not required). Rewrite this code without test subroutine.
11
Synchronization in Some OS (1)
Solaris implements a variety of locks Uses adaptive mutex to protect short code segments On a single processor system, … On a multi processor system, … Uses semaphores and condition variables to protect longer code segments Uses readers-writers locks to protect data are accessed frequently but are usually in a read-only manner. Uses turnstiles is a queue structure One turnstile per thread, instead of per synchronized object a thread can be blocked only on one object at a time. Solaris turnstile implementation is efficient to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock (Reading assignment section 6.8)
12
Synchronization in Some OS (2)
Windows XP On single processor systems, uses interrupt masks to protect access to global resources On multiprocessor systems, uses spinlocks Provides dispatcher objects may act as either mutex and semaphore; may also provide events -- an event acts much like a condition variable (Reading assignment section 6.8)
13
Synchronization in Some OS (3)
Linux Prior to v2.6, Linux was an nonpreemptive kernel disables interrupts to implement short critical sections semaphores spin locks Reader-writer versions of semaphores and spin locks Pthreads Pthreads API is OS-independent Provides: mutex locks Condition variables Some non-portable extensions include: read-write locks (Reading assignment section 6.8)
14
Language support for synchronization
Java (Section 6.9 is not required.)
15
C-Language Support for Synchronization
C language: Pretty straightforward synchronization Just make sure you know all the code paths out of a critical section int Rtn() { lock.acquire(); … if (exception condition) { lock.release(); return 0; } … lock.release(); return 1; }
16
C++ Language Support for Synchronization (1)
In C++ support exceptions are problematic (easy to make a non-local exit without releasing lock) e.g. void Rtn() { lock.acquire(); … DoFoo(); … lock.release(); } void DoFoo() { … if (exception condition) throw errException; … } Notice that an exception in DoFoo() will exit without releasing the lock. How to fix this?
17
C++ Language Support for Synchronization (2)
Solution: must catch all exceptions in critical sections void Rtn() { lock.acquire(); try { … DoFoo(); … } catch (errException) { // catch exception lock.release(); // release lock } lock.release(); } void DoFoo() { … if (exception condition) throw errException; … }
18
Java Language Support for Synchronization (1)
Java has explicit support for threads and thread synchronization Bank Account example: class Account { private int balance; // object constructor public Account (int initialBalance) { balance = initialBalance; } public synchronized int getBalance() { return balance; } public synchronized void deposit(int amount) { balance += amount; } } Every object has an associated lock which gets automatically acquired and released on entry and exit from a synchronized method.
19
Java Language Support for Synchronization (2)
Java also has synchronized statements synchronized (object) { … } Since every Java object has an associated lock, this type of statement acquires and releases the object’s lock on entry and exit of the body Works properly even with exceptions: synchronized (object) { … DoFoo(); … } void DoFoo() { throw errException; }
20
Java Language Support for Synchronization (3)
In addition to a lock, every object has a single condition variable associated with it Wait inside a synchronization method or block: void wait(long timeout); // Wait for timeout void wait(long timeout, int nanoseconds); //variant void wait(); Signal in a synchronized method or block: void notify(); // wakes up oldest waiter void notifyAll(); // like broadcast, wakes everyone ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.