Classic Synchronization Problems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
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.
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 ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Classical Problems of Concurrency
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
Process Synchronization
IPC and Classical Synchronization Problems
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Classical IPC and Synchronization Problems CS 342 – Operating Systems Ibrahim.
Semaphores The producer-consumer problem using semaphores.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.
Synchronisation Examples
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Reference –text: Tanenbaum ch
Interprocess Communication Race Conditions
Dining Philosophers Five philosophers sit around a table
Semaphores Reference text: Tanenbaum ch
Process Synchronization: Semaphores
Classical Synchronization Problems
2.4 Classic IPC Problems Dining philosophers Readers and writers
Lecture 13: Producer-Consumer and Semaphores
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Process Synchronization
Process Synchronization
Critical section problem
Lecture 13: Producer-Consumer and Semaphores
Semaphores Reference text: Tanenbaum ch
Synchronization, Part 2 Semaphores
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Classic Synchronization Problems

Readings Silberschatz et al , Section 6.6

Classic Synchronization Problems Producer-Consumer Readers-Writer Dining Philosopher Sleeping Barber

Producer-Consumer Problem Bounded buffer: size N Producer process writes data to buffer Should not write more than N items Writes to In and moves rightwards Consumer process reads data from buffer Should not try to consume if there is no data Reads from Out and moves rightwards Buffer is circular Out In

Producer-Consumer Problem Some applications: Compiler’s output consumed by assembler Web server produces data consumed by client’s web browser Example: pipe ( | ) in Unix cat file | more prog | sort

Desired Solution The consumer and producer should not access the same slot The consumer should not have to “busy wait” while waiting for the same slot

PCP Preliminaries Functions produce_item(): Producer calls this to generate something to put in buffer Needs to determine next available slot Needs to write remove_item(): Consumer calls to take item from buffer consume_item(): Consumer calls this to do something with item

PCP Preliminaries down, up: semaphore operations Assume that the buffer has N slots The variable count is used to keep track of the number of items in the buffer Shared entities: the buffer and the variable count, In, Out It isn’t always necessary to lock out both consumer and producer. The producer could be adding at one point in the queue and the consumer at another point.

PCP – Solution 1 This solution will make use of 2 semaphores that are initialized as follows: empty = N full = 0

PCP – Solution 1 Producer Code Consumer Code void producer (void) { int item; while (TRUE) { item = produce_item(); down(empty); insert_item(item); up(full); } void consumer (void) { int item; while (TRUE) { down(full); item = remove_item(); up(empty); consume_item(); } The produce and consumer should not be accessing the same slot……but they should be able to access the buffer If the buffer is full then the producer should not be able to add anything. If the buffer is empty then the consumer should not be able do to anything. The producer should only put something into the queue as long as there as an empty slot…... Consumer should not be trying to remove an item unless there is something in the buffer

PCP Solution 1 Say the consumer is scheduled first Out In empty = N full = 0 Say the consumer is scheduled first Since full is 0 the down(full) operation causes the consumer to block

PCP Solution 1 empty = N-1 full = 0 Out In Consumer is scheduled out; Producer is now using the CPU Producer executes down(empty) Producer puts in an item

PCP Solution 1 Producer executes up(full) empty = N-1 full = 1 Out In Producer executes up(full) full is now greater than 0 meaning that the consumer is not blocked

PCP Solution 1 Consumer is allowed access to the buffer empty = N-1 full = 0 Out In Consumer is allowed access to the buffer The value of full is 0 again.

PCP Solution 1 Consumer now executes up(empty) empty = N full = 0 Out In Consumer now executes up(empty)

PCP Solution 1 Looks like we have a solution Tadah! Don’t celebrate yet! What if you have multiple producers?

PCP – Solution 1 Consider the following scenario: What now? Two producers apply the down(empty) operation One of the producers determines the next empty slot and is then scheduled out The second producer determines the next empty slot and gets the same result as the first producer Both producers write to the same slot What now?

PCP – Solution 2 Our next solution will make use of 3 semaphores that are initialized as follows: mutex = 1 empty = N full = 0

PCP – Solution 2 Producer Code Consumer Code void producer (void) { int item; while (TRUE) { item = produce_item(); down(empty); down(mutex); insert_item(item); up(mutex); up(full); } void consumer (void) { int item; while (TRUE) { down(full) down(mutex); item = remove_item(); up(mutex); up(empty); consume_item(); } If you do not use the mutex semaphore then there could be two producers that implement insert_item(item).

PCP Solution 2 Say the consumer is scheduled first Out In mutex=1 empty = N full = 0 Say the consumer is scheduled first Since full is 0 the down(full) operation causes the consumer to block

PCP Solution 2 Now comes a producer The producer executes down(empty) Out In mutex=1 empty = N-1 full = 0 Now comes a producer The producer executes down(empty) It is allowed in

PCP Solution 2 The producer executes down(mutex); empty = N-1 full = 0 Out In The producer executes down(mutex); It is allowed to put something into the buffer Can anyone else do something to the buffer?

PCP Solution 2 mutex=1 empty = N-1 full = 1 Out In When the producer leaves it executes the up(mutex) operation It then executes the up(full) operation Why not execute down(empty)?

PCP Solution 2 full is now 1 which is greater than 0 mutex=1 empty = N-1 full = 1 Out In full is now 1 which is greater than 0 Consumer is no longer blocked What happens to the value of full?

PCP Solution 2 Consumer executes down(mutex) empty = N-1 full = 0 Out In Consumer executes down(mutex) Enters the critical section Consumes its item

PCP Solution 2 Consumer executes up(mutex) Consumer executes up(empty) empty = N full = 0 Out In Consumer executes up(mutex) Consumer executes up(empty)

Readers-Writers Problem Many processes (readers) share a database Some processes (writers) write to the database Only one writer can be active at a time and must have mutual exclusive access Any number of readers can be active simultaneously This problem is non-preemptive Wait for process in critical section to exit Models database access

Readers-Writers Problem Need to keep track of the number of readers current accessing data or wanting to access data Let us denote this by rc Writers have exclusive access to the shared data

Readers-Writers Problem There is a shared variable, rc, used to count the number of processes reading or wanting to read There are two semaphores initialized as follows mutex is initialized to 1 Used to control access to rc db is initialized to 1 Used to control access to database

Readers-Writers Problem void reader(void) { while (TRUE){ down(&mutex); rc++; if (rc == 1) down(&db); up(&mutex); read_database(); down (&mutex); rc--; if (rc == 0) up(&db); use_data_read(); } void write(void) { while (TRUE){ think_up_data(); down(&db); write_data_base(); up(&db); }

Readers-Writers Problem Let us assume 5 readers: R1, R2, R3, R4, R5 3 writers: W1,W2,W3 Initially no one is trying to use the database

Readers-Writers Problem R1 is the first to read the database It finds rc is 0 and increments rc by 1 The condition rc == 1 is true down(&db) is executed R1 proceeds Executes up(mutex) Gets access to the database

Readers-Writers Problem R2 comes along Executes down(mutex) Does it block? No What is the value of rc when it evaluates the condition (rc == 1) rc is 2 The condition rc == 1 is false Executes up(mutex) Gets access to the database

Readers-Writers Problem W1 comes along Can W1 access the database? No Why? What is the value of db? It is 0 W1 waits R1 is done reading the database Decrements rc so that rc is now 1 W1 still can’t write

Readers-Writers Problem When can W1 write? When R2 is done reading and thus it executes up(db) Now let’s say W1 is writing and that W2 wants to write It gets blocked at down(db) R3 wants to read Let’s say R4 wants to read while R3 is blocked at down(db)? Where does R4 block?

Readers-Writers Problem W1 finishes It executes up(db) Who goes first R3 or W2? W2 What if R3 arrived before W2 while W1 was executing? Who would go first? R3

Readers-Writers Problem What if the order of arrival (while W1 is writing) is executing is R3, W2, R4? R3, R4 would be able to read the database before W2. Doesn’t seem fair No. It turns out it is very difficult to be completely fair. One solution: Suspend readers that come after a writer Reduces concurrency

Readers-Writers Problem If there is a writer First reader blocks on if (rc == 1) down(&db); Other readers block on mutex Once a writer exits, all readers get to go through The last reader to exit signals a writer with if (rc == 0) up(&db)

Dining Philosopher’s Problem Formulated in 1965 by Edsger Dikstra Student exam exercise Used to model computers competing for access to tape drive peripherals. Used to model accesses to different data sets The dining philosophers problem is useful for modeling processes that are competing for exclusive access to a limited number of resources

Dining Philosopher’s Problem 5 Philosophers around a table and 5 forks Philosophers eat (spaghetti)/think Eating needs 2 forks Pick one fork at a time (first the right fork then the left one and eat)

Dining Philosophers 5 Philosophers around a table and 5 forks Philosophers eat (spaghetti)/think Eating needs 2 forks Spaghetti is very slippery Pick one fork at a time (first the right fork then the left one and eat) 1 2 3 4

Dining Philosophers Problem The challenge is to grant requests for forks while avoiding deadlock and starvation. Deadlock can occur if everyone tries to get their forks at once. Each gets a left forks, and is stuck, because each right forks is someone else’s left fork.

Dining Philosophers non-Solution The procedure take_fork waits until the specified fork is available and takes it A philosopher takes the left fork and then the right fork Suppose that all philosophers take the left fork at once? Not really serious void philosopher(int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); put_fork(i); put_fork((i+1)%N); }

Dining Philosophers Solution

Dining Philosophers Solution You need two forks – either take none or both The test(i) function is used to determine if you can take both If you can’t take both then block yourself – a philosopher is is blocked needs to be signalled when other philisophers are done

Dining Philosophers Solution State S 5 philosophers are initially in the thinking state (indicated by T in the State array) A philosopher may move into eating state only if neither neighbour is eating A philosopher blocks if the needed forks are busy

Dining Philosophers Solution State S Assume P0 wants to eat It calls take_forks(0) Records that it is hungry

Dining Philosophers Solution State 1 S P0 calls test(0) (within take_forks(0)) P1 and P4 are in thinking state P0 can enter the eat state P0 executes the up(s[0]) operation in test(0)

Dining Philosophers Solution State S P0 executes the down(s[0]) operation in take_forks(0) P0 is now eating

Dining Philosophers Solution State S While P0 is eating, P1 decides it wants to eat It calls take_forks (1) Records that it is hungry

Dining Philosophers Solution State S P1 calls test(1) (within take_forks(1)) P0 is in an eating state; P2 is in a thinking state P1 is not successful in getting forks when it leaves test(1) P1 executes the down(s[1]) operation in take_forks(1) P1 is blocked

Dining Philosophers Solution State S What will unblock P1?

Dining Philosophers Solution State S P0 finishes eating P0 calls put_forks() P0 is put into the thinking state

Dining Philosophers Solution State S P0 calls test(P4) Does not change anything – P4 is not hungry P0 calls test(P1) Executes up(s[1]) P1 was blocked –it isn’t any more so P1 can eat

Dining Philosophers Solution State S What if P0 and P2 were eating when P1 got hungry? The state of the arrays are above

Dining Philosophers Solution State S P0 stops eating P0 calls test(P1) P1 cannot still cannot eat since state[RIGHT] indicates that P2 is eating. Have to wait until P2 calls test(P1)

Dining Philosopher’s Solution How many philosophers can be eat at once?

The Sleeping Barber Problem N customer chairs (waiting chairs) One barber who can cut one customer’s hair at any time No waiting customer => barber sleeps Customer enters => If all waiting chairs full, customer leaves Otherwise, if barber asleep, wake up barber and make him work Otherwise, barber is busy – wait in a chair

The Sleeping Barber Solution Global variable: waiting Use the mutex semaphore to control access Signal to barber that it can sleep Use the customer semaphore Signal to customer to wakeup if all barbers are busy Use the barber semaphore

Sleeping Barber Problem Solution

Conclusions We have examined several non-trivial synchronization problems Producer-Consumer Problem Reader-Writer Problem Dining Philosophers Problem Sleeping Barber’s Problem