CS Introduction to Operating Systems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
1 Semaphores and Monitors: High-level Synchronization Constructs.
Classic Synchronization Problems
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Nachos Phase 1 Code -Hints and Comments
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Atomic Operations David Monismith cs550 Operating Systems.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Semaphores Reference –text: Tanenbaum ch
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
CS 537 – Introduction to Operating Systems
Semaphores Reference text: Tanenbaum ch
CS703 – Advanced Operating Systems
PARALLEL PROGRAM CHALLENGES
Lecture 14: Pthreads Mutex and Condition Variables
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitor Giving credit where it is due:
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
Thread Synchronization including Mutual Exclusion
Lecture 14: Pthreads Mutex and Condition Variables
CSE 153 Design of Operating Systems Winter 19
CSCI1600: Embedded and Real Time Software
Semaphores Reference text: Tanenbaum ch
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
CSE 542: Operating Systems
Presentation transcript:

CS 537 - Introduction to Operating Systems Monitors CS 537 - Introduction to Operating Systems

Issues with Semaphores Semaphores are useful and powerful BUT, they require programmer to think of every timing issue easy to miss something difficult to debug Examples Example 1 Example 2 Example 3 mutex.P() mutex.V() mutex.P() critical section critical section part of critical section mutex.P() mutex.V() remaining critical section

Monitors Let the compiler handle the details! Monitors are a high level language construct for dealing with synchronization similar to classes in Java a monitor has fields and methods Programmer only has to say what to protect Compiler actually does the protection compiler will use semaphores to do protection

Monitors Basic structure of monitor monitor monitor-name { // fields // methods } Only methods inside monitor can access fields At most ONE thread can be active inside monitor at any one time

Condition Variables Monitors utilize condition variables Two methods associated with each condition variable wait: blocks a thread, places itself in a waiting queue for this condition variable, and allows another thread to enter the monitor signal: pulls a single thread off the waiting queue of this condition variable note: only one thread removed from wait queue if no threads waiting, no effect

Example monitor Foo { int maxValue, value; condition atMin, atMax; this.maxValue = maxValue; value = 0; } void increment() { if(value == maxValue) atMax.wait; value++; atMin.signal; void decrement() { if(value == 0) atMin.wait; value--; atMax.signal;

Example Only the methods increment and decrement can access maxValue, value, atMin, atMax If one thread is executing in increment, then no other thread can be executing in either increment OR decrement remember, only one thread allowed into a monitor at any one time Notice signal is always called at the end of increment and decrement if no one waiting on the condition, no effect

Monitor Analogy workbench 1 workbench 2 workbench 3

Monitor Analogy Imagine a single room Only one person allowed in room at a time person locks the door to the room on entry that person is doing some work in the room Anyone else wanting to do work in the room has to wait outside until door is unlocked If person in room decides to rest (but isn’t finished with work), unlocks the door and goes into a side room (wait)

Monitor Analogy Multiple people can be in the side room they are all taking a nap When person finishes working and leaves room, first check side room for anyone napping if someone is napping, wake them up (signal) otherwise, unlock the door and leave the room

More on signal and wait Assume P is running and Q is waiting Remember, only one thread inside the monitor at a time If P calls signal, there are two possible Q thread continuation strategies signal-and-hold: P signals Q and then P blocks until Q leaves the monitor signal-and-continue: P signals Q and then Q waits until P leaves the monitor

Signal-and-Continue Good points Bad points P can exit the monitor and move on to other work P can wakeup multiple threads before exiting the monitor Bad points Q has to wait until P leaves the monitor so the condition it was waiting for may not be true anymore

Signal-and-Continue Example public void decrement() { if(value == 0) atMin.wait; value--; atMax.signal; anotherCondition.signal; } Notice that the thread currently in monitor gets to wakeup two threads and then exit If the thread waiting on anotherCondition wins the monitor lock and then changes value to maxValue, there will be a problem when the thread waiting on atMax wakes up why?

Signal-and-Hold Good points Bad points Revisit above example when Q wakes up it is guaranteed to have its condition still be true P can wakeup multiple threads before exiting the monitor Bad points P must wait immediately so it can’t exit the monitor and do other work Revisit above example

Signal-and-Leave Allow P to continue after calling signal Force P to exit immediately Good points when Q wakes up it is guaranteed to have its condition still be true P can exit the monitor and move on to other work Bad points P can only wakeup a single thread Good compromise

Implementing Signal and Wait Programmer Code Compiler Code (psuedocode) monitor M { monitor M { condition c; sem_t mutex = 1; sen_t cs = 0; int cc = 0; foo() { foo() { . . . mutex.down(); c.wait; . . . . . . cc++; mutex.up(); cs.down(); cc--; } . . . mutex.up(); } bar() { bar() { . . . mutex.down(); c.signal; . . . } if( cc > 0) { cs.up(); } else mutex.up(); } }

Implementing Signal and Wait Previous example is for a signal-and-leave system If a signal-and-hold technique is used, things are a bit different keep a high priority count thread that issues the signal is a high priority on a wait call, check if any high priority threads waiting on a signal, make the current thread wait in a high priority queue

Signal-and-Hold monitor M { sem_t mutex = 1; sem_t cs = 0; int cc = 0; sem_t hs = 0; int hc = 0; foo() { mutex.down(); . . . cc++; if(hc > 0) { hs.up(); } else { mutex.up(); } cs.down(); cc--; } bar() { if( cc > 0) { hc++; cs.up(); hs.down(); hc--; }

Alternative to Signal Major problem with signal is that it only wakes up one process usually not much control over which process this is What if you want all the threads waiting on a condition to become runnable? use notify()

notify() notify() moves all threads currently blocked on some condition to the runnable state only after a signal() is received then all the threads compete to get the CPU Caution: good possibility the condition waiting for may no longer be true when a process gets the CPU should do all wait calls inside of a while loop

Monitor Review CPU Monitor lock owner entry wait queue (waiting for lock) condition wait queue (waiting to be notified) notify() runnable queue (competing for CPU) CPU

signal() Review CPU Monitor lock owner entry wait queue (waiting for lock) condition wait queue (waiting to be notified) signal() runnable queue (competing for CPU) CPU

notify() Review CPU Monitor lock owner entry wait queue (waiting for lock) condition wait queue (waiting to be notified) notify runnable queue (competing for CPU) CPU