CS444/CS544 Operating Systems Synchronization 2/28/2006 Prof. Searleman

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
Chapter 6: Process Synchronization
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
1 Semaphores and Monitors: High-level Synchronization Constructs.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/16/2007 Prof. Searleman
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Monitors CSCI 444/544 Operating Systems Fall 2008.
CS444/CS544 Operating Systems Deadlock 3/7/2007 Prof. Searleman
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
CS Introduction to Operating Systems
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS510 Concurrent Systems Introduction to Concurrency.
Tutorial 5 Even More Synchronization! presented by: Antonio Maiorano Paul Di Marco.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Chapter 71 Monitor for Reader/Writers  Synchronizes access if all processes follow.  If some don’t go through monitor and database is accessed directly,
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Deadlock and Starvation
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization
Deadlock and Starvation
CS510 Operating System Foundations
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Semaphore Originally called P() and V() wait (S) { while S <= 0
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
Chapter 6: Synchronization Tools
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Monitors and Inter-Process Communication
Presentation transcript:

CS444/CS544 Operating Systems Synchronization 2/28/2006 Prof. Searleman

Outline Synchronization NOTE: Return & discuss HW#5 Lab#2 posted, due Thurs, 3/9 Read: Chapter 7 Exam#1, Wednesday, March 1, 7:00 pm, SC162, SGG: Chapters 5 & 6 Exam#1

Semaphore’s Waiting Queue Recall: good to integrate semaphore’s waiting queue with scheduler When placed on waitingQueue should be removed from runningQueue Could use scheduling priority to decide who on queue enters semaphore when it is open next Beware of starvation just like in priority scheduling If OS exports semaphore, then kernel scheduler aware of waitingQueue If user-level thread package exports semaphore, then user-level thread scheduler (scheduling time on the available kernel threads) aware of waitingQueue

Is busy-waiting eliminated? Threads block on the queue associated with the semaphore instead of busy waiting Busy waiting is not gone completely When accessing the semaphore’s critical section, thread holds the semaphore’s lock and another process that tries to call wait or signal at the same time will busy wait Semaphore’s critical section is normally much smaller than the critical section it is protecting so busy waiting is greatly minimized Also avoid context switch overhead when just checking to see if can enter critical section and know all threads that are blocked on this object

Are spin locks always bad? Adaptive Locking in Solaris Adaptive mutexes Multiprocessor system if can’t get lock And thread with lock is not running, then sleep And thread with lock is running, spin wait Uniprocessor if can’t get lock Immediately sleep (no hope for lock to be released while you are running) Programmers choose adaptive mutexes for short code segments and semaphores or condition variables for longer ones Blocked threads placed on separate queue for desired object Thread to gain access next chosen by priority and priority inversion is implemented

Pthread’s Locks (Mutex)  Create/destroy int pthread_mutex_init (pthread_mutex_t *mut, const pthread_mutexattr_t *attr); int pthread_mutex_destroy (pthread_mutex_t *mut);  Lock int pthread_mutex_lock (pthread_mutex_t *mut);  Non-blocking Lock int pthread_mutex_trylock (pthread_mutex_t *mut);  Unlock int pthread_mutex_unlock (pthread_mutex_t *mut);

Semaphores  Not part of pthreads per se  #include  Support for use with pthreads varies (sometime if one thread blocks whole process does!)  Create/destroy int sem_init (sem_t *sem, int sharedBetweenProcesses, int initalValue); Int sem_destory(sem_t *sem)  Wait int sem_wait (sem_t *sem) int sem_trywait(sem_t * sem)  Signal int sem_post(sem_t *sem);  Get value int sem_getvalue(sem_t *, int * value);

Window’s Locks (Mutex)  Create/destroy HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpsa, // optional security attributes BOOL bInitialOwner // TRUE if creator wants ownership LPTSTR lpszMutexName ) // object’s name BOOL CloseHandle( hObject );  Lock DWORD WaitForSingleObject( HANDLE hObject, // object to wait for DWORD dwMilliseconds );  Unlock BOOL ReleaseMutex( HANDLE hMutex );

Window’s Locks (CriticalSection)  Create/Destroy VOID InitializeCriticalSection( LPCRITICAL_SECTION lpcs ); VOID DeleteCriticalSection( LPCRITICAL_SECTION lpcs );  Lock VOID EnterCriticalSection( LPCRITICAL_SECTION lpcs );  Unlock VOID LeaveCriticalSection( LPCRITICAL_SECTION lpcs );

Window’s Semaphores  Create HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpsa, // optional security attributes LONG lInitialCount, // initial count (usually 0) LONG lMaxCount, // maximum count (limits # of threads) LPTSTR lpszSemName ); // name of the semaphore (may be NULL) BOOL CloseHandle( hObject );  Lock DWORD WaitForSingleObject( HANDLE hObject, // object to wait for DWORD dwMilliseconds );  Unlock BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG lRelease, // amount to increment counter on release // (usually 1) LPLONG lplPrevious ); // variable to receive the previous count

Sharing Window’s Synchronization Objects Threads in the same process can share handle through a global variable Critical sections can only be used within the same process Much faster though Handles to mutexes and semaphores can be shared across processes One process creates another and the child inherits the handle (must specifically mark handle for inheritance) Unrelated processes can share through DuplicateHandle function or OpenMutex or OpenSemaphore (based on knowledge of the name – like a shared file name)

Windows 2000 Synchronization Uses interrupt masks to protect access to global resources on uniprocessor systems. Uses spinlocks on multiprocessor systems. Also provides dispatcher objects which may act as wither mutexes and semaphores. Dispatcher objects may also provide events. An event acts much like a condition variable.

Problems with Locks and Semaphores There is no syntactic connection between the semaphore ( or lock or event) and the shared data/resources it is protecting Thus the “meaning” of the semaphore is defined by the programmer’s use of it Bad software engineering  Semaphores basically global variables accessed by all threads Easy for programmers to make mistakes Also no separation between use for mutual exclusion and use for resource management and use for expressing ordering/scheduling constraints

Programming Language Support Add programming language support for synchronization Declare a section of code to require mutually exclusive access (like Java’s synchronized) Associate the shared data itself with the locking automatically Monitor = programming language support to enforce synchronization Mutual exclusion code added by the compiler!

Monitors A monitor is a software module that encapsulates: Shared data structures Procedures that operated on them Synchronization required of processes that invoke these procedures Like a public/private data interface prevents access to private data members; Monitors prevent unsynchronized access to shared data structures

Example: bankAccount Monitor bankAccount{ int balance; int readBalance( ){return balance}; void upateBalance(int newBalance){ balance = newBalance; } int withdraw (int amount) { balance = balance – amount; return balance; } int deposit (int amount){ balance = balance + amount; return balance; } Locking added by the compiler!

Monitor S balance readBalance updateBalance withdraw deposit Shared data Procedures Waiting queue One thread In Monitor

Waiting Inside a Monitor What if you need to wait for an event within one of the procedures of a monitor? Monitors as we have seen to this point enforce mutual exclusion – what about the Introduce another synchronization object, the condition variable Within the monitor declare a condition variable: condition x;

Wait and signal Condition variables, like semaphores, have the two operations have the two operations, wait and signal. The operation x.wait() means that the process invoking this operation is suspended until another process invokes x.signal(); The operation wait allows another process to enter the monitor (or no one could ever call signal!) The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect

Monitor With Condition Variables S balance readBalance updateBalance withdraw deposit Waiting queue One thread Running in Monitor Condition Variables and their associated wait queues

Semaphores vs Condition Variables I’d like to be able to say that condition variables are just like semaphores but … With condition variables, if no process is suspended then the signal operation has no effect With semaphores, signal increments the value regardless of whether any process is waiting Semaphores have “history” (they remember signals) while condition variables have no history

Condition Variable Alone? Could you use a condition variable concept outside of monitors? Yes, basically a semaphore without history Couldn’t do locking with it because no mutual exclusion on its own Couldn’t do resource management (counting semaphore) because no value/history Can use it for ordering/scheduling constraints (more on this later)

Monitor Invariants Can specify invariants that should hold whenever no thread is in the monitor Not checked by compiler More like a precondition to be respected by the programmer Right back into the problem with not having compiler support for mutual exclusion except in very simple cases!

Who first? If thread in Monitor calls x.signal waking another thread then who is running in the monitor now? (Can’t both be running in the monitor!) Hoare monitors Run awakened thread next; signaler blocks Mesa monitors Waiter is made ready; signaler continues

Does it matter? Yes If waiter runs immediately, then clearly “condition” being signaled still holds Signaler must restore any “monitor invariants” before signaling If waiter runs later, then when waiter finally enters monitor must recheck condition before executing Signaler need not restore any “monitor invariants” before signaling upon exiting

Write different code as a result If waiter runs immediately then if (condition not true) C.wait() If waiter runs later then while (condition not true) C.wait() Conclusion? Mesa style (waiter runs later) has fewer context switches and directly supports a broadcast primitive (I.e. c.signalAll) While instead of if not a big price to pay

Semaphores vs Monitors If have one you can implement the other…

Implementing Semaphores With Monitors Monitor semaphore { int value; conditionVariable_t waitQueue; void wait(){ value--; while (value < 0){ //Notice Mesa semantics condWait(&waitQueue); } void signal(){ value++; condSignal(&waitQueue) ; } } //end monitor semaphore void setValue(int value){ value = newValue; } int getValue(){ return value; }

Implementing Monitors with Semaphores semaphore_t mutex, next; int nextCount = 1; Initialization code: mutex.value = 1; next.value = 0; For each procedure P in Monitor, implement P as Wait (mutex); unsynchronizedBodyOfP(); if (nextCount >0){ signal(next); }else { signal(mutex); } conditionVariable_t { int count; semaphore_t sem; } condWait (conditionVariable_t *x) { //one more waiting on this cond x->count = x_count++; if (nextCount > 0){ signal(next); //wake up someone } else { signal (mutex); } wait(x->sem); x->count = x->count--; } condSignal(conditionVariable_t *x){ //if no one waiting do nothing! if (x->count > 0){ next_count = nextCount++; signal(x->sem); wait (next); nextCount--; } }

Revisit: Uses of Semaphores Mutual exclusion Binary semaphores (wait/signal used just like lock/unlock) “hold” Managing N copies of a resource Counting semaphores “enter” Anything else? Another type of synchronization is to express ordering/scheduling constraints “Don’t allow x to proceed until after y”

Semaphores for expressing ordering Initialize semaphore value to 0 Code: P i P j   Await signalB Execute B in P j only after A executed in P i Note: If signal executes first, wait will find it is an signaled state (history!)

Next time Classic synchronization problems and their solutions Bounded Buffer Readers/Writers Dining Philosophers