Consider the producer-consumer problem where the producer produces items to be consumed by the consumer. A solution of the problem is to be implemented using threads. The main process will run producer and consumer as two different threads. The producer will put the items it generates into a buffer of length 15. The consumer reads the elements of the buffer to get the items. A solution is given below. Each item produced and consumed are also printed to the screen. Run the program several times and find a sample run when the items produced are not correctly consumed. Explain why the program does not always work correctly.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Consider the producer-consumer problem where the producer produces items to be consumed by the
consumer. A solution of the problem is to be implemented using threads. The main process will run
producer and consumer as two different threads. The producer will put the items it generates into a
buffer of length 15. The consumer reads the elements of the buffer to get the items. A solution is
given below. Each item produced and consumed are also printed to the screen. Run the program
several times and find a sample run when the items produced are not correctly consumed. Explain
why the program does not always work correctly.
Transcribed Image Text:Consider the producer-consumer problem where the producer produces items to be consumed by the consumer. A solution of the problem is to be implemented using threads. The main process will run producer and consumer as two different threads. The producer will put the items it generates into a buffer of length 15. The consumer reads the elements of the buffer to get the items. A solution is given below. Each item produced and consumed are also printed to the screen. Run the program several times and find a sample run when the items produced are not correctly consumed. Explain why the program does not always work correctly.
#include <gtdia.h>
#include <stdlib.h
#include <pthread.h>
m
int buffer [15]
{0};
int in =0, out = 0;
void *Producer ()//
{
int nextR:
w wo
for (int i=0;i<15;i++) {
//produce an item
nextp = 2 *3+i;
buffer [in] =
printf("The producer produced the item = %d\n",nextp);
in = (in+1);
nextp;
wwww
}
}
void *Consumer () / /
{
int nextsi
for (int i-0;i<15;i++) {
nextc = buffer[out];
printf("The consumer consumed the item = %d\n",nextc);
out = (out+1);
}
}
void main ()
{
Rthread t threadl;
Rthrsad.t thread2;
gthread create (&threadl, NULL, Producer, NULL);
gthread create (&thread2, NULL, Consumer, NULL);
pthread join (thread1,NULL);
pthread join (thread2, NULL);
exit (0) ;
}
Transcribed Image Text:#include <gtdia.h> #include <stdlib.h #include <pthread.h> m int buffer [15] {0}; int in =0, out = 0; void *Producer ()// { int nextR: w wo for (int i=0;i<15;i++) { //produce an item nextp = 2 *3+i; buffer [in] = printf("The producer produced the item = %d\n",nextp); in = (in+1); nextp; wwww } } void *Consumer () / / { int nextsi for (int i-0;i<15;i++) { nextc = buffer[out]; printf("The consumer consumed the item = %d\n",nextc); out = (out+1); } } void main () { Rthread t threadl; Rthrsad.t thread2; gthread create (&threadl, NULL, Producer, NULL); gthread create (&thread2, NULL, Consumer, NULL); pthread join (thread1,NULL); pthread join (thread2, NULL); exit (0) ; }
Expert Solution
steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY