Andy Wang Operating Systems COP 4610 / CGS 5765

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.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
PROCESS SYNCHRONIZATION READINGS: CHAPTER 5. ISSUES IN COOPERING PROCESSES AND THREADS – DATA SHARING Shared Memory Two or more processes share a part.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
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,
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
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.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
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.
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.
CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations, Locks September 10, 2012 Ion Stoica
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
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,
Homework 3-4 Sarah Diesburg Operating Systems COP 4610.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
29 June 2015 Charles Reiss CS162: Operating Systems and Systems Programming Lecture 5: Basic Scheduling + Synchronization.
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Process Synchronization
COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala
Monitors, Condition Variables, and Readers-Writers
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Lecture 11: Mutual Exclusion
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
January 31, 2011 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations,
Designing Parallel Algorithms (Synchronization)
Sarah Diesburg Operating Systems COP 4610
Semaphores and Bounded Buffer
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Race conditions and Synchronization
Implementing Mutual Exclusion
Implementing Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Andy Wang Operating Systems COP 4610 / CGS 5765
Andy Wang Operating Systems COP 4610 / CGS 5765
EECE.4810/EECE.5730 Operating Systems
Andy Wang Operating Systems COP 4610 / CGS 5765
Don Porter Portions courtesy Emmett Witchel
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Andy Wang Operating Systems COP 4610 / CGS 5765 Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765

Motivating Example: Too Much Milk Two robots are programmed to maintain the milk inventory at a store… They are not aware of each other’s presence… Robot: Dumber Robot: Dumb

Motivating Example: Too Much Milk Dumb 10:00 Look into fridge: Out of milk Dumber

Motivating Example: Too Much Milk Dumb 10:00 Look into fridge: Out of milk 10:05 Head for the warehouse Dumber

Motivating Example: Too Much Milk Dumb 10:05 Head for the warehouse Dumber 10:10 Look into fridge: Out of milk

Motivating Example: Too Much Milk Dumb Dumber 10:10 Look into fridge: Out of milk 10:15 Head for the warehouse

Motivating Example: Too Much Milk Dumb 10:20 Arrive with milk Dumber 10:15 Head for the warehouse

Motivating Example: Too Much Milk Dumb 10:20 Arrive with milk Dumber 10:15 Head for the warehouse

Motivating Example: Too Much Milk Dumb 10:20 Arrive with milk 10:25 Go party Dumber

Motivating Example: Too Much Milk Dumb 10:20 Arrive with milk 10:25 Go party Dumber 10:30 Arrive with milk: “Uh oh…”

Definitions Synchronization: uses atomic operations to ensure cooperation among threads Mutual exclusion: ensures one thread can do something without the interference of other threads Critical section: a piece of code that only one thread can execute at a time

More on Critical Section A lock prevents a thread from doing something A thread should lock before entering a critical section A thread should unlock when leaving the critical section A thread should wait if the critical section is locked Synchronization often involves waiting

Too Much Milk: Solution 1 Two properties: Only one robot will go get milk Someone should go get the milk if needed Basic idea of solution 1 Leave a note (kind of like a lock) Remove the note (kind of like a unlock) Don’t go get milk if the note is around (wait)

Too Much Milk: Solution 1 if (no milk) { if (no note) { // leave a note; // go get milk; // remove the note; }

Too Much Milk: Solution 1 Dumb 10:00 if (no milk) { Dumber

Too Much Milk: Solution 1 Dumb 10:00 if (no milk) { Dumber 10:01 if (no milk) {

Too Much Milk: Solution 1 Dumb 10:00 if (no milk) { Dumber 10:01 if (no milk) { 10:02 if (no note) {

Too Much Milk: Solution 1 Dumb 10:00 if (no milk) { 10:03 if (no note) { Dumber 10:01 if (no milk) { 10:02 if (no note) {

Too Much Milk: Solution 1 Dumb 10:00 if (no milk) { 10:03 if (no note) { 10:04 // leave a note Dumber 10:01 if (no milk) { 10:02 if (no note) {

Too Much Milk: Solution 1 Dumb 10:03 if (no note) { 10:04 // leave a note Dumber 10:01 if (no milk) { 10:02 if (no note) { 10:05 // leave a note

Too Much Milk: Solution 1 Dumb 10:03 if (no note) { 10:04 // leave a note 10:06 // go get milk Dumber 10:02 if (no note) { 10:05 // leave a note

Too Much Milk: Solution 1 Dumb 10:03 if (no note) { 10:04 // leave a note 10:06 // go get milk Dumber 10:05 // leave a note 10:07 // go get milk

Too Much Milk: Solution 2 Okay…solution 1 does not work The notes are posted too late… What if both robots begin by leaving their own notes?

Too Much Milk: Solution 2 // leave a note; if (no note from the other) { if (no milk) { // go get milk; } // remove the note;

Too Much Milk: Solution 2 Dumb 10:00 // leave a note Dumber

Too Much Milk: Solution 2 Dumb 10:00 // leave a note Dumber 10:01 // leave a note

Too Much Milk: Solution 2 Dumb 10:00 // leave a note 10:02 if (no note from Dumber) {…} Dumber 10:01 // leave a note

Too Much Milk: Solution 2 Dumb 10:00 // leave a note 10:02 if (no note from Dumber) {…} Dumber 10:01 // leave a note 10:03 if (no note from Dumb) {…}

Too Much Milk: Solution 2 Dumb 10:00 // leave a note 10:02 if (no note from Dumber) {…} 10:04 // remove the note Dumber 10:01 // leave a note 10:03 if (no note from Dumb) {…}

Too Much Milk: Solution 2 Dumb 10:00 // leave a note 10:02 if (no note from Dumber) {…} 10:04 // remove the note Dumber 10:01 // leave a note 10:03 if (no note from Dumb) {…}

Too Much Milk: Solution 2 Dumb 10:02 if (no note from Dumber) {…} 10:04 // remove the note Dumber 10:01 // leave a note 10:03 if (no note from Dumb) {…} 10:05 // remove the note

Too Much Milk: Solution 2 Dumb 10:02 if (no note from Dumber) {…} 10:04 // remove the note Dumber 10:01 // leave a note 10:03 if (no note from Dumb) {…} 10:05 // remove the note

Too Much Milk: Solution 3 Dumb // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { if (no milk) { // go get milk } // remove Dumber’s note

Too Much Milk Solution 3 How do we verify the correctness of a solution? Test arbitrary interleaving of locking and checking locks In this case, leaving notes and checking notes

Dumber Challenges Dumb: Case 1 // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note Time

Dumber Challenges Dumb: Case 2 // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note Time

Dumber Challenges Dumb: Case 3 // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note Time

Dumb Challenges Dumber: Case 1 // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { if (no milk) { // go get milk } // remove Dumber’s note Time

Dumb Challenges Dumber: Case 2 // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note Time

Dumb Challenges Dumber: Case 3 // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note Dumber // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note Time

Lessons Learned Although it works, Solution 3 is ugly Difficult to verify correctness Two threads have different code Difficult to generalize to N threads While Dumb is waiting, it consumes CPU time (busy waiting) More elegant with higher-level primitives lockacquire(); if (no milk) { // go get milk } lockrelease();