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.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Section 3. True/False Changing the order of semaphores’ operations in a program does not matter. False.
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: High-level Synchronization Constructs.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
1 CSE451 – Section 4. 2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization February 13, 2006 Prof. Anthony D. Joseph.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization September 26, 2005 Prof. John Kubiatowicz.
Reminders Homework 3 due next Monday
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization September 23, 2009 Prof. John Kubiatowicz.
Monitors CSCI 444/544 Operating Systems Fall 2008.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization September 25, 2006 Prof. John Kubiatowicz.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
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.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
CS Introduction to Operating Systems
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
CS510 Concurrent Systems Introduction to Concurrency.
Nachos Phase 1 Code -Hints and Comments
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables February 2, 2011 Ion Stoica
11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CS162 Operating Systems and Systems Programming Lecture 7 Readers-Writers Language Support for Synchronization February 13, 2008 Prof. Anthony D. Joseph.
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization Friday 11, 2010 Ion Stoica
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Background on the need for Synchronization
CS162 Operating Systems and Systems Programming Lecture 6 Readers/Writers Problem, Working in Teams February 10, 2014 Anthony D. Joseph
1 July 2015 Charles Reiss CS162: Operating Systems and Systems Programming Lecture 7: Synchronization: Lock Implementation,
Monitors, Condition Variables, and Readers-Writers
Introduction to Operating Systems
Sarah Diesburg Operating Systems COP 4610
September 17, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 6 Readers/Writers Problem,
Anthony D. Joseph and Ion Stoica
Anthony D. Joseph and John Canny
Chapter 30 Condition Variables
Implementing Mutual Exclusion
February 6, 2013 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Implementing Mutual Exclusion
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Chapter 6: Synchronization Tools
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSE 542: Operating Systems
CS162 Operating Systems and Systems Programming Lecture 8 Locks, Semaphores, Monitors, and Quick Intro to Scheduling September 21st 2016 Prof. Anthony.
September 19, 2018 Prof. Ion Stoica
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

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 thread with a reference to the semaphore can call V()

True/False A thread needs to own the monitor lock before it can signal() a condition variable. True: A thread must acquire the monitor lock before it can access any of the monitor’s state.

True/False Anything that can be done with monitors can also be done with semaphores. True: Since monitors can be implemented using semaphores.

StackOverflow Analogy A monitor is like a public toilet. Only one person can enter at a time. They lock the door to prevent anyone else coming in, do their stuff, and then unlock it when they leave.

Short Answer Give two reasons why the following implementation of a condition variable is incorrect (assume that MySemi is a semaphore initialized to 0): Wait() { MySemi.P(); } Signal() { MySemi.V(); } Reason 1: Semaphores are commutative, while condition variables are not. Practically speaking, if someone executes MySemi.V() followed by MySemi.P(), the latter will not wait. In contrast, execution of Signal() before Wait() should have no impact on Wait().

Short Answer Give two reasons why the following implementation of a condition variable is incorrect (assume that MySemi is a semaphore initialized to 0): Wait() { MySemi.P(); } Signal() { MySemi.V(); } Reason 2: The above implementation of Wait() will deadlock the system if it goes to sleep on MySemi.P() (since it will go to sleep while holding the monitor lock).

Condition Variables Condition Variable: a queue of threads waiting for something inside a critical section –Key idea: allow sleeping inside critical section by atomically releasing lock at time we go to sleep –Contrast to semaphores: Can’t wait inside critical section Operations: – Wait(&lock) : Atomically release lock and go to sleep. Re-acquire lock later, before returning. – Signal() : Wake up one waiter, if any – Broadcast() : Wake up all waiters Rule: Must hold lock when doing condition variable ops!

Complete Monitor Example (with condition variable) Here is an (infinite) synchronized queue Lock lock; Condition dataready; Queue queue; AddToQueue(item) { lock.Acquire();// Get Lock queue.enqueue(item);// Add item dataready.signal();// Signal any waiters lock.Release();// Release Lock } RemoveFromQueue() { lock.Acquire();// Get Lock while (queue.isEmpty()) { dataready.wait(&lock); // If nothing, sleep } item = queue.dequeue();// Get next item lock.Release();// Release Lock return(item); }

Short Answer What is the difference between Mesa and Hoare scheduling for monitors? For Mesa scheduling, the signaler keeps the lock and CPU, while the signaled thread is simply put on the ready queue and will run at a later time. Further, a programmer with Mesa scheduled monitors must recheck the condition after being awoken from a Wait() operation [ i.e. they need a while loop around the execution of Wait().

Short Answer What is the difference between Mesa and Hoare scheduling for monitors? For Hoare scheduling, the signaler gives the lock and CPU to the signaled thread which begins running until it releases the lock, at which point the signaler regains the lock and CPU. A programmer with Hoare scheduled monitors does not need to recheck the condition after being awoken, since they know that the code after the Wait() is executed immediately after the Signal()

Short Answer Explain when using a spin-lock can be more efficient. Answer: If the expected wait time of the lock is very short (such as because the lock is rarely contested or the critical sections are very short), then it is possible that a spin lock will waste many fewer cycles than putting threads to sleep/waking them up. The important issue is that the expected wait time must be less than the time to put a thread to sleep and wake it up.

Short Answer Give two reasons why this is a bad implementation for a lock: lock.acquire() { disable interrupts; } lock.release() { enable interrupts; } Answer: (1) It prevents hardware events from occurring during the critical section, (2) User programs cannot use this lock, (3) It doesn’t work for data shared between different processors.

Semaphore Implementation public P() { lock.Acquire(); while (value == 0) c.Wait(); value--; lock.Release(); }

Semaphore Implementation public V() { lock.Acquire(); value++; c.Signal(); lock.Release(); }