CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ken Birman 1. Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J]
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Fundamentals of Python: From First Programs Through Data Structures
Nachos Phase 1 Code -Hints and Comments
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSE 425: Target Machine Architecture Target Machine Details Many architectures can be similar in overall structure –E.g., Von Neumann with CISC instruction.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
CSE 425: Concurrency II Semaphores and Mutexes Can avoid bad inter-leavings by acquiring locks –Guard access to a shared resource to take turns using it.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSE 425: Control Abstraction II Exception Handling Previous discussion focuses on normal control flow –Sometimes program reaches a point where it cannot.
Condition Variables Condition variables support three operations: Wait – add calling thread to the condition variable’s queue and put the thread to sleep.
June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
pThread synchronization
Introduction to Concurrency: Synchronization Mechanisms
Threaded Programming in Python
Names and Attributes Names are a key programming language feature
Background on the need for Synchronization
C++11 Threading Lieven de Cock
The Active Object Pattern
Transactional Memory Semaphores, monitors, and conditional critical regions all suffer from limitations based on lock semantics Naïve synchronization may.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Threading And Parallel Programming Constructs
Multithreading.
Iteration Implemented through loop constructs (e.g., in C++)
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Monitor Object Pattern
Parallelism and Concurrency
Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
CSE 542: Operating Systems
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement within an object (Monitor Object Pattern) –E.g., built with mutexes, condition variables (and threads) At most one thread may be active within a monitor –I.e., all other threads within the monitor must be suspended –Synchronization is based on a common shared lock –Active thread may (atomically) signal, release lock, suspend –Signaled thread then may (atomically) resume, acquire lock Monitors introduce conditional scopes for concurrency –E.g., a consumer suspends unless a queue is non-empty –E.g., producers suspend if queue reaches “high-water-mark”, until queue again reaches “low-water-mark” condition

CSE 425: Concurrency III Queue empty - wait notify Queue Desired Internal Behavior

CSE 425: Concurrency III “Monitor Object” Pattern Approach –Methods run in callers’ threads –Condition variables arbitrate use of a common shared lock E.g., using a std::mutex, a std::unique_lock (must be able to unlock and re-lock it) and a std::condition_variable –Ensures incremental progress while avoiding race conditions Threads wait on condition Condition variable performs thread-safe lock/wait and wake/unlock operations Thread released when it can proceed E.g., when queue isn’t empty/full –Blocks caller until request can be handled, coordinates callers Client Proxy List (Monitor Object) lookup() add() Condition Lock (Passive) Monitor Objects

CSE 425: Concurrency III Can notify_one() or notify_all() –If it doesn’t matter which thread, just wake one up –If all need to see if it’s their turn, wake all of them Can limit waiting time –Use wait_for() to return after a specified interval –Use wait_until() to return at a specified time Can pass a predicate to wait (or wait_* ) method(s) –Won’t return until predicate is satisfied (or call times out) –Helps to avoid spurious wake cases A Few Variations

CSE 425: Concurrency III Today’s Studio Exercises We’ll code up ideas from Scott Chapter 12.4 to –Again via C++11 concurrency/synchronization types/features –Looking at higher-level concurrency/synchronization ideas like condition variables being used to implement monitors Today’s exercises are again in C++ –Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site –The provided Makefile also may be helpful –As always, please ask us for help as needed When done send with your answers to the account with subject line “Concurrency Studio III”