Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Lecture 20: Synchronization & Consistency Topics: synchronization, consistency models (Sections )
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.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
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.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
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,
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.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
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.
More on Semaphores Andy Wang Operating Systems COP 4610 / CGS 5765.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
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.
Exam Review Andy Wang Operating Systems COP 4610 / CGS 5765.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
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?
6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations, Locks, Semaphores September 12, 2011 Anthony D. Joseph and.
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.
Thread Synchronization
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
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Designing Parallel Algorithms (Synchronization)
Sarah Diesburg Operating Systems COP 4610
Andy Wang Operating Systems COP 4610 / CGS 5765
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Race conditions and Synchronization
Implementing Mutual Exclusion
Chapter 6: Process Synchronization
Implementing Mutual Exclusion
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
EECE.4810/EECE.5730 Operating Systems
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

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: Dumb Robot: Dumber

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

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

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

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

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

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

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

Motivating Example: Too Much Milk Dumb 10:20Arrive with milk 10:25Go party Dumber 10:30Arrive 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 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) { } // remove Dumber’s note Time

Dumber Challenges Dumb: Case 2 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) { } // remove Dumber’s note Time

Dumber Challenges Dumb: Case 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) { } // remove Dumber’s note Time

Dumb Challenges Dumber: Case 1 Dumb // 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 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) { } // remove Dumber’s note Time

Dumb Challenges Dumber: Case 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) { } // 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();