Operating systems and concurrency (B08)

Similar documents
High-level Synchronization

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

W4118 Operating Systems. Instructor: Junfeng Yang

Synchronising Threads

Synchroniza+on II COMS W4118

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Paralleland Distributed Programming. Concurrency

Chapter 6: Process Synchronization

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Synchronization. Dr. Yingwu Zhu

Reminder from last time

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Introduction to parallel computing

Introduction to Operating Systems

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

More Shared Memory Programming

CSE 4/521 Introduction to Operating Systems

CMSC421: Principles of Operating Systems

Lecture 8: September 30

Operating systems and concurrency (B10)

Synchronization Principles

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

CS370 Operating Systems

Synchronization Primitives

Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Chapter 6 Process Synchronization

Process Synchronization(2)

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Real Time Operating Systems and Middleware

Multithreading Programming II

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

ANSI/IEEE POSIX Standard Thread management

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Operating Systems (2INC0) 2017/18

A Brief Introduction to OS/2 Multithreading

Chapter 7: Process Synchronization!

Chapter 6: Process Synchronization

Process Synchronization

Synchronization Principles II

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Process Synchronization(2)

Process Synchronization

Concurrency: a crash course

Concurrent Programming Lecture 10

CHAPTER 6: PROCESS SYNCHRONIZATION

Threads need to synchronize their activities to effectively interact. This includes:

Operating systems. Lecture 3 Interprocess communication. Narcis ILISEI, 2018

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Solving the Producer Consumer Problem with PThreads

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks

Process Synchronization(2)

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Lesson 6: Process Synchronization

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Chapter 6: Process Synchronization

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Condition Variables & Semaphores

Dealing with Issues for Interprocess Communication

Concurrency and Synchronisation

Concurrency and Synchronisation

The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of

What's wrong with Semaphores?

Process Synchronization

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Process Synchronization

Monitors (Deprecated)

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Semaphores (by Dijkstra)

IV. Process Synchronisation

Chapter 6: Process Synchronization

POSIX Threads. HUJI Spring 2011

1. Introduction. 2. Project Submission and Deliverables

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Chapter 7: Process Synchronization. Background

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Chapter 7: Process Synchronization. Background. Illustration

Module 6: Process Synchronization

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Process Synchronization

Transcription:

Operating systems and concurrency (B08) David Kendall Northumbria University David Kendall (Northumbria University) Operating systems and concurrency (B08) 1 / 20

Introduction Semaphores provide an unstructured synchronisation primitive Can lead to problems: Accidental release Deadlock Starvation... more on this next week Can be difficult to detect and debug Monitors and condition variables offer one approach to a more structured synchronisation mechanism Support widely available, e.g. POSIX threads (Pthreads), Java, C#, etc. David Kendall (Northumbria University) Operating systems and concurrency (B08) 2 / 20

Monitor 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 mutual exclusion monitor monitor name { / / shared v a r i a b l e d e c l a r a t i o n s procedure P1 (... ) {...... procedure Pn (... ) {... I n i t i a l i z a t i o n code (... ) {... Proposed independently by Per Brinch Hansen and Tony Hoare in 1973/74 David Kendall (Northumbria University) Operating systems and concurrency (B08) 3 / 20

Schematic view of a monitor Thread executing a monitor operation must hold the monitor mutex Other threads wanting to execute a monitor operation are queued on the mutex until the executing thread finishes and releases the mutex (from [SGG13, p.226]) David Kendall (Northumbria University) Operating systems and concurrency (B08) 4 / 20

Mutexes Mutex is short for for mutual exclusion A mutex variable is very similar to a semaphore without a counter Simpler to implement and use A mutex acts like a thread-safe lock; if multiple threads try to lock a mutex at the same time, only one will succeed and the other threads will be suspended on a queue associated with the mutex. Only the thread that successfully locked the mutex can unlock it. When that happens, one of the suspended threads will be made ready to run. If there are no threads waiting for the mutex, the unlock operation has no effect (how does this differ from a semaphore?) You should prefer the use of mutexes over semaphores for mutual exclusion David Kendall (Northumbria University) Operating systems and concurrency (B08) 5 / 20

Mutexes in Pthreads In Pthreads the mutex type is pthread_mutex_t and the main operations available for use on variable m of type pthread_mutex_t are: pthread_mutex_init(&m, NULL) initialises the mutex m; instead of NULL we can pass a pointer to a mutex attributes structure more on this later Can also initialise m statically, e.g. pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&m) used to lock the mutex; if the mutex is already locked, the calling thread will be suspended pthread_mutex_unlock(&m) used to unlock the mutex; if there are threads suspended waiting for the mutex, one of them will be made ready to run David Kendall (Northumbria University) Operating systems and concurrency (B08) 6 / 20

Condition variables So a monitor provides mutual exclusion Condition variables introduced to allow signalling Three operations allowed on condition variable, cv: wait(cv) block until another thread calls signal or broadcast on cv signal(cv) wake up one thread waiting on cv broadcast(cv) wake up all threads waiting on cv In Pthreads the CV type is pthread_cond_t Use pthread_cond_init() to initialise pthread_cond_wait(&cv, &mutex); pthread_cond_signal(&cv); pthread_cond_broadcast(&cv); (adapted from slides by Matt Welsh, Harvard University, 2009) David Kendall (Northumbria University) Operating systems and concurrency (B08) 7 / 20

Monitor with condition variables A thread that waits on a condition releases the monitor mutex When it is signalled, it must reacquire the mutex before continuing (from [SGG13, p.227]) David Kendall (Northumbria University) Operating systems and concurrency (B08) 8 / 20

Hoare vs Mesa Monitor Semantics The monitor signal() operation can have two different meanings: Hoare monitors (1974) signal(cv) means to run the waiting thread immediately Effectively hands the lock to the thread just signaled. Causes the signalling thread to block Mesa monitors (Lampson and Redell, Xerox PARC, 1980) signal(cv) puts waiting thread back onto the ready queue for the monitor But, signaling thread keeps running. Signaled thread doesn t get to run until it can acquire the lock. This is what we almost always use, eg Pthreads, Java, C#, etc. because it s much easier for the OS to implement efficiently. What s the practical difference? In Hoare-style semantics, the condition that triggered the signal() will always be true when the awoken thread runs For example, that the buffer is now no longer empty In Mesa-style semantics, awoken thread has to recheck the condition Since another thread might have beaten it to the punch (adapted from slides by Matt Welsh, Harvard University, 2009) David Kendall (Northumbria University) Operating systems and concurrency (B08) 9 / 20

Safe bounded buffer using a monitor s t a t i c pthread_mutex_t bufmutex ; s t a t i c pthread_cond_t f u l l S l o t ; s t a t i c pthread_cond_t f r e e S l o t ; s t a t i c u i n t 8 _ t n F u l l = 0; void s a f e B u f f e r I n i t ( void ) { pthread_mutex_init (& bufmutex, NULL ) ; pthread_cond_init (& f u l l S l o t, NULL ) ; pthread_cond_init (& f r e e S l o t, NULL ) ; bufmutex is the monitor mutex fullslot is the condition variable used to wait for a full slot freeslot is the condition variable used to wait for an free slot nfull used to keep track of the number of messages in the buffer See https://github.com/davidkendall/bb_with_monitor for the complete program. David Kendall (Northumbria University) Operating systems and concurrency (B08) 10 / 20

Safe bounded buffer using a monitor (ctd.) void safebufferput ( message_t const const msg) { pthread_mutex_lock (& bufmutex ) ; while ( n F u l l == BUF_SIZE ) { pthread_cond_wait (& f r e e S l o t, &bufmutex ) ; p u t B u f f e r (msg ) ; n F u l l += 1; pthread_cond_signal (& f u l l S l o t ) ; pthread_mutex_unlock (& bufmutex ) ; void safebufferget ( message_t const msg) { pthread_mutex_lock (& bufmutex ) ; while ( n F u l l == 0) { pthread_cond_wait (& f u l l S l o t, &bufmutex ) ; g e t B u f f e r (msg ) ; n F u l l = 1; pthread_cond_signal (& f r e e S l o t ) ; pthread_mutex_unlock (& bufmutex ) ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 11 / 20

Monitors concurrency good practice Organise all shared state into one or more monitors Each monitor should have a single monitor mutex Every public method (function, procedure) should acquire the monitor mutex at the very beginning and release it at the very end of the method Use condition variables to synchronise (wait and signal for the state to satisfy some condition) Notice that you have much more flexibility in expressing the condition to be waited for than when using a semaphore - you can use any boolean expression on the available state Condition variables do not have an associated counter, just a queue of waiting threads Always wait for a condition variable in a loop (assume Mesa semantics) David Kendall (Northumbria University) Operating systems and concurrency (B08) 12 / 20

Dining philosophers Another classic synchronisation problem Introduced here to illustrate how to build a monitor using Pthreads Philosophers think, get hungry, then eat,... repeatedly,... that s all To eat, a philosopher must have two chopsticks Spot the deadlock possibility... what about starvation?... much more on this next week See https://github.com/davidkendall/dp_with_monitor David Kendall (Northumbria University) Operating systems and concurrency (B08) 13 / 20

dpmonitor.c #include < a s s e r t. h> # include <sys / types. h> # include <pthread. h> #include < s t d i o. h> #include < s t d l i b. h> #include < s t r i n g. h> #include " dpmonitor. h " / Type d e c l a r a t i o n s / typedef enum { THINKING, HUNGRY, EATING s t a t e _ t ; / Local f u n c t i o n prototypes / s t a t i c void eatifok ( i n t i ) ; s t a t i c i n t l e f t N g h b r ( i n t i ) ; s t a t i c i n t rightnghbr ( i n t i ) ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 14 / 20

dpmonitor.c / Monitor v a r i a b l e s / s t a t i c pthread_mutex_t dpmutex ; s t a t i c pthread_cond_t oktoeat [ N_PHIL ] ; s t a t i c s t a t e _ t s t a t e [ N_PHIL ] ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 15 / 20

dpmonitor.c / Monitor f u n c t i o n d e f i n i t i o n s / void d p I n i t ( void ) { i n t rc ; / / I n i t i a l i s e the monitor mutex rc = pthread_mutex_init (&dpmutex, NULL ) ; assert ( rc == 0 ) ; / / I n i t i a l i s e the s t a t e and the c o n d i t i o n v a r i a b l e s for ( i n t i =0; i <N_PHIL ; i +=1) { s t a t e [ i ] = THINKING ; rc = p t h r e ad_cond_init (& oktoeat [ i ], NULL ) ; a s s e r t ( rc == 0 ) ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 16 / 20

dpmonitor.c void dppickup ( i n t i ) { pthread_ mutex_ lock (& dpmutex ) ; s t a t e [ i ] = HUNGRY; eatifok ( i ) ; while ( s t a t e [ i ]!= EATING) { pthread_cond_wait (& oktoeat [ i ], &dpmutex ) ; pthread_mutex_unlock (& dpmutex ) ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 17 / 20

dpmonitor.c void dpputdown ( i n t i ) { pthread_ mutex_ lock (& dpmutex ) ; s t a t e [ i ] = THINKING ; eatifok ( rightnghbr ( i ) ) ; eatifok ( l e f t N g h b r ( i ) ) ; pthread_mutex_unlock (& dpmutex ) ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 18 / 20

dpmonitor.c / Local f u n c t i o n d e f i n i t i o n s / s t a t i c void eatifok ( i n t i ) { i f ( ( s t a t e [ i ] == HUNGRY) && ( s t a t e [ rightnghbr ( i ) ]!= EATING) && ( s t a t e [ l e f t N g h b r ( i ) ]!= EATING ) ) { s t a t e [ i ] = EATING ; pthread_cond_signal (& oktoeat [ i ] ) ; s t a t i c i n t l e f t N g h b r ( i n t i ) { return ( ( i +(N_PHIL 1)) % N_PHIL ) ; s t a t i c i n t rightnghbr ( i n t i ) { return ( ( i +1) % N_PHIL ) ; David Kendall (Northumbria University) Operating systems and concurrency (B08) 19 / 20

Acknowledgements [SGG13] Silberschatz,A., Galvin, P., Gagne,G., Operating System Concepts (9th edition), Wiley, 2013 Lawrence Livermore Pthreads tutorial Welsh, M., Semaphores, Condition Variables and Monitors, Lecture slides, Harvard University, 2009 (local copy) David Kendall (Northumbria University) Operating systems and concurrency (B08) 20 / 20