15-410, F’04 - 1 - Synchronization #1 Sep. 17, 2004 Dave Eckhardt Bruce Maggs L08b_Synch 15-410 “My computer is 'modern'!”

Slides:



Advertisements
Similar presentations
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
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 ©2013 Operating System Concepts – 9 th Edition Chapter 5: 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.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
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.
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.
CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
The Critical-Section Problem
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency in Distributed Systems: Mutual exclusion.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
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.
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.
The Critical Section Problem
1 Synchronization (1) Dave Eckhardt
Concurrency, Mutual Exclusion and Synchronization.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Critical Sections with lots of Threads. Refresher: Deker’s Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true;
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Dave Eckhardt Synchronization (2) Dave Eckhardt
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Designing Parallel Algorithms (Synchronization)
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
The Critical-Section Problem
Introduction to Cooperating Processes
Module 7a: Classic Synchronization
Lecture 20 Syed Mansoor Sarwar
Synchronization and Semaphores
Lecture 2 Part 2 Process Synchronization
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

15-410, F’ Synchronization #1 Sep. 17, 2004 Dave Eckhardt Bruce Maggs L08b_Synch “My computer is 'modern'!”

15-410, F’ Synchronization Project 0 feedback plan First step: red ink on paper Soon: scores (based mostly on test outcomes) Project 1 alerts “make print” must work Please check for completeness Doxygen documenation must build Please check for completeness

15-410, F’ Project 0 Common Themes Style/structure Integers instead of #defined tokens “2” is not better than “TYPE_DOUBLE” It is much much much worse Don't ever do that “Code photocopier” - indicates a problem, often serious Bad variable/function names initialize() should not terminate int i; /* number of frogs */  Don't apologize; fix the problem! Excessively long functions while(1) should be rare Don't make us read... False comments, dead code, extra copies of code Harry Bovik did not help you write your P0

15-410, F’ Project 0 Common Themes Style/structure Code is read by people Us Your partner Your manager... Don't make it painful for us or else...

15-410, F’ Project 0 Common Themes Robustness Not checking syscall returns (e.g., tmpfile()) Not finding last function / not handing unnamed function Memory leak (no need for malloc() at all!) File-descriptor leak

15-410, F’ Project 0 Common Themes Not following spec Hand-verifying addresses (compare vs. 0x xc000...) Approximating arg-offset info Instead of getting it from the table!! Give up via exit()  caller never authorized that! Stopping trace at hard-coded function name Semantic mismatch \b is a “backspace character” Clever hack to “undo” a comma in the output stream? Only when the output stream is a terminal!!! Instead of fixing the wrong thing, do the right thing

15-410, F’ Outline Me vs. Chapter 7 – Mind your P's and Q's – Atomic sequences vs. voluntary de-scheduling ● “Sim City” example – You will need to read the chapter – Hopefully my preparation/review will clarify it

15-410, F’ Outline An intrusion from the “real world” Two fundamental operations Three necessary critical-section properties Two-process solution N-process “Bakery Algorithm”

15-410, F’ Mind your P's and Q's What you write choosing[i] = true; number[i] = max(number[0], number[1],...) + 1; choosing[i] = false; What happens... number[i] = max(number[0], number[1],...) + 1; choosing[i] = false;

15-410, F’ Mind your P's and Q's What you write choosing[i] = true; number[i] = max(number[0], number[1],...) + 1; choosing[i] = false; Or maybe this happens... choosing[i] = false; number[i] = max(number[0], number[1],...) + 1; “Computer Architecture for $200, Dave”...

15-410, F’ My computer is broken?! No, your computer is "modern" – Processor "write pipe" queues memory stores –...and coalesces "redundant" writes!Crazy? – Not if you're pounding out pixels!

15-410, F’ My computer is broken?! Magic "memory barrier" instructions available... –...stall processor until write pipe is empty Ok, now I understand – Probably not! ● ● “Double-Checked Locking is Broken” Declaration – See also "release consistency" Textbook's memory model –...is “what you expect” – Ok to use simple model for homework, exams

15-410, F’ Synchronization Fundamentals Two fundamental operations – Atomic instruction sequence – Voluntary de-scheduling Multiple implementations of each – Uniprocessor vs. multiprocessor – Special hardware vs. special algorithm – Different OS techniques – Performance tuning for special cases Be very clear on features, differences

15-410, F’ Synchronization Fundamentals Multiple client abstractions Textbook covers – Semaphore, critical region, monitor Very relevant – Mutex/condition variable (POSIX pthreads) – Java "synchronized" keyword (3 uses)

15-410, F’ Synchronization Fundamentals Two Fundamental operations  Atomic instruction sequence Voluntary de-scheduling

15-410, F’ Atomic instruction sequence Problem domain – Short sequence of instructions – Nobody else may interleave same sequence ● or a "related" sequence – “Typically” nobody is competing

15-410, F’ Non-interference Multiprocessor simulation (think: “Sim City”) – Coarse-grained “turn” (think: hour) – Lots of activity within turn – Think: M:N threads, M=objects, N=#processors Most cars don't interact in a game turn... – Must model those that do!

15-410, F’ Commerce

15-410, F’ Commerce – Observations Instruction sequences are “short” – Ok to force competitors to wait Probability of collision is "low" – Many non-colliding invocations per second – Must not use an expensive anti-collision approach! ● Oh, just make a system call... – Common (non-colliding) case must be fast

15-410, F’ Synchronization Fundamentals Two Fundamental operations Atomic instruction sequence  Voluntary de-scheduling

15-410, F’ Voluntary de-scheduling Problem domain – “Are we there yet?” – “Waiting for Godot” Example - "Sim City" disaster daemon while (date < ) cwait(date); while (hour < 5) cwait(hour); for (i = 0; i < max_x; i++) for (j = 0; j < max_y; j++) wreak_havoc(i,j);

15-410, F’ Voluntary de-scheduling Anti-atomic – We want to be “interrupted” Making others wait is wrong – Wrong for them – we won't be ready for a while – Wrong for us – we can't be ready until they progress We don't want exclusion We want others to run - they enable us CPU de-scheduling is an OS service!

15-410, F’ Voluntary de-scheduling Wait pattern LOCK WORLD while (!(ready = scan_world())){ UNLOCK WORLD WAIT_FOR(progress_event) } Your partner-competitor will SIGNAL(progress_event)

15-410, F’ Standard Nomenclature Textbook's code skeleton / naming do { entry section critical section:...computation on shared state... exit section remainder section:...private computation... } while (1);

15-410, F’ Standard Nomenclature What's muted by this picture? What's in that critical section? – Quick atomic sequence? – Need for a long sleep? For now... – Pretend critical section is brief atomic sequence – Study the entry/exit sections

15-410, F’ Three Critical Section Requirements Mutual Exclusion – At most one process executing critical sectionProgress – Choosing next entrant cannot involve nonparticipants – Choosing protocol must have bounded time Bounded waiting – Cannot wait forever once you begin entry protocol –...bounded number of entries by others

15-410, F’ Notation For 2-Process Protocols Process[i] = “us” Process[j] = “the other process” i, j are process-local variables – {i,j} = {0,1} – j == 1 – i This notation is “odd” – But it may well appear in an exam question

15-410, F’ Idea #1 - “Taking Turns” int turn = 0; while (turn != i) ;...critical section... turn = j; Mutual exclusion - yes Progress - no – Strict turn-taking is fatal – If P[i] never tries to enter, P[j] will wait forever

15-410, F’ Idea #2 - “Registering Interest” boolean want[2] = {false, false}; want[i] = true; while (want[j]) ;...critical section... want[i] = false; Mutual exclusion – yes Progress - almost

15-410, F’ Failing “Progress”

15-410, F’ “Taking Turns When Necessary” Rubbing two ideas together boolean want[2] = {false, false}; int turn = 0; want[i] = true; turn = j; while (want[j] && turn == j) ;...critical section... want[i] = false;

15-410, F’ Proof Sketch of Exclusion Both in c.s. implies want[i] == want[j] == true Thus both while loops exited because “turn != j” Cannot have (turn == 0 && turn == 1) – So one exited first w.l.o.g., P0 exited first – So turn==0 before turn==1 – So P1 had to set turn==0 before P0 set turn==1 – So P0 could not see turn==0, could not exit loop first!

15-410, F’ Proof Sketch Hints

15-410, F’ Bakery Algorithm More than two processes? – Generalization based on bakery/deli counter ● Get monotonically-increasing ticket number from dispenser ● Wait until monotonically-increasing “now serving” == you Multi-process version – Unlike “reality”, two people can get the same ticket number – Sort by (ticket number, process number)

15-410, F’ Bakery Algorithm Phase 1 – Pick a number – Look at all presently-available numbers – Add 1 to highest you can find Phase 2 – Wait until you hold lowest number – Not strictly true: processes may have same number – Use process-id as a tie-breaker ● (ticket 7, process 45) < (ticket 7, process 99) – Your turn when you hold lowest (t,pid)

15-410, F’ Bakery Algorithm boolean choosing[n] = { false,... }; int number[n] = { 0,... } ;

15-410, F’ Bakery Algorithm Phase 1: Pick a number choosing[i] = true; number[i] = max(number[0], number[1],...) + 1; choosing[i] = false; Worst case: everybody picks same number! But at least latecomers will pick a larger number...

15-410, F’ Bakery Algorithm Phase 2: Sweep “proving” we have lowest number for (j = 0; j < n; ++j) { while (choosing[j]) ; while ((number[j] != 0) && ((number[j], j) < (number[i], i))) ; }...critical section... number[i] = 0;

15-410, F’ Summary Memory is weird Two fundamental operations - understand! – Brief exclusion for atomic sequences – Long-term yielding to get what you want Three necessary critical-section properties Understand these race-condition parties! Two-process solution N-process “Bakery Algorithm”