Presentation is loading. Please wait.

Presentation is loading. Please wait.

Producer-Consumer Problem David Monismith cs550 Operating Systems.

Similar presentations


Presentation on theme: "Producer-Consumer Problem David Monismith cs550 Operating Systems."— Presentation transcript:

1 Producer-Consumer Problem David Monismith cs550 Operating Systems

2 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

3 Example (Consumer) | V +-------------------------------+ | X | X | | | | | | | +-------------------------------+ ^ | (Producer)

4 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

5 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

6 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

7 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

8 Producer-Consumer Demo See class website

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

10 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.

11 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

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

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

14 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()

15 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.


Download ppt "Producer-Consumer Problem David Monismith cs550 Operating Systems."

Similar presentations


Ads by Google