0% found this document useful (0 votes)
52 views9 pages

10.producer, Consumer Problem

Uploaded by

Ghaffar Buzdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

10.producer, Consumer Problem

Uploaded by

Ghaffar Buzdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 7: Synchronization

Examples

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline

 Explain the bounded-buffer synchronization problem


 Explain the readers-writers synchronization problem
 Explain and dining-philosophers synchronization problems

Operating System Concepts – 10th Edition 7.2 Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
 Classical problems used to test newly-proposed
synchronization schemes
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem

Operating System Concepts – 10th Edition 7.3 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem

Operating System Concepts – 10th Edition 7.4 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem

 n is the size of the buffers


• n = 8; here the size of the buffer is eight.

 count how many items are there in the buffer


• count = 0; There is no item in the buffer.
 in is positioning the next slot of buffers, for the producer
 out is positioning the next slot of buffers, for the consumer

Operating System Concepts – 10th Edition 7.5 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem
Producer Consumer
int count = 0; //Global Variable N=8;
Buffer[0,1…..n-1]
Void consumer(void)
Void producer(void) count 0 {
{ int itemc;
int itemp; in 0 out while(True)
while(True) 0 {
1 0
{ while(count==0); /*Buffer empty*/
produce_item(itemp); 2 /*Buffer empty*/
/*function call to produce item*/
while(count==n); /*Buffer Full*/ 3 Buffer[out]= itemc;
Buffer[in]= itemp;//store item in buffer /*take item from buffer */
in = in+1%n; //position next slot 4

5 out = out+1%n;
//count++; //increment /*position next slot */
count = count+1; 6 //count--; //Decrement
} count = count-1;
} I : Load Rp, m[count]; 7 process-item(itemc);
II: INCR Rp;
III: store m[count], Rp; I : Load Rc, m[count];
} II: DECR Rc;
III: store m[count], Rc;
}
Operating System Concepts – 10th Edition 7.6 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem (Case)

 Case1(Best): For one value in the buffer


• The producer comes to produce items, and the
consumer comes to consume items. (Comes Serial
Mode, No-Preemption) [OK]
 Case 2 (Worst Scenario): Suppose, there are three (3)
values(items) in the buffer.
 Producer(exe I1,I2)PreemptionConsumer(exe I1,I2)Preemption
Producer(exe I3)Preemption Consumer(exe I3) [Terminate].

• The producer comes (executes I1, I2, saves PCB), and then a context switch
occurs due to an interrupt or high-priority process(Preemption).
• Then the consumer comes (executes I1, I2, saves PCB), Preemption occurs.
• Again producer comes(executes I3, process terminated).
• Again consumer comes(executes I3, process terminated).

• [Problem]

Operating System Concepts – 10th Edition 7.7 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem-(Best Case)
Producer Consumer
int count = 0; //Global Variable N=8;
Buffer[0,1…..n-1]
Void consumer(void)
Void producer(void) count 0 {
{ int itemc;
int itemp; in 0 out while(True)
while(True) 0 {
1 0
{ while(count==0); /*Buffer empty*/
produce_item(itemp); 2 /*Buffer empty*/
/*function call to produce item*/
while(count==n); /*Buffer Full*/ 3 Buffer[out]= itemc;
Buffer[in]= itemp;//store item in buffer /*take item from buffer */
in = in+1%n; //position next slot 4

5 out = out+1%n;
//count++; //increment /*position next slot */
count = count+1; 6 //count--; //Decrement
} count = count-1;
} I : Load Rp, m[count]; 7 process-item(itemc);
II: INCR Rp;
III: store m[count], Rp; I : Load Rc, m[count];
} II: DECR Rc;
III: store m[count], Rc;
}
Operating System Concepts – 10th Edition 7.8 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem-(Worst case)
Producer Consumer
int count = 0; //Global Variable N=8;
Buffer[0,1…..n-1]
Void consumer(void)
Void producer(void) count 3 {
{ int itemc;
int itemp; in 0 x1
out while(True)
while(True) 3 {
1 x2 0
{ while(count==0);
produce_item(itemp); 2 x3 /*Buffer empty*/
/*function call to produce item*/
while(count==n); /*Buffer Full*/ 3 Buffer[out]= itemc;
Buffer[in]= itemp;//store item in buffer /*take item from buffer */
in = in+1%n; //position next slot 4

5 out = out+1%n;
//count++; //increment /*position next slot */
count = count+1; 6 //count--; //Decrement
count = count-1;
} I : Load Rp, m[count]; 7 process-item(itemc);
} II: INCR Rp;
III: store m[count], Rp; } I : Load Rc, m[count];
} II: DECR Rc;
III: store m[count], Rc;
Operating System Concepts – 10th Edition 7.9 Silberschatz, Galvin and Gagne ©2018

You might also like