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

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

Concurrency: a crash course

Lecture 8: September 30

Chapter 6: Process Synchronization

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

What's wrong with Semaphores?

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Chapter 6: Process Synchronization

Java Threads. Introduction to Java Threads

Writing Parallel Programs COMP360

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

Chapter 32 Multithreading and Parallel Programming

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

Introduction to Real-Time Operating Systems

C09: Process Synchronization

Java Threads. COMP 585 Noteset #2 1

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Threads Questions Important Questions

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

Interprocess Communication By: Kaushik Vaghani

MCS-378 Intraterm Exam 1 Serial #:

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

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

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

Monitors; Software Transactional Memory

Multitasking / Multithreading system Supports multiple tasks

Models of concurrency & synchronization algorithms

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

1 Process Coordination

Reminder from last time

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

Applying Idioms for Synchronization Mechanisms Synchronizing communication components for the One-dimensional Heat Equation

Operating Systems. Lecture 8

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Concurrent Programming using Threads

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

OS06: Monitors in Java

Spinlocks. Spinlocks. Message Systems, Inc. April 8, 2011

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Dealing with Issues for Interprocess Communication

Operating Systems (1DT020 & 1TT802)

HY345 - Operating Systems

Concurrent Programming. CS105 Programming Languages Supplement

Multi-threaded programming in Java

Lecture 13 Concurrent Programming

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Internet and Intranet Protocols and Applications

COMP 346 WINTER Tutorial 5 MONITORS

Semaphores and Monitors: High-level Synchronization Constructs

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Midterm Exam Amy Murphy 19 March 2003

CS370 Operating Systems

Threads and Parallelism in Java

MCS-378 Intraterm Exam 1 Serial #:

Programming Language Concepts: Lecture 11

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

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

HY 345 Operating Systems

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Concurrent Programming

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Chapter 13 Topics. Introduction. Introduction

Chapter 5 Asynchronous Concurrent Execution

CS140 Operating Systems and Systems Programming Midterm Exam

Monitors & Condition Synchronization

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

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

CPS 506 Comparative Programming Languages. Programming Language Paradigms

Monitors; Software Transactional Memory

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

Concurrent Processes Rab Nawaz Jadoon

High-level Synchronization

Multiple Inheritance. Computer object can be viewed as

Programming in Parallel COMP755

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

Introduction to Operating Systems

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

Chapter 13. Concurrency ISBN

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

CS3502 OPERATING SYSTEMS

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

Synchronization (Part 2) 1/40

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Concurrency: mutual exclusion and synchronization

1 /10 2 /15 3 /20 4 /15 total /60. CSE 120 Fall 2007 Midterm Examination. Name. Student ID

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

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Operating Systems Structure

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Critical Section Problem: Example

Process Synchronization. studykorner.org

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Transcription:

Summary on Monitor Implementation techniques Note: in this document we use process and thread interchangeably. Monitor is neither a process (thread) nor an active entity. It is just an abstract data type (or class) whose code can be executed by only one process (or thread) at a time. A monitor object is an object intended to be used to coordinate multiple threads. Methods in the monitor can be either invoked static or non-static ways (creates monitor objects). The methods defined in the monitor are executed with mutual exclusion. That is, at each point in time, at most one thread may be executing any of its methods. Monitors also provide a mechanism for threads to temporarily give up exclusive access, in order to wait for some condition to be met, before regaining exclusive access and resuming their task. Monitors also have a mechanism for signaling other threads that such conditions have been met. Need of Condition Variables in the Monitor For many applications, mutual exclusion is not enough. Threads attempting an operation may need to wait until some condition P holds true. A busy waiting (or spin lock) is not an efficient solution for implementing wait() operation associated with a condition variable. Because by the definition of the monitor, only one thread is allowed in the monitor, the thread with busy waiting will prevent any other thread from entering the monitor to make the condition true. A condition variable defined in the monitor is associated with a queue on which a thread may wait for some condition to become true. Thus each condition variable c is associated with an assertion P c. While a thread is waiting on a condition variable, that thread is not considered to occupy the monitor, and so other threads may enter the monitor to change the monitor's state. In most types of monitors, these other threads may signal the condition variable c to indicate that assertion P c is true in the current state. Thus there are two main operations required on condition variables: wait c is called by a thread that needs to wait until the assertion P c is true before proceeding. While the thread is waiting, it does not occupy the monitor. signal c (sometimes termed as notify c) is called by a thread to indicate that the assertion P c is true. When a signal happens on a condition variable that at least one other thread is waiting on, there are at least two threads that could then occupy the monitor: the thread that signals and any one of the threads that is waiting. To ensure only one thread is active in the monitor at each time, a choice must be made. Two possibilities as the text book explains: Blocking condition variables (or Signal and Wait) give priority to a signaled thread. Non-blocking condition variables (or Signal and Continue) give priority to the signaling thread. 1. Blocking condition variables (Signal and Wait) The original proposals by two famous computer scientists C. A. R. Hoare and Per Brinch Hansen were for blocking condition variables. With a blocking condition variable, the signaling thread must wait until the 1

signaled thread relinquishes occupancy of the monitor by either returning or by again waiting on a condition variable. Let us first see the monitor implementation (with blocking condition variables) by using semaphores explained in the textbook. Only one thread can be active in a monitor at any instance. o When a thread calls a monitor procedure (method), the procedure will first check to see if any other thread is currently active within the monitor. o If so, the calling thread will be suspended until the other thread has left the monitor. o If no other thread is using the monitor, the calling thread may enter. So, by turning all the critical sections (of a problem) into monitor procedures, no two threads (or processes) will ever execute their critical sections at the same time. For each condition variable x, we introduce a semaphore x_sem and an integer variable x_count, both initialized to 0. Here, we implement the signal and wait in such a way that a signaling thread must wait until the resumed thread either leaves or wait. To do that we need an additional semaphore, next, initialized to 0, on which the signaling threads may suspend themselves. Our textbook does not explain the blocking condition variable concept clearly. Also the blocking condition variables (signal wait approach) can be implemented in two different ways (signal and urgent wait; signal and wait) with/without an additional queue in the monitor. To better understand a blocking implementation technique (signal and urgent wait), let us see the Figure 1. Figure 1: Monitor implementation based on blocking condition variables (signal and urgent wait) There are two condition variables a b in this monitor. We assume there are two queues of threads associated with each monitor object e is the entrance queue s is a queue of threads that have signaled. 2

In addition we assume that for each condition variable c, there is a queue c.q, which is a queue for threads waiting on condition variable c All queues are typically guaranteed to be fair. Each monitor operation in this implementation technique (signal and urgent wait) is explained below. enter the monitor operation: enter the method if the monitor is locked add this thread to e lock the monitor leave the monitor operation: return from the monitor (i.e., from the method invoked) schedule //see schedule operation below wait c operation (operation on condition variable c): add this thread to c.q schedule signal c operation : if there is a thread waiting on c.q select and remove one such thread t from c.q (t is called "the signaled thread") add this thread (that have signaled) to s resume t (so t will occupy the monitor next) Schedule operation : if there is a thread on s select and remove one thread from s and restart it (this thread will occupy the monitor next) if there is a thread on e select and remove one thread from e and restart it (this thread will occupy the monitor next) unlock the monitor (the monitor will become unoccupied) An alternative technique is signal and wait. There is no s queue in this technique and signaler waits on the e queue instead. In either case ("signal and urgent wait" or "signal and wait"), when a condition variable is signaled and there is at least one thread on waiting on the condition variable, the signaling 3

thread hands occupancy over to the signaled thread, so that no other thread can gain occupancy in between. 2. Non-blocking condition variables (signal and continue) With non-blocking condition variables (signal and continue condition variables), signaling does not cause the signaling thread to lose occupancy of the monitor. Instead the signaled threads are moved to the e queue. There is no need for the s queue. Figure 2: Non-blocking condition variables (signal and continue) implementation With non-blocking condition variables, the signal operation is often called notify. It is also common to provide a notifyall operation that moves all threads waiting on a condition variable to the e queue. Each monitor operation in this implementation technique (signal and continue) is explained below. enter the monitor operation: enter the method if the monitor is locked add this thread to e lock the monitor leave the monitor operation: return from the method schedule wait c operation: 4

add this thread to c.q schedule notify c operation : if there is a thread waiting on c.q select and remove one thread t from c.q (t is called "the notified thread") move t to e notify all c operation: move all threads waiting on c.q to e schedule operation : if there is a thread on e select and remove one thread from e and restart it unlock the monitor As a variation on this scheme, the notified thread may be moved to a queue called w, which has priority over e. 3. Java Monitor Implementation Technique Figure 3: Java monitor implementation In the Java language, each object may be used as a monitor. (However, methods that require mutual exclusion must be explicitly marked as synchronized.) Rather than having explicit condition variables, each monitor (i.e. object) is equipped with a single wait queue, in addition to its entrance queue. All waiting is done on this single wait queue and all notify and notify all operations apply to this queue. This approach has also been adopted in other languages such as C#. In some case notify signaling may be done implicitly (i.e., returning thread will trigger implicit signaling). 5

6