Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Similar documents
Note: in this document we use process and thread interchangeably.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Models of concurrency & synchronization algorithms

Threads and Parallelism in Java

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

What is a thread anyway?

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Last Class: Deadlocks. Today

What's wrong with Semaphores?

Reminder from last time

Multiple Inheritance. Computer object can be viewed as

Monitors; Software Transactional Memory

IT 540 Operating Systems ECE519 Advanced Operating Systems

CMSC 330: Organization of Programming Languages

OS06: Monitors in Java

Monitors; Software Transactional Memory

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Concurrent Programming Lecture 10

Lecture 8: September 30

Lecture 9: Midterm Review

Performance Throughput Utilization of system resources

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

MCS-378 Intraterm Exam 1 Serial #:

CS 450 Exam 2 Mon. 4/11/2016

Virtual Machine Design

Dealing with Issues for Interprocess Communication

Exercises and Labs. Part I. Exercises

Java Threads. COMP 585 Noteset #2 1

Today: Synchronization. Recap: Synchronization

COMP 346 WINTER Tutorial 5 MONITORS

Chapter 32 Multithreading and Parallel Programming

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

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

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

CPS 506 Comparative Programming Languages. Programming Language Paradigms

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

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

CS Operating Systems

CS Operating Systems

CHAPTER 6: PROCESS SYNCHRONIZATION

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Synchronization SPL/2010 SPL/20 1

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

CMSC 330: Organization of Programming Languages

Resource management. Real-Time Systems. Resource management. Resource management

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

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

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Concurrency: a crash course

Concurrent Programming using Threads

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Real-Time and Concurrent Programming Lecture 1 (F1): Embedded concurrent software

1 Process Coordination

Writing Parallel Programs COMP360

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

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

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Operating Systems (1DT020 & 1TT802)

Operating Systems ECE344

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Concurrent Programming

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

CSE 153 Design of Operating Systems

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Synchronization I. Jo, Heeseung

Administrivia. Assignments 0 & 1 Class contact for the next two weeks Next week. Following week

Concurrency and Synchronisation

Concurrency and Synchronisation

Synchroniza+on II COMS W4118

Synchronization

CSE 153 Design of Operating Systems

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

EE458 - Embedded Systems Lecture 8 Semaphores

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

High Performance Computing Course Notes Shared Memory Parallel Programming

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

Threads Questions Important Questions

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

CSE 374 Programming Concepts & Tools

Lecture 9: Introduction to Monitors

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Synchronization Lecture 24 Fall 2018

CS533 Concepts of Operating Systems. Jonathan Walpole

Introduction to OS Synchronization MOS 2.3

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

Java Threads. Introduction to Java Threads

Transcription:

http://cs.lth.se/eda040 Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify Klas Nilsson 2016-09-20 http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 1 / 1

http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 2 / 1

Objects with mutual exclusion Monitors and synchronized objects Upcoming Monitor content Language construct for synchronization of threads More practical than semaphores Fits in well with object orientation Supported by synchronized in Java Signaling supported inside locked objects http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 3 / 1

Objects with mutual exclusion Mutual exclusion as part of interface In-line use of semaphores for mutual exclusion Disadvantage: take/give tends to get spread out through the entire program (learned from exercise 1). Abstract data-types for mutual exclusion Principle: take/give part of (mutually exclusive) methods that are kept together with the hidden data. Monitor: Such a data-type with mutually exclusive access-functions is called a Monitor. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 4 / 1

Objects with mutual exclusion Monitors (objects & concept) In OOP we use classes as a (more powerful) mean to accomplish abstract data-types. Objects with such mutually exclusive methods are then monitor objects. For a class like Account: class Account { //... void deposit ( int a){ mutex. take (); balance += a; mutex. give (); the monitor concept is implemented by using semaphores. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 5 / 1

Objects with mutual exclusion Monitor == Semaphore Semaphores and Monitors are equivalent since: Semaphores (for threads but not for interrupt routines) can be (and are in standard Java) implemented by a monitor (with methods take and give). Monitors can be implemented by semaphores, for a given set of threads (using one MutexSem per monitor, and one CountingSem for each thread per monitor). Thus, a specific implementation using one mechanism can (even if hard) always be reimplemented using the other. Use the right technique depending on the problem to solve! http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 6 / 1

Abstractions related to concurrency Language support for Monitors Problem: Using semaphores requires (too much) discipline. Idea: Provide support via language constructs. Degree of language support: None [C/C++]: Manual calls (as with mutex; take/give) using library functions. Object-orientation may simplify usage. Explicit per method[java]: Declared property of methods (language and run-time support). Implicit per task [Ada]: Declared property of class (implicitly applies to all methods and data). None, i.e. no support, results in more complicated programming. Implicit language support safest and simplest but can limit applicability. Explicit with mutually exclusive methods is the pragmatic Java approach. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 7 / 1

Abstractions related to concurrency Abstractions Thread: Performs execution using a processor. Execution state: Thread status stored in context. Mutual exclusion: Restriction on context switching In Java we have threads represented by objects of type Thread, state of execution as in sequential programming, synchronized methods for mutual exclusion. The purpose of abstractions is to cope with complexity... http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 8 / 1

Abstractions related to concurrency Objects and concurrency Object properties Implicit mutual exclusion of methods Thread Exec. state No Yes Comment No No Object Monitor Passive objects No Yes Coroutine Co-monitor Not in Java 5 6 Yes No Not useful 7 8 Yes Yes Thread-object Task Active objects 1 The objects as in object oriented programming. 2 The monitors we accomplish by using synchronized. 3 Named Fibers (by Microsoft) when managed by OS. 7 Our active objects, not being monitors too! 8 Avoid; less practical and not supported in Java. 1 3 2 4 http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 9 / 1

http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 10 / 1

Mutual-exclusion support in Java Object categories Thread object: - Active object (if started but not terminated); drives execution. - Don t call me, I ll call you! Monitor object: - Mutually exclusive methods, e.g., by using synchronized. - Should be passive; do not mix monitors and threads! Plain passive object: - Thread safe by reentrant methods (java.lang.math) - Explicitly thread unsafe; to be used by a single thread (java.util.hashset) - Implicitly thread unsafe; has to be assumed if not documented. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 11 / 1

Mutual-exclusion support in Java Java-supported monitors In Java: Critical region/block/method is declared using the keyword synchronized for methods 1 or objects 2. Unfortunately, neither classes nor attributes can be declared synchronized; discipline required. The monitor concept by use of Java class Account { //... synchronized void deposit ( int a){ balance += a; 1 Meaning method(arg){synchronized(this){... 2 synchronized(obj){... locks obj for running the... code, but do not use. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 12 / 1

Waiting and Signaling in monitors synchronized - wait - notify Condition queue In addition to locking the object for exclusive access (mutex): Temporarily unlock until someone signals that the state has changed: synchronized(.){ Exclusive area Thread with exclusive access to monitor data wait Scheduling notify Thread about to enter waiting for exclusive access Monitor queue awaiting fulfilled condition Condition queue Thread just left monitor http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 13 / 1

Waiting and Signaling in monitors Notification is stateless; put any needed state in monitor A CountingSem has state Thread1 Thread2 s. take (); s. give (); to be compared with Thread1 Thread2 s. give (); s. take (); A notify is stateless Thread1 Thread2 wait (); notify (); to be compared with Thread1 Thread2 notify (); wait (); Thread1 continues in both cases since the internal state (counter) reflects the give. Thread1 waits until next notification; The notify is forgotten, unless appropriate state variables exist in the monitor. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 14 / 1

Waiting and Signaling in monitors Execution states, revisited Due to application Due to scheduler / kernel / OS Running Priority, timesharing Ready By this: wait, synchronized call Blocked By other: notify, synchronized return Object methods wait notify notifyall Keywords vs. methods The keyword synchronized is in language and in JVM The Object methods are in class Object and in JVM http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 15 / 1

http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 16 / 1

An analogy for better understanding monitors Monitor conditions - analogy Assume shared resource providing three operations: opa, opb, and addpaper. Only one can enter at a time, entrance means exclusive access. The opb requires that paper is available, discovered after entrance. Two queues, one for entrance (left) and one for conditions (right). http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 17 / 1

An analogy for better understanding monitors Monitor conditions - analogy/scenario It is the responsibility of the one performing addpaper to inform the waiting persons that the state of the object has changed. Scenario The persons (threads) entering the monitor to do opb, but discovers that there is no paper aborts the operation, exits, and waits in a special queue until the condition paper is available will be true. Even though there is no paper, other persons are let in to perform opa. Eventually someone arrives who changes the roll of paper after which the waiting persons can be let in again. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 18 / 1

Monitor variants Original Hoare Monitor (1974) Originally defined monitor properties: Immediate Resumption; the awakened thread takes control immediately The notify must be performed last, one thread only is awakened. The condition for waiting could be coded: if (!ok) wait(); The notifying thread guarantees that the condition being waited for is true. Easier to prove that starvation can not occur. Does not handle priority for blocked threads. The enter queue can be FIFO or (preferably) a priority queue. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 19 / 1

Monitor variants Real-time Monitor We assume these monitor properties: High priority threads should be given precedence, even to threads which have been waiting longer (desired starvation risk). Immediate resumption not guaranteed (depends on OS/scheduler) The condition being waited for might not be true anymore when a blocked thread resumes execution. Waiting for a condition must be coded: while (!ok) wait(); Use notifyall to avoid problems (practical - wakes all). The notify not necessarily called last in the method. When previously blocked threads precedes those with same priority + notify last + one level of priority: equivalent with Hoare Monitor. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 20 / 1

http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 21 / 1

HOWTO Basic rules Coding for concurrency correct programs Do not mix a thread and a monitor in the same object/class [so you can get assistance from the compiler concerning proper access, which should go over visible methods]. All public methods should be synchronized [and that is not inherited so redo in subclass]. Wrap thread-unsafe classes by monitor [if possibly used by multiple threads]. Do not use (spread-out) synchronized blocks [which are more for limited GUI concurrency]. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 22 / 1

HOWTO Details (on board and in book) Atomic access of long and double. Keyword volatile. Attribute for locking: private and final. The monitor property (synchronized) is not inherited. Subclass blocking. The internal lock can be exposed for external synchronization. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 23 / 1

Buffer example Badly implemented buffer class Producer extends Thread { public void run() { prod = source.get(); buffer.post(prod); class Consumer extends Thread { public void run() { cons = buffer.fetch(); sink.put(cons); class Buffer { synchronized void post(object obj) { if (buff.size()==maxsize) wait(); if (buff.isempty()) notify(); buff.add(obj); synchronized Object fetch() { if (buff.isempty()) wait(); if (buff.size()==maxsize) notify(); buff.remove(buff.size()); The if (...) wait(); makes the buffer fragile: additional calls of notify or additional interacting threads could course the buffering to fail. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 24 / 1

Buffer example Better buffer class Buffer // Inefficient!! { synchronized void post(object obj) { while (buff.size()>=maxsize) { wait(); buff.add(obj); notifyall(); synchronized Object fetch() { while (buff.isempty()) { wait(); buff.remove(buff.size()); notifyall(); class Buffer // Well done. { synchronized void post(object obj) { while (buff.size()>=maxsize) { wait(); if (buff.isempty()) notifyall(); buff.add(obj); synchronized Object fetch() { while (buff.isempty()) { wait(); if (buff.size()>=maxsize) notifyall(); buff.remove(buff.size()); The while (...) wait(); makes the buffer robust with respect to other threads that can access the buffer and change the conditions. http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 25 / 1

Take-home message: synchronized - wait - notify synchronized(.){ Thread about to enter For Lab2 & exam Make sure you understand how threads are interacting via monitors in Java; do understand this figure: Exclusive area Thread with exclusive access to monitor data wait Scheduling waiting for exclusive access notify Monitor queue awaiting fulfilled condition Condition queue Thread just left monitor http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and notify 2016-09-20 26 / 1