11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.

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.
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
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.
Mutual Exclusion.
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 Mutual Exclusion: Primitives and Implementation Considerations.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
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.
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.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS510 Concurrent Systems Introduction to Concurrency.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Memory Consistency Models. Outline Review of multi-threaded program execution on uniprocessor Need for memory consistency models Sequential consistency.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Slides on threads borrowed by Chase Landon Cox. Thread states BlockedReady Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
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.
6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Memory Consistency Models
COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala
Memory Consistency Models
Review: threads and synchronization
CS510 Operating System Foundations
Andy Wang Operating Systems COP 4610 / CGS 5765
Producer-Consumer Problem
Lecture 2 Part 2 Process Synchronization
Background and Motivation
Slides by Prof. Landon Cox and Vamsi Thummala
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Lecture 20: Synchronization
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Process/Thread Synchronization (Part 2)
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King, and Andrew S Tanenbaum

Synchronizing multiple threads Must control interleaving between threads Order of some operations irrelevant Independent Other operations are dependent and order does matter All possible interleaving must yield a correct answer A correct concurrent program will work no matter how fast the processors are that execute the various threads 11/18/20152

Synchronizing multiple threads All interleavings result in correct answer Try to constrain the thread executions as little as possible Controlling the execution and order of threads is called “synchronization” 11/18/20153

Too much milk The Gunter household drinks a lot of milk, but has a small fridge Problem: Carl and Elsa want there to always at least one gallon of milk in the fridge for dinner; fridge holds at most two gallons If either sees there is less than one gallon, goes to buy milk Specification: Someone buys milk if running low Never more than two gallons of milk milk 11/18/20154

Solution #0 – no sync Carl Elsa 5:30 Comes home 5:35 Checks milk 5:40 Goes to store 5:45Comes home 5:50 Buys milkChecks milk 5:55 Goes homeGoes to store 6:00 Puts milk in Fridge Buys milk 6:05Comes home 6:10Too much milk! 11/18/20155

Mutual Exclusion Ensure that only 1 thread is doing a certain thing at one time Only one person goes shopping at one time Critical section A section of code that needs to run atomically w.r.t. other code If code A and code B are critical sections w.r.t. each other threads cannot interleave events from A and B Critical sections must be atomic w.r.t. each other Share data (or other resourced, e.g., screen, fridge) What is the critical section in solution #0? 11/18/20156

Too much milk (solution #1) Assume only atomic operations are load and store Idea: leave note that going to check on milk status Carl: if (no note) {if (milk low) {leave note; buy milk; remove note;} Elsa: if (no note) {if (milk low) {leave note; buy milk; remove note;} What can go wrong? Is this better than before? 11/18/20157

Too much milk (solution #2) Idea: Change order of leave note and check milk Carl: if (milk low) {if (no note) {leave note; buy milk; remove note;} Elsa: if (milk low) {if (no note) {leave note; buy milk; remove note;} What can go wrong? Is this better than before? 11/18/20158

Too much milk (solution #3) Idea: Protect more actions with note Carl: if (no note) {leave note; if (milk low) {buy milk}; remove note;} Elsa: if (no note) {leave note; if (milk low) {buy milk}; remove note;} What can go wrong? Is this better than before? 11/18/20159

Too much milk (solution #4) Idea: Change order of leaving note and checking note Carl: leave noteCarl; if (no noteElsa) { if (milk low) {buy milk};} ; remove noteCarl Elsa: leave noteElsa; if (no noteCarl) { if (milk low) {buy milk};} ; remove noteElsa What can go wrong? Is this better than before? 11/18/201510

Too much milk (solution #5) Idea: When both leave note, always give priority to fixed one to buy milk Carl: leave noteCarl; while (noteElsa) {do nothing}; if (milk low) {buy milk}; remove noteCarl Elsa: leave noteElsa; if (no noteCarl) { if (milk low) {buy milk};} ; remove noteElsa Simplified instance of Bakery Algorithm 11/18/201511

Too much milk (solution #5) while (noteElsa) for Carl prevents him from buying milk at same time as Elsa Proof of correctness Two parts: Will never have two people buying milk at same time Will always have someone able to buy milk if it is needed Correct, but ugly Complicated, Asymmetric, Inefficient Carl wastes time while waiting (Busy Waiting) 11/18/201512

Higher-level synchronization Problem: could solve “too much milk” using atomic loads/stores, but messy Solution: raise the level of abstraction to make life easier for the programmer 11/18/ Concurrent programs High-level synchronization provided by software Low-level atomic operations provided by hardware

Locks (mutexes) A lock is used to prevent another thread from entering a critical section Two operations Lock(): wait until lock is free, then acquire do {if (lock == LOCK_FREE) { lock = LOCK_SET; break;} } while(1) Unlock(): lock = LOCK_FREE 11/18/201514

Locks (mutexes) Why was the “note” in Too Much Milk solutions #1 and #2 not a good lock? Four elements of using locks Lock is initialized to be free Acquire lock before entering a critical section Wait to acquire lock if another thread already holds Release lock after exiting critical section All synchronization involves waiting Thread can be running, or blocked (waiting) 11/18/201515

Locks Locks -- shared variable among all thread Multiple threads share locks Only affects threads that try to acquire locks Important: Lock acquisition is atomic! 11/18/201516

Lock Variables Critical section -- part of the program where threads access shared (global) state Locks -- shared variables used to enforce mutual exclusion Can have multiple lock variables 11/18/201517

Locks (mutexes) Locks make “Too Much Milk” really easy to solve! Correct but inefficient How to reduce waiting for lock? How to reduce time lock is held? 11/18/ Elsa: lock(frigdelock); if (milk low) {buy milk} unlock(fridgelock) Carl: lock(frigdelock); if (milk low) {buy milk} unlock(fridgelock)

Too Much Milk – Solution 7 Does the following work? lock(); if (milk low & no note) { leave note; unlock(); buy milk; remove note; } else { unlock() } 11/18/201519

Too Much Milk – Solution 7 Does the following work? lock(); if (milk low & no note) { leave note; unlock(); buy milk; lock(); remove note; unlock(); } else { unlock() } 11/18/201520

Queues without Locks enqueue (new_element, head) { // find tail of queue for(ptr=head; ptr->next != NULL; ptr = ptr->next); // add new element to tail ptr->next = new_element; new_element->next = NULL; } 11/18/201521

Queues without Locks dequeue(head, element) { element = NULL; // if something on queue, remove it if(head->next != NULL) { element = head->next; head->next = head->next->next;} return element; } What bad things can happen if two threads manipulate the queue at the same time? 11/18/201522

Thread-safe Queues with Locks enqueue (new_elt, head) { lock(queuelock); // find tail of queue for(ptr=head; ptr->next != NULL; ptr = ptr->next); // add new element to tail ptr->next = new_elt; new_elt->next = NULL; unlock(queuelock); } dequeue(head, elt) { lock(queuelock); element = NULL; // remove if possible if(head->next != NULL) { elt = head->next; head->next = head->next->next;} unlock(queuelock); return elt; } 11/18/201523

Invariants for multi-threaded queue Can enqueue() unlock anywhere? Stable state called an invariant I.e., something that is “always” true Is the invariant ever allowed to be false? 11/18/201524

Invariants for multi-threaded queue In general, must hold lock when manipulating shared data What if you’re only reading shared data? 11/18/201525

Enqueue – Can we do better? enqueue() { lock find tail of queue unlock lock add new element to tail of queue unlock } Is this better? 11/18/201526

Dequeue if empty? What if you wanted to have dequeue() wait if the queue is empty? Could spin in a loop: dequeue() { lock(queuelock); element = NULL; while (head-next == NULL) {wait;}; if(head->next != NULL) { element = head->next; head->next = head->next->next;} unlock(queuelock); return element; } 11/18/201527

Problem lock(queuelock); … while (head-next == NULL) {wait;}; Holding lock while waiting No one else can access list (if they observe the lock) Wait forever 11/18/201528

Dequeue if empty – Try 2 Could release the lock before spinning: lock(queuelock); … unlock(queuelock); while (head-next == NULL) {wait;}; Will this work? 11/18/201529

Dequeue if empty – Try 3 Could release lock and acquire lock on every iteration lock(queuelock); … while (head-next == NULL) {unlock(queuelock); lock(queuelock);}; This will work, but very ineffecient. 11/18/201530