CS533 Concepts of Operating Systems Class 3

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
CHAPTER3 Higher-Level Synchronization and Communication
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
CS533 Concepts of Operating Systems Class 3 Data Races and the Case Against Threads.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS533 Concepts of Operating Systems Class 3 Monitors.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Monitors: An Operating System Structuring Concept
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
CS510 Concurrent Systems Introduction to Concurrency.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Chapter 71 Monitors (7.7)  A high-level-language object-oriented concept that attempts to simplify the programming of synchronization problems  A synchronization.
Eraser: A dynamic Data Race Detector for Multithreaded Programs Stefan Savage, Michael Burrows, Greg Nelson, Patrick Sobalvarro, Thomas Anderson Presenter:
CS533 Concepts of Operating Systems Class 2a Monitors.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS703 - Advanced Operating Systems
Chapter 6: Process Synchronization
Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Jonathan Walpole Computer Science Portland State University
CS510 Operating System Foundations
Chapter 5: Process Synchronization
CS510 Operating System Foundations
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Chapter 30 Condition Variables
CS533 Concepts of Operating Systems Class 3
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Synchronization: Monitors
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Eraser: A dynamic data race detector for multithreaded programs
Process/Thread Synchronization (Part 2)
Presentation transcript:

CS533 Concepts of Operating Systems Class 3 Monitors

Enforcing mutual exclusion Assumptions: Every thread sets the lock before accessing shared data! Every thread releases the lock after it is done! Only works if you follow these programming conventions all the time! Thread 1 Thread 2 Thread 3 Lock Lock A = 2 A = A+1 A = A*B Unlock Unlock CS533 - Concepts of Operating Systems

Solutions to misuse (or no use) of locks Solution 1: use static or dynamic checking tools to help track down misuses of locking primitives (synchronization bugs) Solution 2: have the compiler insert the synchronization primitives for you automatically CS533 - Concepts of Operating Systems

Solution 1: Checking tools (class 2 cont.) Eraser A dynamic checker that uses binary re-writing techniques Gathers an “execution history” of reads, writes and lock acquisitions Evaluates consistency with rules Is it enough to simply check that some lock is held whenever a global variable is accessed? CS533 - Concepts of Operating Systems

Automated checking of conventions Eraser doesn’t know ahead of time which locks protect which variables It infers which locks protect which variables using a lock-set algorithm Assume all locks are candidates for a variable ( C(v) is full) For each access take intersection of C(v) and locks held by thread and make these the candidate set C(v) If C(v) becomes empty, issue warning CS533 - Concepts of Operating Systems

Improving the locking discipline The standard approach produces many false positives that arise due to special cases: Initialization No need to lock if no thread has a reference yet Read sharing No need to lock if all threads are readers Reader/writer locking Distinguish concurrent readers from concurrent readers and writers CS533 - Concepts of Operating Systems

CS533 - Concepts of Operating Systems Improved algorithm wr, new thread virgin rd, wr First thread shared Modified (race?) exclusive rd rd, new thread wr wr shared CS533 - Concepts of Operating Systems

CS533 - Concepts of Operating Systems Solution 2: Monitors Monitors employ two key concepts, both of which can be automated by a compiler: Encapsulation: Local data variables are accessible only via the monitor’s entry procedures (like methods) Mutual exclusion: The entry procedures are treated as critical sections CS533 - Concepts of Operating Systems

Two kinds of synchronization Mutual exclusion Only one at a time in the critical section Condition synchronization Wait until a certain condition holds Signal waiting threads when the condition holds CS533 - Concepts of Operating Systems

(Each has an associated list of waiting threads) Monitor structures initialization code “entry” methods y x shared data condition variables monitor entry queue List of threads waiting to enter the monitor Can be called from outside the monitor. Only one active at any moment. Local to monitor (Each has an associated list of waiting threads) local methods CS533 - Concepts of Operating Systems

Implementing mutual exclusion for monitors How can we implement mutual exclusion for monitor procedures? CS533 - Concepts of Operating Systems

Implementing mutual exclusion for monitors How can we implement mutual exclusion for monitor procedures? Will spinning locks work? CS533 - Concepts of Operating Systems

Implementing mutual exclusion for monitors How can we implement mutual exclusion for monitor procedures? Will spinning locks work? Will yielding locks work? CS533 - Concepts of Operating Systems

Implementing mutual exclusion for monitors How can we implement mutual exclusion for monitor procedures? Will spinning locks work? Will yielding locks work? What if we don’t have atomic instructions? CS533 - Concepts of Operating Systems

Implementing mutual exclusion for monitors How can we implement mutual exclusion for monitor procedures? Will spinning locks work? Will yielding locks work? What if we don’t have atomic instructions? Idea 1: Disable interrupts during monitor procedures CS533 - Concepts of Operating Systems

Using monitors to build a blocking mutex Blocking_mutex:monitor Begin busy:boolean; nonbusy:condition; busy:=false; // initial value Procedure acquire() if busy then nonbusy.wait; busy:=true; End Procedure release() busy:=false; nonbusy.signal End; End Blocking_mutex; CS533 - Concepts of Operating Systems

Using monitors to build a blocking mutex Blocking_mutex:monitor Begin busy:boolean; nonbusy:condition; Busy:=false; // initial value Procedure acquire() Begin <----- disable interrupts if busy then nonbusy.wait; busy:=true; End <----- enable interrupts Procedure release() busy:=false; nonbusy.signal End; <----- enable interrupts End Blocking_mutex; CS533 - Concepts of Operating Systems

Using monitors to build a blocking mutex Blocking_mutex:monitor Begin busy:boolean; nonbusy:condition; Busy:=false; // initial value Procedure acquire() Begin ----- disable interrupts if busy then nonbusy.wait; <----- ???? busy:=true; End ----- enable interrupts Procedure release() busy:=false; nonbusy.signal <----- ???? End; ----- enable interrupts End Blocking_mutex; CS533 - Concepts of Operating Systems

Implementing condition variables Wait Add process to queue of processes waiting on this condition Suspend process Release monitor’s mutual exclusion Reenable interrupts Wake up / schedule next process trying to enter monitor Signal Wake up / schedule first process waiting on this condition Release monitor’s mutual exclusion? Suspend yourself On what? … and how do you ever wake up again? CS533 - Concepts of Operating Systems

Implementing mutual exclusion for monitors How can we implement mutual exclusion for monitor procedures? Will spinning locks work? Will yielding locks work? What if we don’t have atomic instructions? Idea 1: Disable interrupts during monitor procedures Idea 2: Use binary semaphores CS533 - Concepts of Operating Systems

Building monitors from binary semaphores See example in paper CS533 - Concepts of Operating Systems

Bounded buffer solution with monitors process Producer begin loop <produce char “c”> BoundedBuffer.append(c) end loop end Producer BoundedBuffer: monitor var buffer : ...; nextIn, nextOut :... ; procedure append (c: char) begin ... end procedure remove (var c: char) end BoundedBuffer process Consumer begin loop BoundedBuffer.remove(c) <consume char “c”> end loop end Consumer CS533 - Concepts of Operating Systems

Bounded buffer solution with monitors BoundedBuffer: monitor var buffer : array[0..n-1] of char nextIn,nextOut : 0..n-1 := 0 Count : 0..n := 0 nonEmpty, nonFull : condition procedure append(c:char) procedure remove(var c: char) begin begin if (Count = n) then if (Count = n) then wait(nonFull) wait(nonEmpty) end if end if buffer[nextIn] := c c := buffer[nextOut] nextIn := nextIn+1 mod n nextOut := nextOut+1 mod n Count := Count+1 Count := Count-1 signal(nonEmpty) signal(nonFull) end append end remove end BoundedBuffer CS533 - Concepts of Operating Systems

CS533 - Concepts of Operating Systems Alarm clock example AlarmClock: monitor Begin now: integer; wakeup: condition; now := 0; Procedure wakeme(n: integer); alarmsetting: integer; alarmsetting := now + n; While now < alarmsetting do wakeup.wait (alarmsetting); wakeup.signal; End; Procedure tick; now := now + 1; End AlarmClock; CS533 - Concepts of Operating Systems