10.producer, Consumer Problem
10.producer, Consumer Problem
Examples
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
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
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)
• 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