Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8b Monitors.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements


Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Multi-core Programming Programming with Posix Threads.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition Chapter 5 Monitors & Condition Synchronization.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
1 Semaphores and Monitors: High-level Synchronization Constructs.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Concurrency Monitors and Condition Synchronization Claus Brabrand
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.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Multithreaded Web Server.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Atomic Operations David Monismith cs550 Operating Systems.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Programming with POSIX* Threads Intel Software College.
Internet Software Development Controlling Threads Paul J Krause.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
2015 Concurrency: logical properties 1 ©Magee/Kramer 2 nd Edition Chapter 14 Logical Properties Satisfied? Not satisfied?
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
pThread synchronization
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Background on the need for Synchronization
“Language Mechanism for Synchronization”
Principles of Operating Systems Lecture 11
Threads Threads.
Condition Synchronization
Operating Systems CMPSC 473
Thread synchronization
Threading And Parallel Programming Constructs
Concurrency: Mutual Exclusion and Process Synchronization
Thread Synchronization including Mutual Exclusion
Monitors and Inter-Process Communication
Presentation transcript:

Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8b Monitors and condition synchronisation Len Freeman, Graham Riley Centre for Novel Computing School of Computer Science University of Manchester

Concurrency: monitors & condition synchronization2 ©Magee/Kramer 2 nd Edition Chapter 5 Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization3 ©Magee/Kramer 2 nd Edition monitors & condition synchronization Concepts : monitors: encapsulated data + access procedures mutual exclusion + condition synchronization single access procedure active in the monitor Models :guarded actions Practice : ‘private’ data and exclusive access using locks. wait, notify and notifyAll model for condition synch. single thread active in the monitor at a time

Concurrency: monitors & condition synchronization4 ©Magee/Kramer 2 nd Edition 5.1 Condition synchronization A controller is required for a carpark, which only permits cars to enter when the carpark is not full and does not permit cars to leave when there are no cars in the carpark. Car arrival and departure are simulated by separate threads.

Concurrency: monitors & condition synchronization5 ©Magee/Kramer 2 nd Edition carpark model  Events or actions of interest? arrive and depart  Identify processes. arrivals, departures and carpark control  Define each process and interactions (structure). ARRIVALS CARPARK CONTROL DEPARTURES arrive depart CARPARK

Concurrency: monitors & condition synchronization6 ©Magee/Kramer 2 nd Edition carpark model CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->SPACES[i-1] |when(i SPACES[i+1] ). ARRIVALS = (arrive->ARRIVALS). DEPARTURES = (depart->DEPARTURES). ||CARPARK = (ARRIVALS||CARPARKCONTROL(4)||DEPARTURES). Guarded actions are used to control arrive and depart. LTS?

Concurrency: monitors & condition synchronization7 ©Magee/Kramer 2 nd Edition carpark program  Model - all entities are processes interacting by actions  Program - need to identify threads and monitors  thread - active entity which initiates (output) actions  monitor - passive entity which responds to (input) actions. For the carpark? ARRIVALS CARPARK CONTROL DEPARTURES arrive depart CARPARK

Concurrency: monitors & condition synchronization8 ©Magee/Kramer 2 nd Edition carpark program Arrivals and Departures implemented as (active) threads. CarParkControl provides the control (condition synchronization) implemented as a (passive) monitor (data + synchronized access functions). In code : // Thread creation (in main()): pthread_create( &thread1, NULL, &arrivals, NULL); pthread_create( &thread2, NULL, &departures, NULL);

Concurrency: monitors & condition synchronization9 ©Magee/Kramer 2 nd Edition carpark program - Arrivals and Departures threads void *arrivals() { for(;;) { arrive(); } How do we implement the control of CarParkControl ? Similarly departures which calls depart().

Concurrency: monitors & condition synchronization10 ©Magee/Kramer 2 nd Edition Carpark program - CarParkControl monitor condition synchronization? block if full? (spaces==0) block if empty? (spaces==CAPACITY) mutual exclusion? // The CarparkControl monitor data and functions #define CAPACITY 4 int spaces = CAPACITY; void *arrive(); void *depart(); void *arrive() { … --spaces; … } void *depart() { … ++spaces; … }

Concurrency: monitors & condition synchronization11 ©Magee/Kramer 2 nd Edition condition synchronization in C and pthreads pthreads provides the following condition variable functions: (Remember, a condition variable is used with an associated mutex lock) notify: pthread_cond_signal(&cond_var) Wakes up a single thread that is waiting on a condition variable. notifyAll: pthread_cond_broadcast(&cond_var) Wakes up all threads that are waiting on a condition variable. Wait: pthread_cond_wait(&cond_var, &cond_mutex) Waits to be notified by another thread. The waiting thread releases the synchronization lock associated with the cond var (i.e. the ‘monitor lock’). When notified, the thread must wait to reacquire the monitor lock before resuming execution.

Concurrency: monitors & condition synchronization12 ©Magee/Kramer 2 nd Edition condition synchronization in pthreads We refer to a thread entering a monitor when it acquires the mutual exclusion lock associated with the monitor and exiting the monitor when it releases the lock. wait - causes the thread to exit the monitor, permitting other threads to enter the monitor. Thread AThread B wait notify Monitor data

Concurrency: monitors & condition synchronization13 ©Magee/Kramer 2 nd Edition monitor lock (here a queue) wait Monitor data waiting Thread C Thread E Thread B Thread F Thread A notify (then release lock) Thread B Thread F Thread E Thread A Thread C Thread A

Concurrency: monitors & condition synchronization14 ©Magee/Kramer 2 nd Edition condition synchronization in C/pthreads FSP: when cond act -> NEWSTAT C/pthreads: void *act() { pthread_mutex_lock(); while (!cond) pthread_cond_wait(); // modify monitor data pthread_cond_broadcast()//notifyall pthread_mutex_unlock(); } The while loop is necessary to retest the condition cond to ensure that cond is indeed satisfied when it re-enters the monitor. notifyall is necessary to awaken other threads that may be waiting to enter the monitor now that the monitor data has been changed.

Concurrency: monitors & condition synchronization15 ©Magee/Kramer 2 nd Edition CarParkControl - condition synchronization – arrive() void *arrive() { pthread_mutex_lock( &condition_mutex ); while( spaces == 0 ) { pthread_cond_wait( &condition_cond, &condition_mutex ); printf("spaces value in arrival on waking: %d\n",spaces); } --spaces; printf("spaces after an arrival: %d\n",spaces); pthread_cond_signal( &condition_cond ); pthread_mutex_unlock( &condition_mutex ); } Is it safe to use notify (i.e. signal) here rather than notifyAll (broadcast) ?

Concurrency: monitors & condition synchronization16 ©Magee/Kramer 2 nd Edition CarParkControl - condition synchronization – depart() void *depart() { pthread_mutex_lock( &condition_mutex ); while ( spaces == CAPACITY ) { pthread_cond_wait( &condition_cond, &condition_mutex ); printf("spaces value in depart on waking: %d\n",spaces); } ++spaces; printf("spaces after a departure: %d\n",spaces); pthread_cond_signal( &condition_cond ); pthread_mutex_unlock( &condition_mutex ); } Is it safe to use notify here rather than notifyAll ?

Concurrency: monitors & condition synchronization17 ©Magee/Kramer 2 nd Edition models to monitors - summary Each guarded action in the model of a monitor is implemented as a condition variable using a while loop and wait to implement the guard. The while loop condition is the negation of the model guard condition. Active entities (that initiate actions) are implemented as threads. Passive entities (that respond to actions) are implemented as monitors. Changes in the state of the monitor are signaled to waiting threads using signal() or broadcast().

Concurrency: monitors & condition synchronization18 ©Magee/Kramer 2 nd Edition 5.2 Semaphores Semaphores are widely used for dealing with inter-process synchronization in operating systems. Semaphore s is an integer variable that can take only non-negative values. down(s): if s >0 then decrement s else block execution of the calling process up(s): if processes blocked on s then awaken one of them else increment s The only operations permitted on s are up(s) and down(s). Blocked processes are held in a FIFO queue. (think of s as a count of ‘free’ resources)

Concurrency: monitors & condition synchronization19 ©Magee/Kramer 2 nd Edition modeling semaphores const Max = 3 range Int = 0..Max SEMAPHORE(N=0) = SEMA[N], SEMA[v:Int] = (up->SEMA[v+1] |when(v>0) down->SEMA[v-1] ), SEMA[Max+1] = ERROR. To ensure analyzability, we only model semaphores that take a finite range of values. If this range is exceeded then we regard this as an ERROR. N is the initial value. LTS?

Concurrency: monitors & condition synchronization20 ©Magee/Kramer 2 nd Edition modeling semaphores Action down is only accepted when value v of the semaphore is greater than 0. Action up is not guarded. Trace to a violation: up  up  up  up

Concurrency: monitors & condition synchronization21 ©Magee/Kramer 2 nd Edition semaphore demo - model LOOP = (mutex.down->critical->mutex.up->LOOP). ||SEMADEMO = (p[1..3]:LOOP ||{p[1..3]}::mutex:SEMAPHORE(1)). Three processes p[1..3] use a shared semaphore mutex to ensure mutually exclusive access (action critical ) to some resource. For mutual exclusion, the semaphore initial value is 1. Why? Is the ERROR state reachable for SEMADEMO ? Is a binary semaphore sufficient (i.e. Max=1 ) ? LTS?

Concurrency: monitors & condition synchronization22 ©Magee/Kramer 2 nd Edition semaphore demo - model

Concurrency: monitors & condition synchronization23 ©Magee/Kramer 2 nd Edition Summary  Concepts monitors: encapsulated data + access procedures mutual exclusion + condition synchronization  Model guarded actions  Practice ‘private’ data and exclusive access using locks wait, notify and notifyAll for condition synchronization single thread active in the monitor at a time