CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
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.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
CS444/CS544 Operating Systems Classic Synchronization Problems 2/28/2007 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/16/2007 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/28/2006 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.
ISP – 5 th Recitation Mutexes Code example. Mutex Wikipedia definition: Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 3/02/2006 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.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Jonathan Walpole Computer Science Portland State University
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS Introduction to Operating Systems
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
CS510 Concurrent Systems Introduction to Concurrency.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
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: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
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.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Web Server Architecture Client Main Thread for(j=0;j
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
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
Chapter 6: Process Synchronization
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Deadlock and Starvation
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Waiting and Synchronization
Synchronization and Semaphores
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Chapter 7: Synchronization Examples
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Synchronization Primitives – Semaphore and Mutex
CSE 153 Design of Operating Systems Winter 19
26.
Window Application Development
Monitors and Inter-Process Communication
Presentation transcript:

CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman

CS444/CS544 Spring 2007 Synchronization Primitives Implementations that block the waiting process/thread Semaphores Binary semaphores Counting semaphores Spinlock semaphores Reading assignment: Chapter 6

Recap: Implementing a semaphore with a lock struct semaphore_t { int value; queue waitingQueue; lock_t l; } void wait( semaphore_t *s){ lock(&s->l); s->value--; if (s->value < 0){ add self to s->waitingQueue unlock(&s->l); block } else { unlock(&s->l); } void signal( semaphore_t *s){ lock(&s->l); s->value++; if (s->value <=0) { P = remove process from s->waitingQueue unlock(&s->l); wakeup (P) } else { unlock(&s-l); }

Recap: Binary & Counting Semaphores Binary semaphore: value is either 0 or 1 Used to guarantee exclusive access to shared resource semaphore mutex = 1; down(mutex); /* critical section */; up(mutex); Counting semaphore: value is any int Useful to control access to a resource with N interchangeable units available; Allow threads to enter semaphore as long as sufficient resources are available semaphore Nsem = N; down(Nsem); /* use one unit */; up(Nsem);

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

Binary, Counting & Spinlock Semaphores “spinlock” semaphore: wait(s){ while (s->value <= 0); s.value--; } signal(s){ s->value++; } “binary” semaphore: only 0/1 wait(s) { if (s->value > 0) s->value--; else { add current to s->queue; block current; } } signal(s){ if (s->queue ≠ empty) { remove t from s->queue; wakeup(t); } else s->value++; } “counting” semaphore: any int wait(s) { s->value--; if (s->value < 0) { add current to s->queue; block current; } } signal(s){ s->value++; if (s->value <= 0) { remove t from s->queue; wakeup(t); } } typedef struct { int value; struct p* queue; } semaphore ; current = running proc/thread wait()/signal() MUST be indivisible!

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 Poor software design  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