15-410, S’04 - 1 - Synchronization #3 Feb. 2, 2004 Dave Eckhardt Bruce Maggs L10_Synch 15-410 “...Arguably less wrong...”

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.
Ch 7 B.
CSCC69: Operating Systems
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Secure Operating Systems Lesson 5: Shared Objects.
15-410, F’ Synchronization #3 Sep. 22, 2004 Dave Eckhardt Bruce Maggs L10a_Synch “...Arguably less wrong...”
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
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
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
What we will cover… Process Synchronization Basic Concepts
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS Introduction to Operating Systems
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
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.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
CS510 Concurrent Systems Introduction to Concurrency.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
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.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
1 Synchronization (3) Dave Eckhardt
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization – Part 3
Operating Systems CMPSC 473
Chapter 5: Process Synchronization
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Synchronization and Semaphores
Critical section problem
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Implementing Mutual Exclusion
Chapter 6: Synchronization Tools
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
Presentation transcript:

15-410, S’ Synchronization #3 Feb. 2, 2004 Dave Eckhardt Bruce Maggs L10_Synch “...Arguably less wrong...”

15-410, S’ Synchronization Project 0 assembly language interface – What we hoped you'd do ● Experiment with calling asm from C ● Or understand how hard asm() is to use correctly – What we saw some people do ● %ebp mangling via brittle C – Assuming knowledge of current frame's layout ● asm() without register/variable mapping declarations – The issue ● “Detouring around” experimentation & learning isn't ideal – “Learn now vs. learn later” – “Learn vs. not learn”

15-410, S’ Synchronization P2 (et seq.) partners – “Partner Registration Page” on web site – 16 people have already registered - Thanks! – If you know, please register today ● This will help people still looking for partners Good things to talk about – How many late days? – Project schedule in other classes ● Write down a joint project schedule – Auditing or pass/fail? Target 410 grade? – Prior experience

15-410, S’ Outline Last time – How mutual exclusion is really implemented Condition variables – Under the hood – The atomic-sleep problemSemaphoresMonitors Next time – Project 2 (thread library)

15-410, S’ Voluntary de-scheduling The Situation – You hold lock on shared resource – But it's not in “the right mode” Action sequence – Unlock shared resource – Write down “wake me up when...” – Go to sleep until resource changes state

15-410, S’ What not to do while (!reckoning) { mutex_lock(&scenario_lk); if ((date >= ) && (hour >= 5)) reckoning = true; else mutex_unlock(&scenario_lk); } wreak_general_havoc(); mutex_unlock(&scenario_lk);

15-410, S’ What not to do Why is this wrong? – Make sure you understand! – See previous two lectures – Do not do this in P2 or P3

15-410, S’ Arguably Less Wrong while (!reckoning) { mutex_lock(&scenario_lk); if ((date >= ) && (hour >= 5)) reckoning = true; else { mutex_unlock(&scenario_lk); sleep(1); } wreak_general_havoc(); mutex_unlock(&scenario_lk);

15-410, S’ Arguably less wrong Don't do this either – How wrong is “a while”? ● N times it's much too short ● Last time it's much too long ● It's wrong every time

15-410, S’ Something is missing Mutex protects shared state – Good How can we sleep for the right duration? – Get an expert to tell us! We encapsulated “interfering code sequence” –...into a “mutex” object Encapsulate “the right duration” –...into a “condition variable” object

15-410, S’ Once more, with feeling! mutex_lock(&scenario_lk); while (cvar = wait_on()) { cond_wait(&scenario_lk, &cvar); } wreak_general_havoc(); /* locked! */ mutex_unlock(&scenario_lk);

15-410, S’ wait_on()? if (y < 1906) return (&new_year); else if (m < 4) return (&new_month); else if (d < 18) return (&new_day); else if (h < 5) return (&new_hour); else return (0);

15-410, S’ What wakes us up? for (y = 1900; y < 2000; y++) for (m = 1; m <= 12; m++) for (d = 1; d <= days(month); d++) for (h = 0; h < 24; h++)... cond_broadcast(&new_hour); cond_broadcast(&new_day); cond_broadcast(&new_month); cond_broadcast(&new_year);

15-410, S’ Condition Variable Requirements Keep track of threads asleep “for a while” Allow notifier thread to wake sleeping thread(s) Must be thread-safe – Many threads may call condition_wait() at same time – Many threads may call condition_signal() at same time – Say, those look like “interfering sequences”...

15-410, S’ Why two parameters? condition_wait(&mutex, &cvar); Lock required to access/modify the shared state Whoever awakens you will need to hold that lock – You'd better give it up. When you wake up, you will need to hold it again – “Convenient” for condition_wait() to un-lock/re-lock But there's something more subtle

15-410, S’ Inside a Condition Variable “queue” - of sleeping processes – FIFO or more exoticmutex – protects against interfering cond_wait()/cond_signal()

15-410, S’ Inside a Condition Variable cond_wait(mutex, cvar) { lock(cvar->mutex); enq(cvar->queue, my_thread_id()); unlock(mutex); ATOMICALLY { unlock(cvar->mutex); kernel_thread_pause(); } What is this “ATOMICALLY” stuff?

15-410, S’ Pathological Execution Sequence

15-410, S’ Achieving condition_wait() Atomicity Disable interrupts (if you are a kernel) Rely on OS to implement condition variables – (yuck?) Have a “better” thread-sleep interface P2 challenge – Understand this issue – Understand the host kernel we give you – Put the parts together – Don't use “wrong” or “arguably less wrong” approaches

15-410, S’ Outline Last time – How mutual exclusion is really implemented Condition variables – Under the hood – The atomic-sleep problem  Semaphores Monitors Next time – Project 2 (thread library)

15-410, S’ Semaphore Concept Semaphore is a different encapsulation object – Can produce mutual exclusion – Can produce sleep-until-it's-time Intuition: counted resource – Integer represents “number available” ● Semaphore object initialized to a particular count – Thread blocks until it is allocated an instance

15-410, S’ Semaphore Concept wait(), aka P(), aka proberen (“wait”) – wait until value > 0 – decrement value signal(), aka V(), aka verhogen (“increment”) – increment value Just one small issue... – wait() and signal() must be atomic

15-410, S’ “Mutex-style” Semaphore semaphore m = 1; do { wait(m); /* mutex_lock() */..critical section... signal(m); /* mutex_unlock() */...remainder section... } while (1);

15-410, S’ “Condition-style” Semaphore

15-410, S’ “Condition with Memory”

15-410, S’ Semaphore vs. Mutex/Condition Good news – Semaphore is a higher-level construct – Integrates mutual exclusion, waiting – Avoids mistakes common in mutex/condition API ● Lost signal() ● Reversing signal() and wait() ●...

15-410, S’ Semaphore vs. Mutex/Condition Bad news – Semaphore is a higher-level construct – Integrates mutual exclusion, waiting ● Some semaphores are “mutex-like” ● Some semaphores are “condition-like” ● How's a poor library to know? – Spin-wait or not???

15-410, S’ Semaphores - 31 Flavors Binary semaphore – It counts, but only from 0 to 1! ● “Available” / “Not available” – Consider this a hint to the implementor... ● “Think mutex!” Non-blocking semaphore – wait(semaphore, timeout); Deadlock-avoidance semaphore – #include

15-410, S’ My Personal Opinion One “simple, intuitive” synchronization object In 31 performance-enhancing flavors!!! “The nice thing about standards is that you have so many to choose from.” – Andrew S. Tanenbaum Conceptually simpler to have two objects – One for mutual exclusion – One for waiting –...after you've understood what's actually happening

15-410, S’ Semaphore Wait: Inside Story wait(semaphore s) ACQUIRE EXCLUSIVE ACCESS --s->count; if (s->count < 0) enqueue(s->queue, my_id()); ATOMICALLY RELEASE EXCLUSIVE ACCESS thread_pause() else RELEASE EXCLUSIVE ACCESS

15-410, S’ Semaphore Signal: Inside Story signal(semaphore s) ACQUIRE EXCLUSIVE ACCESS ++s->count; if (s->count <= 0) { tid = dequeue(s->queue); thread_wakeup(tid); RELEASE EXCLUSIVE ACCESS What's all the shouting? – An exclusion algoritm much like a mutex, or – OS-assisted atomic de-scheduling

15-410, S’ Monitor Basic concept – Semaphores eliminate some mutex/condition mistakes – Still some common errors ● Swapping “signal()” & “wait()” ● Accidentally omitting one Monitor: higher-level abstraction – Module of high-level language procedures ● All access some shared state – Compiler adds synchronization code ● Thread in any procedure blocks all thread entries

15-410, S’ Monitor “commerce” int cash_in_till[N_STORES] = { 0 }; int wallet[N_CUSTOMERS] = { 0 } ; boolean buy(int cust, store, price) { if (wallet[cust] >= price) { cash_in_till[store] += price; wallet[cust] -= price; return (true); } else return (false); }

15-410, S’ Monitors – What about waiting? Automatic mutal exclusion is nice... –...but it is too strong Sometimes one thread needs to wait for another – Automatic mutual exclusion forbids this – Must leave monitor, re-enter - when? Have we heard this “when” question before?

15-410, S’ Monitor Waiting – The Problem void stubbornly_cash_check(acct a, check c) { while (account[a].bal < check.val) {...what goes here?... } account[a].bal -= check.val; }

15-410, S’ Monitor Waiting – Wrong Solution boolean try_cash_check(acct a, check c) { if (account[a].bal < check.val) return (false); account[a].bal -= check.val; return (true); }

15-410, S’ Monitor condition variables Similar to condition variables we've seen condition_wait(cvar) – Only one parameter – Mutex-to-drop is implicit ● (the “monitor mutex”) – Operation ● “Temporarily exit monitor” -- drop the mutex ● Wait until signalled ● “Re-enter monitor” - re-acquire the mutex

15-410, S’ Monitor Waiting void stubbornly_cash_check(acct a, check c) { while (account[a].bal < check.val) { cond_wait(account[a].activity) ; } account[a].bal -= check.val; }

15-410, S’ Monitor condition variables signal() policy question - which thread to run? – Signalling thread? Signalled thread? – Or: signal() exits monitor as side effect! – Different signal() policies mean different monitor flavors

15-410, S’ Summary Two fundamental operations – Mutual exclusion for must-be-atomic sequences – Atomic de-scheduling (and then wakeup) Mutex/condition-variable (“pthreads”) style – Two objects for two core operations Semaphores, Monitors – Semaphore: one object – Monitor: invisible compiler-generated object – Same core ideas inside

15-410, S’ Summary What you should know – Issues/goals – Underlying techniques – How environment/application design matters All done with synchronization? – Only one minor issue left ● Deadlock