Operating systems and concurrency (B08)

Size: px
Start display at page:

Download "Operating systems and concurrency (B08)"

Transcription

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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 for the complete program. David Kendall (Northumbria University) Operating systems and concurrency (B08) 10 / 20

11 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

12 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

13 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 David Kendall (Northumbria University) Operating systems and concurrency (B08) 13 / 20

14 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

15 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

16 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

17 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

18 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

19 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

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

High-level Synchronization

High-level Synchronization Recap of Last Class High-level Synchronization CS 256/456 Dept. of Computer Science, University of Rochester Concurrent access to shared data may result in data inconsistency race condition. The Critical-Section

More information

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

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Semaphore Semaphore S integer variable Two standard operations modify S: wait() and signal() Originally called P() and V() Can only be accessed via two indivisible (atomic) operations wait (S) { while

More information

W4118 Operating Systems. Instructor: Junfeng Yang

W4118 Operating Systems. Instructor: Junfeng Yang W4118 Operating Systems Instructor: Junfeng Yang Outline Semaphores Producer-consumer problem Monitors and condition variables 2 Semaphore motivation Problem with lock: mutual exclusion, but no ordering

More information

Synchronising Threads

Synchronising Threads Synchronising Threads David Chisnall March 1, 2011 First Rule for Maintainable Concurrent Code No data may be both mutable and aliased Harder Problems Data is shared and mutable Access to it must be protected

More information

Synchroniza+on II COMS W4118

Synchroniza+on II COMS W4118 Synchroniza+on II COMS W4118 References: Opera+ng Systems Concepts (9e), Linux Kernel Development, previous W4118s Copyright no2ce: care has been taken to use only those web images deemed by the instructor

More information

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

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

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

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018 CIS 3207 - Operating Systems Application of Semaphores Professor Qiang Zeng Spring 2018 Big picture of synchronization primitives Busy-waiting Software solutions (Dekker, Bakery, etc.) Hardware-assisted

More information

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Condition Variables Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu)

More information

Paralleland Distributed Programming. Concurrency

Paralleland Distributed Programming. Concurrency Paralleland Distributed Programming Concurrency Concurrency problems race condition synchronization hardware (eg matrix PCs) software (barrier, critical section, atomic operations) mutual exclusion critical

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018 Deadlock and Monitors CS439: Principles of Computer Systems February 7, 2018 Last Time Terminology Safety and liveness Atomic Instructions, Synchronization, Mutual Exclusion, Critical Sections Synchronization

More information

Synchronization. Dr. Yingwu Zhu

Synchronization. Dr. Yingwu Zhu Synchronization Dr. Yingwu Zhu Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Threads accessing a memory cache in a Web server To coordinate

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 3: CCR, monitors, and concurrency in practice DrRobert N. M. Watson 1 Reminder from last time Implementing mutual exclusion: hardware support for atomicity and inter-processor

More information

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Operating Systems. Thread Synchronization Primitives. Thomas Ropars. 1 Operating Systems Thread Synchronization Primitives Thomas Ropars thomas.ropars@univ-grenoble-alpes.fr 2017 2 Agenda Week 42/43: Synchronization primitives Week 44: Vacation Week 45: Synchronization

More information

Introduction to parallel computing

Introduction to parallel computing Introduction to parallel computing Shared Memory Programming with Pthreads (3) Zhiao Shi (modifications by Will French) Advanced Computing Center for Education & Research Vanderbilt University Last time

More information

Introduction to Operating Systems

Introduction to Operating Systems Introduction to Operating Systems Lecture 4: Process Synchronization MING GAO SE@ecnu (for course related communications) mgao@sei.ecnu.edu.cn Mar. 18, 2015 Outline 1 The synchronization problem 2 A roadmap

More information

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

More Shared Memory Programming

More Shared Memory Programming More Shared Memory Programming Shared data structures We want to make data structures that can be shared by threads. For example, our program to copy a file from one disk to another used a shared FIFO

More information

CSE 4/521 Introduction to Operating Systems

CSE 4/521 Introduction to Operating Systems CSE 4/521 Introduction to Operating Systems Lecture 7 Process Synchronization II (Classic Problems of Synchronization, Synchronization Examples) Summer 2018 Overview Objective: 1. To examine several classical

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

More information

Operating systems and concurrency (B10)

Operating systems and concurrency (B10) Operating systems and concurrency (B10) David Kendall Northumbria University David Kendall (Northumbria University) Operating systems and concurrency (B10) 1 / 26 Introduction This lecture looks at Some

More information

Synchronization Principles

Synchronization Principles Synchronization Principles Gordon College Stephen Brinton The Problem with Concurrency Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms

More information

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

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives. CS 5523 Operating Systems: Concurrency and Synchronization Thank Dr. Dakai Zhu and Dr. Palden Lama for providing their slides. Lecture Outline Problems with concurrent access to shared data Ø Race condition

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 12 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ 2 Mutex vs Semaphore Mutex is binary,

More information

Synchronization Primitives

Synchronization Primitives Synchronization Primitives Locks Synchronization Mechanisms Very primitive constructs with minimal semantics Semaphores A generalization of locks Easy to understand, hard to program with Condition Variables

More information

Process Synchronization

Process Synchronization Process Synchronization Part III, Modified by M.Rebaudengo - 2013 Silberschatz, Galvin and Gagne 2009 POSIX Synchronization POSIX.1b standard was adopted in 1993 Pthreads API is OS-independent It provides:

More information

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization Chapter 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

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

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift CS 537 Lecture 8 Monitors Michael Swift Two Classes of Synchronization Problems Uniform resource usage with simple scheduling constraints No other variables needed to express relationships Use one semaphore

More information

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

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution Race Condition Synchronization CSCI 315 Operating Systems Design Department of Computer Science A race occurs when the correctness of a program depends on one thread reaching point x in its control flow

More information

Chapter 6 Process Synchronization

Chapter 6 Process Synchronization Chapter 6 Process Synchronization Cooperating Process process that can affect or be affected by other processes directly share a logical address space (threads) be allowed to share data via files or messages

More information

Process Synchronization(2)

Process Synchronization(2) CSE 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Computer Science and Engineering York University Semaphores Problems with the software solutions. Not easy

More information

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

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science High Performance Computing Lecture 21 Matthew Jacob Indian Institute of Science Semaphore Examples Semaphores can do more than mutex locks Example: Consider our concurrent program where process P1 reads

More information

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1 Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary

More information

Real Time Operating Systems and Middleware

Real Time Operating Systems and Middleware Real Time Operating Systems and Middleware POSIX Threads Synchronization Luca Abeni abeni@dit.unitn.it Real Time Operating Systems and Middleware p. 1 Threads Synchronisation All the threads running in

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

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

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger Operating Systems CMPSC 473 Synchronization February 26, 2008 - Lecture 12 Instructor: Trent Jaeger Last class: Synchronization Problems and Primitives Today: Synchonization Solutions Midterm (Both Sections)

More information

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

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks Synchronization II Today Condition Variables Semaphores Monitors and some classical problems Next time Deadlocks Condition variables Many times a thread wants to check whether a condition is true before

More information

ANSI/IEEE POSIX Standard Thread management

ANSI/IEEE POSIX Standard Thread management Pthread Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The

More information

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang Lecture 9: Thread Synchronizations Spring 2016 Jason Tang Slides based upon Operating System Concept slides, http://codex.cs.yale.edu/avi/os-book/os9/slide-dir/index.html Copyright Silberschatz, Galvin,

More information

Operating Systems (2INC0) 2017/18

Operating Systems (2INC0) 2017/18 Operating Systems (2INC0) 2017/18 Condition Synchronization (07) Dr. Courtesy of Prof. Dr. Johan Lukkien System Architecture and Networking Group Agenda Condition synchronization motivation condition variables

More information

A Brief Introduction to OS/2 Multithreading

A Brief Introduction to OS/2 Multithreading A Brief Introduction to OS/2 Multithreading Jaroslav Kaƒer jkacer@kiv.zcu.cz University of West Bohemia Faculty of Applied Sciences Department of Computer Science and Engineering Why Use Parallelism? Performance

More information

Chapter 7: Process Synchronization!

Chapter 7: Process Synchronization! Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors 7.1 Background Concurrent access to shared

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Objectives Introduce Concept of Critical-Section Problem Hardware and Software Solutions of Critical-Section Problem Concept of Atomic Transaction Operating Systems CS

More information

Process Synchronization

Process Synchronization TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Process Synchronization [SGG7] Chapter 6 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin

More information

Synchronization Principles II

Synchronization Principles II CSC 256/456: Operating Systems Synchronization Principles II John Criswell University of Rochester 1 Synchronization Issues Race conditions and the need for synchronization Critical Section Problem Mutual

More information

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Fall 2012 References Some of the slides in this lecture is based on the following references: http://www.cobweb.ecn.purdue.edu/~eigenman/ece563/h

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

Process Synchronization

Process Synchronization Process Synchronization Chapter 6 2015 Prof. Amr El-Kadi Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly

More information

Concurrency: a crash course

Concurrency: a crash course Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Concurrency: a crash course Concurrent computing Applications designed as a collection of computational units that may execute

More information

Concurrent Programming Lecture 10

Concurrent Programming Lecture 10 Concurrent Programming Lecture 10 25th September 2003 Monitors & P/V Notion of a process being not runnable : implicit in much of what we have said about P/V and monitors is the notion that a process may

More information

CHAPTER 6: PROCESS SYNCHRONIZATION

CHAPTER 6: PROCESS SYNCHRONIZATION CHAPTER 6: PROCESS SYNCHRONIZATION The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. TOPICS Background

More information

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

Threads need to synchronize their activities to effectively interact. This includes: KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 8 Threads Synchronization ( Mutex & Condition Variables ) Objective: When multiple

More information

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

Operating systems. Lecture 3 Interprocess communication. Narcis ILISEI, 2018 Operating systems Lecture 3 Interprocess communication Narcis ILISEI, 2018 Goals for today Race Condition Critical Region Mutex / Monitor Semaphore Inter-process communication (IPC) A mechanism that allows

More information

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

Chapter 6: Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Synchronization, Silberschatz, Galvin and Gagne 2009 Outline Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Solving the Producer Consumer Problem with PThreads

Solving the Producer Consumer Problem with PThreads Solving the Producer Consumer Problem with PThreads Michael Jantz Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads: Producer-Consumer 1 Introduction This lab is an extension of last week's lab.

More information

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

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks Synchronization II q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks Condition variables Locks are not enough to build concurrent programs Many times a thread

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

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

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004 Lecture 8: Semaphores, Monitors, & Condition Variables 8.0 Main Points: Definition of semaphores Example of use

More information

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

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on Chapter 6: Process Synchronization Operating System Concepts 9 th Edit9on Silberschatz, Galvin and Gagne 2013 Objectives To present the concept of process synchronization. To introduce the critical-section

More information

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2. Learning Outcomes Concurrency and Synchronisation Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

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

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Race Shared Data: 465 1 8 5 6 209? tail A[] thread switch Enqueue(): A[tail] = 20; tail++; A[tail] = 9; tail++; Thread 0 Thread

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

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

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018 CS 31: Intro to Systems Misc. Threading Kevin Webb Swarthmore College December 6, 2018 Classic thread patterns Agenda Pthreads primitives and examples of other forms of synchronization: Condition variables

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

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

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012 Pre-lab #2 tutorial ECE 254 Operating Systems and Systems Programming May 24, 2012 Content Concurrency Concurrent Programming Thread vs. Process POSIX Threads Synchronization and Critical Sections Mutexes

More information

Condition Variables & Semaphores

Condition Variables & Semaphores Condition Variables & Semaphores Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau) Review: Concurrency Objectives Mutual Exclusion A & B don t run at the same time Solved using locks Ordering

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

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

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 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 World War I (pictured is a Sopwith Camel). Due to a number

More information

What's wrong with Semaphores?

What's wrong with Semaphores? Next: Monitors and Condition Variables What is wrong with semaphores? Monitors What are they? How do we implement monitors? Two types of monitors: Mesa and Hoare Compare semaphore and monitors Lecture

More information

Process Synchronization

Process Synchronization CSC 4103 - Operating Systems Spring 2007 Lecture - VI Process Synchronization Tevfik Koşar Louisiana State University February 6 th, 2007 1 Roadmap Process Synchronization The Critical-Section Problem

More information

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. 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 Synchronization tool (provided by the OS) that do not require busy waiting A semaphore S is an integer variable that, apart from initialization, can only be accessed through 2 atomic and mutually

More information

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations Semaphores Synchronization tool (provided by the OS) that do not require busy waiting A semaphore S is an integer variable that, apart from initialization, can only be accessed through 2 atomic and mutually

More information

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

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology Process Synchronization: Semaphores CSSE 332 Operating Systems Rose-Hulman Institute of Technology Critical-section problem solution 1. Mutual Exclusion - If process Pi is executing in its critical section,

More information

Process Synchronization

Process Synchronization Chapter 7 Process Synchronization 1 Chapter s Content Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors 2 Background

More information

Monitors (Deprecated)

Monitors (Deprecated) D Monitors (Deprecated) Around the time concurrent programming was becoming a big deal, objectoriented programming was also gaining ground. Not surprisingly, people started to think about ways to merge

More information

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

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010 CSC 4304 - Systems Programming Fall 2010 Lecture - XIV Concurrent Programming Tevfik Ko!ar Louisiana State University November 2nd, 2010 1 Concurrency Issues 2 Concurrency Issues 3 Synchronization Mechanism

More information

Semaphores (by Dijkstra)

Semaphores (by Dijkstra) CSCI 4401 Principles of Operating Systems I Process Synchronization II: Classic Problems Vassil Roussev vassil@cs.uno.edu Semaphores (by Dijkstra) A higher-level way of doing synchronization between threads/processes

More information

IV. Process Synchronisation

IV. Process Synchronisation IV. Process Synchronisation Operating Systems Stefan Klinger Database & Information Systems Group University of Konstanz Summer Term 2009 Background Multiprogramming Multiple processes are executed asynchronously.

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Module 6: Process Synchronization Chapter 6: Process Synchronization Background! The Critical-Section Problem! Peterson s Solution! Synchronization Hardware! Semaphores! Classic Problems of Synchronization!

More information

POSIX Threads. HUJI Spring 2011

POSIX Threads. HUJI Spring 2011 POSIX Threads HUJI Spring 2011 Why Threads The primary motivation for using threads is to realize potential program performance gains and structuring. Overlapping CPU work with I/O. Priority/real-time

More information

1. Introduction. 2. Project Submission and Deliverables

1. Introduction. 2. Project Submission and Deliverables Spring 2018 Programming Project 2 Due 11:59 PM, Wednesday, 3/21/18 (3/21 is the Wednesday after Spring Break, so if you don t want to work on the program over break or save all the work for those last

More information

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

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2 Plan Demos Project: Due» Demos following week (Wednesday during class time) Next Week» New phase of presentations» Deadlock, finish synchronization Course Progress:» History, Structure, Processes, Threads,

More information

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

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems Synchronization CS 475, Spring 2018 Concurrent & Distributed Systems Review: Threads: Memory View code heap data files code heap data files stack stack stack stack m1 m1 a1 b1 m2 m2 a2 b2 m3 m3 a3 m4 m4

More information

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

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data Lecture 4 Monitors Summary Semaphores Good news Simple, efficient, expressive Passing the Baton any await statement Bad news Low level, unstructured omit a V: deadlock omit a P: failure of mutex Synchronisation

More information

Chapter 7: Process Synchronization. Background

Chapter 7: Process Synchronization. Background Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris

More information

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T Operating Systems Operating Systems Summer 2017 Sina Meraji U of T More Special Instructions Swap (or Exchange) instruction Operates on two words atomically Can also be used to solve critical section problem

More information

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

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University Frequently asked questions from the previous class survey CS 370: SYSTEM ARCHITECTURE & SOFTWARE [PROCESS SYNCHRONIZATION] Shrideep Pallickara Computer Science Colorado State University Semaphores From

More information

Chapter 7: Process Synchronization. Background. Illustration

Chapter 7: Process Synchronization. Background. Illustration Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris

More information

Module 6: Process Synchronization

Module 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic

More information

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions CS 470 Spring 2018 Mike Lam, Professor Semaphores and Conditions Synchronization mechanisms Busy-waiting (wasteful!) Atomic instructions (e.g., LOCK prefix in x86) Pthreads Mutex: simple mutual exclusion

More information

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Motivation and definitions Processes Threads Synchronization constructs Speedup issues Motivation and definitions Processes Threads Synchronization constructs Speedup issues Overhead Caches Amdahl s Law CS550: Advanced Operating Systems 2 If task can be completely decoupled into independent

More information

Process Synchronization

Process Synchronization CS307 Process Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2018 Background Concurrent access to shared data may result in data inconsistency

More information