Synchronization Methods Topics Mutual-exclusion methods Producer/consumer problem Readers/writers problem semaphores.ppt CS 105 “Tour of the Black Holes.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
Synchronization Methods Topics Mutual-exclusion methods Producer/consumer problem Readers/writers problem CS 105 “Tour of the Black Holes of Computing”
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Secure Operating Systems Lesson 5: Shared Objects.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Classic Synchronization Problems
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Classical Problems of Concurrency
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 2.3 : Interprocess Communication
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.
University of Pennsylvania 9/28/00CSE 3801 Concurrent Programming (Critical Regions, Monitors, and Threads) CSE 380 Lecture Note 6 Insup Lee.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
CS510 Concurrent Systems Introduction to Concurrency.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
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.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Synchronization Methods Topics Mutual-exclusion methods Producer/consumer problem Readers/writers problem CS 105 “Tour of the Black Holes of Computing”
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Synchronization Methods
Concurrency: Mutual Exclusion and Synchronization
Monitors Chapter 7.
Synchronization Methods
Critical section problem
Monitors Chapter 7.
Monitors Chapter 7.
Synchronization Methods
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Synchronization Methods
Presentation transcript:

Synchronization Methods Topics Mutual-exclusion methods Producer/consumer problem Readers/writers problem semaphores.ppt CS 105 “Tour of the Black Holes of Computing”

– 2 – CS 105 Mutual Exclusion Need ways to enforce critical sections Prevent race conditions that cause errors Requirements for mutual exclusion Safety: only one process/thread at a time inside CS Progress: if nobody has access and somebody wants in, somebody gets in No starvation: if you want in, you will eventually get in Desirable properties: Efficiency: can get into CS in relatively few instructions Low load: waiting for CS doesn’t waste resources Fairness: if you want in, nobody else gets in ahead of you twice

– 3 – CS 105 Additional Requirements Synchronization is tricky to get right Failure to protect critical sections Incorrect use of primitives Deadlock Programmer-friendliness is big plus

– 4 – CS 105 Hardware Mutex Support Test and Set Read word, set it nonzero, and set condition codes All in one indivisible operation Compare and Swap Read word, compare to register, store other register into word Again, indivisible Generalization of Test & Set

– 5 – CS 105 Example of Test and Set enter_critical_region: leallock, %eax.L1:tsl(%eax); Set lock NZ, set CC jne.L1; Loop if was already NZ ; We now have exclusive access retleave_critical_region: xor%eax, %eax movl%eax, lock ret

– 6 – CS 105 Evaluating Test and Set  Very fast entry to unlocked region  Easy to implement  Guarantees safety & progress  Wastes CPU when waiting (spin lock/busy wait)  Doesn’t make it easy for other threads to run  Extremely high memory (i.e., bus) traffic  Prone to errors (e.g., forget to unlock)  Prone to starvation For these reasons, test & set is used only to implement higher-level constructs.

– 7 – CS 105 Semaphores Higher-level construct, discussed previously Invented by Edsger Dijkstra P(sem) or wait(sem) decrements and possibly waits V(sem) or signal(sem) increments and lets somebody else in Usually implemented by operating system Allows scheduler to run different thread while waiting OS can guarantee fairness and no starvation Or can even enforce priority scheme More flexibility for user (e.g., can count things) Still error-prone P’s and V’s must be matched Single extra V blows mutual exclusion entirely (compare Test & Set)

– 8 – CS 105 Problems in Synchronization Many standard problems in concurrent programming Producer/consumer Readers/writers Dining philosophers Drinking philosophers Etc. Standard problems capture common situations Also give way to evaluate proposed synchronization mechanisms

– 9 – CS 105 Monitors High-level mutual-exclusion construct Invented by C.A.R. “Tony” Hoare Difficult or impossible to use incorrectly Like Java/C++ class: combines data with functions needed to manage it Keys to monitor correctness Data is available only to functions within monitor Specific functions (gatekeepers) control access Only one process/thread allowed inside monitor at a time Queues keep track of who is waiting for monitor Turns out to be hard to do certain things with monitors Programmers wind up standing on heads or implementing things like semaphores

– 10 – CS 105 The Producer/Consumer Problem Two processes communicate Producer generates things (e.g., messages) into a buffer Consumer takes those things and uses them Correctness requirements Producer must wait if buffer is full Consumer must not extract things from empty bufferSolutions Can be done with just load/store (but tricky) We have seen simple semaphore-based solution Perfect application for monitors

– 11 – CS 105 Producer/Consumer with Semaphores semaphore mutex = 1, hasData = 0, hasRoom = N; hasRoom = N; void producer() { while(1) { message nextItem = produceItem(); P(&hasRoom);P(&mutex);enterInBuffer(nextItem);V(&mutex);V(&hasData);}}

– 12 – CS 105 Producer/Consumer with Semaphores (continued) void consumer() { while(1) { P(&hasData);P(&mutex); message nextItem = removeFromBuffer(); V(&mutex);V(&hasRoom);consumeItem(nextItem);}}

– 13 – CS 105 Producer/Consumer with Monitors monitor producerconsumermonitor; var buffer[0..slots-1] of message; slotsinuse: 0..slots; nexttofill, nexttoempty: 0..slots-1; bufferhasdata, bufferhasspace: condition; procedure fillslot(var data: message) begin if slotsinuse = slots; then wait(bufferhasspace); buffer[nexttofill] := data; nexttofill := (nexttofill + 1) mod slots; slotsinuse := slotsinuse + 1; slotsinuse := slotsinuse + 1;signal(bufferhasdata);end;

– 14 – CS 105 Producer/Consumer with Monitors (continued) procedure emptyslot(var data: message) begin if slotsinuse = 0; then wait(bufferhasdata); data := buffer[nexttoempty]; nexttoempty = (nexttoempty + 1) mod slots; slotsinuse := slotsinuse – 1; slotsinuse := slotsinuse – 1;signal(bufferhasspace);end;begin slotsinuse := 0; nexttofill := 0; nexttoempty := 0; end;

– 15 – CS 105 The Readers/Writers Problem More complex than producer/consumer Many processes accessing single resource Some read, some write (some could do both) OK for many to read at once No danger of stepping on each others’ feet Only one writer allowed at a timeExamples: Shared access to file Multiple producers, multiple consumers

– 16 – CS 105 Readers/Writers with Semaphores (Polling Version) semaphore mutex = 1; int nreaders = 0, nwriters = 0; void reader() { while (1) { P(mutex); while (nwriters != 0) { V(mutex);pause();P(mutex);}nreaders++;V(mutex);read();P(mutex);nreaders--;V(mutex);}}

– 17 – CS 105 Readers/Writers with Semaphores (Polling continued) void writer() { while (1) { P(mutex); while (nreaders + nwriters != 0) { V(mutex);pause();P(mutex);}nwriters++;V(mutex);write();P(mutex);nwriters--;V(mutex);}}

– 18 – CS 105 Readers/Writers with Semaphores (Polling continued) What are the drawbacks of this approach? How can we write a non-polling version?

– 19 – CS 105 Readers/Writers with Monitors monitor readersandwriters; var readers: integer; someonewriting: boolean; readallowed, writeallowed: condition; procedure beginreading begin if someonewriting or queue(writeallowed) then wait(readallowed); readers := readers + 1; signal(readallowed);end; procedure donereading begin readers := readers – 1; if readers = 0 then signal(writeallowed); end;

– 20 – CS 105 Readers/Writers with Monitors (continued) procedure beginwriting begin if readers ¬= 0 or someonewriting then wait(writeallowed); someonewriting := true; end; procedure donewriting begin someonewriting := false; if queue(readallowed) then signal(readallowed); else signal(writeallowed); end;begin readers := 0; someonewriting := false; end;

– 21 – CS 105 Readers/Writers with Monitors Characteristics of solution No starvation Arriving readers wait if writer is waiting Group of readers runs after each writer Arrival order of writer, writer, reader runs in different order Requires several auxiliary variables

– 22 – CS 105 Deadlock Consider following program: semaphore a = 1, b = 1; void func1() {P(a);P(b);do_something();V(a);V(b);} void func2() {P(b);P(a);do_something_else();V(a);V(b);}

– 23 – CS 105 Dining Philosophers Models many important synchronization problems Most famous concurrency problem Posed by Dijkstra Characteristics Five philosophers alternate thinking and eating Only food is spaghetti Requires two forks Each philosopher has assigned seat at round table One fork between each pair of plates Problem: control access to forks, such that every can eat Note that “pick up left, then pick up right” doesn’t work Solvable with semaphores

– 24 – CS 105 Drinking Philosophers Extension of dining philosophers Arbitrary number of philosophers Each likes own drink, mixed from bottles on table Can only mix drink when holding all bottles Each drink uses different subset of bottles Problem: control access to bottles, such that there is no deadlock and no starvation