Producer-Consumer Problem David Monismith cs550 Operating Systems.

Slides:



Advertisements
Similar presentations
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Advertisements

Ch 7 B.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
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.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Classical Problems of Concurrency
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Chapter 5 Concurrency. 2 Concurrency 3 4 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int *i) { if (*i == 0) { *i.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 6: Process Synchronization.
CS3530 OPERATING SYSTEMS Summer 2014 Synchronization Chapter 6.
CS 346 – Sect Process synchronization –What is the problem? –Criteria for solution –Producer / consumer example –General problems difficult because.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
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.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
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.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization.
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.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Synchronization Hank Levy 1.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
Chapter 6 Synchronization Principles
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
, Part II Process Synchronization
Problem: CFQ (Circular Fifo Queue)
Process/Thread Synchronization (Part 2)
Presentation transcript:

Producer-Consumer Problem David Monismith cs550 Operating Systems

Bounded-Buffer or Producer- Consumer Problem Involves two or more processes In its simplest form we have: – 1 producer that creates data items and puts them in a buffer (a shared memory location) – 1 consumer that removes data items from the buffer and consumes them

Example (Consumer) | V | X | X | | | | | | | ^ | (Producer)

Producer-Consumer Facts Both need access to the shared buffer. Restrictions – Producer can't put items (X's) in the buffer when it is full – Consumer can't remove items (X's) from the buffer when it is empty – Operations to insert and remove data items (X's) are mutually exclusive

Need for Semaphores One called full for the number of full slots (counting) One called empty for the number of empty slots (counting) One binary semaphore s for mutual exclusion

Producer Pseudocode while(true) produce an item empty.acquire() // Decrease the // # of empty slots s.acquire() //get the semaphore // put an item in the buffer s.release() //release semaphore full.release() // Increase the // # of full slots end while

Consumer Pseudocode while(true) full.acquire() //decrease the //# of full slots s.acquire() //get the semaphore //remove an item from the buffer s.release() //release the //semaphore empty.release()//increase the number of empty slots end while

Producer-Consumer Demo See class website

Readers/Writers Problem Another classic synchronization problem 2 types of processes Readers - read data only Writers - need access to data resource to update data

Readers-Writers Problem Satisfy the following conditions: – Any number of readers processes can access shared data simultaneously – If writer process has access to shared data, it has exclusive access Need two levels of mutual exclusion – Individual mutually exclusive access to a shared resource – Group exclusive access to a shared resource Group of readers may access shared data object simultaneously, but a writer needs exclusive access.

Reader Priority Implementation Readers accessing resource block writes Readers accessing resource initially may read if other readers are accessing the resource Starvation is possible for writers Writer only has access if no readers are accessing the data

Writer Pseudocode wrt.acquire() //Get access to //the shared data // Write to the shared data // object. wrt.release()

Reader Pseudocode Two semaphores are needed Mutual exclusion (wrt semaphore) for readers only Mutual exclusion to protect the counter for the readers

Reader Pseudocode mutex.acquire() readcount++ if(readcount == 1) wrt.acquire() end if mutex.release() //read from shared data mutex.acquire() readcount— if(readcount == 0) wrt.release() //Last reader releases mutex end if mutex.release()

Writer Priority Can also have writer priority, but – Need two additional semaphores A shared variable for writers, or A priority queue as part of semaphore definition Giving higher priority to writers does not guarantee no new readers will be allowed access to the shared data if there is at least one writer waiting. Other (better) options exist, too.