Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Module 6: Process Synchronization
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Producer-Consumer One common situation: Producers and consumers communicate via a buffer.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Classic Synchronization Problems
Ken Birman 1. Synchronization paradigms We’ve looked at critical sections Really, a form of locking When one thread will access shared data, first it.
Classical Problems of Concurrency
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Race Conditions Critical Sections Dekker’s Algorithm.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
IPC and Classical Synchronization Problems
Synchronization April 14, 2000 Instructor Gary Kimura Slides courtesy of Hank Levy.
Synchronization..or: the trickiest bit of this course.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
© 2004, D. J. Foreman 1 Mutual Exclusion © 2004, D. J. Foreman 2  Mutual exclusion ■ Critical sections ■ Primitives  Implementing it  Dekker's alg.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
Classical Synchronization Problems. Announcements CS 4410 #1 grades and solutions available in CMS soon. –Average 54, stddev 9.7 –High of 70. –Score out.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Monitor Solutions to Classical Problems. 2 Announcements CS 415 Projects graded. –Mean 80.7, High 90 out of 90 CS 414 Homework due Monday.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6(b): Synchronization.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Producer-Consumer Problem David Monismith cs550 Operating Systems.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Homework-6 Questions : 2,10,15,22.
Synchronization..or: the trickiest bit of this course.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Interprocess Communication Race Conditions
CS703 - Advanced Operating Systems
Process Synchronization: Semaphores
Background on the need for Synchronization
Concurrent Processes.
CSE451 Basic Synchronization Spring 2001
The Critical-Section Problem (Two-Process Solution)
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Chapter 6 Synchronization Principles
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
CIS 720 Lecture 6.
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Classical Synchronization Problems

Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread will access shared data, first it gets a kind of lock –This prevents other threads from accessing that data until the first one has finished –We saw that semaphores make it easy to implement critical sections

Reminder: Critical Section Classic notation due to Dijkstra: Semaphore mutex = 1; CSEnter() { P(mutex); } CSExit() { V(mutex); } Other notation (more familiar in Java): CSEnter() { mutex.wait(); } CSExit() { mutex.signal(); }

Bounded Buffer This style of shared access doesn’t capture two very common models of sharing that we would also like to support Bounded buffer: –Arises when two or more threads communicate with some threads “producing” data that others “consume”. –Example: preprocessor for a compiler “produces” a preprocessed source file that the parser of the compiler “consumes”

Readers and Writers In this model, threads share data that some threads “read” and other threads “write”. Instead of CSEnter and CSExit we want –StartRead…EndRead; StartWrite…EndWrite Goal: allow multiple concurrent readers but only a single writer at a time, and if a writer is active, readers wait for it to finish

Producer-Consumer Problem Start by imagining an unbounded (infinite) buffer Producer process writes data to buffer –Writes to In and moves rightwards Consumer process reads data from buffer –Reads from Out and moves rightwards –Should not try to consume if there is no data OutIn Need an infinite buffer

Producer-Consumer Problem Bounded buffer: size ‘N’ –Access entry 0… N-1, then “wrap around” to 0 again Producer process writes data to buffer –Must not write more than ‘N’ items more than consumer “ate” Consumer process reads data from buffer –Should not try to consume if there is no data 01 In Out N-1

Producer-Consumer Problem A number of applications: –Data from bar-code reader consumed by device driver –Data in a file you want to print consumed by printer spooler, which produces data consumed by line printer device driver –Web server produces data consumed by client’s web browser Example: so-called “pipe” ( | ) in Unix > cat file | sort | uniq | more > prog | sort Thought questions: where’s the bounded buffer? How “big” should the buffer be, in an ideal world?

Producer-Consumer Problem Solving with semaphores –We’ll use two kinds of semaphores –We’ll use counters to track how much data is in the buffer One counter counts as we add data and stops the producer if there are N objects in the buffer A second counter counts as we remove data and stops a consumer if there are 0 in the buffer –Idea: since general semaphores can count for us, we don’t need a separate counter variable Why do we need a second kind of semaphore? –We’ll also need a mutex semaphore

Producer-Consumer Problem Shared: Semaphores mutex, empty, full; Init: mutex = 1; /* for mutual exclusion*/ empty = N; /* number empty buf entries */ full = 0; /* number full buf entries */ Producer do {... // produce an item in nextp... P(empty); P(mutex);... // add nextp to buffer... V(mutex); V(full); } while (true); Consumer do { P(full); P(mutex);... // remove item to nextc... V(mutex); V(empty);... // consume item in nextc... } while (true);

Readers-Writers Problem Courtois et al 1971 Models access to a database –A reader is a thread that needs to look at the database but won’t change it. –A writer is a thread that modifies the database Example: making an airline reservation –When you browse to look at flight schedules the web site is acting as a reader on your behalf –When you reserve a seat, the web site has to write into the database to make the reservation

Readers-Writers Problem Many threads share an object in memory –Some write to it, some only read it –Only one writer can be active at a time –Any number of readers can be active simultaneously Key insight: generalizes the critical section concept One issue we need to settle, to clarify problem statement. –Suppose that a writer is active and a mixture of readers and writers now shows up. Who should get in next? –Or suppose that a writer is waiting and an endless of stream of readers keeps showing up. Is it fair for them to become active? We’ll favor a kind of back-and-forth form of fairness: –Once a reader is waiting, readers will get in next. –If a writer is waiting, one writer will get in next.

Readers-Writers (Take 1) Shared variables: Semaphore mutex, wrl; integer rcount; Init: mutex = 1, wrl = 1, rcount = 0; Writer do { P(wrl);... /*writing is performed*/... V(wrl); }while(TRUE); Reader do { P(mutex); rcount++; if (rcount == 1) P(wrl); V(mutex);... /*reading is performed*/... P(mutex); rcount--; if (rcount == 0) V(wrl); V(mutex); }while(TRUE);

Readers-Writers Notes If there is a writer –First reader blocks on wrl –Other readers block on mutex Once a reader is active, all readers get to go through –Trick question: Which reader gets in first? The last reader to exit signals a writer –If no writer, then readers can continue If readers and writers waiting on wrl, and writer exits –Who gets to go in first? Why doesn’t a writer need to use mutex ?

Does this work as we hoped? If readers are active, no writer can enter –The writers wait doing a P(wrl) While writer is active, nobody can enter –Any other reader or writer will wait But back-and-forth switching is buggy: –Any number of readers can enter in a row –Readers can “starve” writers With semaphores, building a solution that has the desired back-and-forth behavior is really, really tricky! –We recommend that you try, but not too hard…