Question: Please help me with this lab assignment Here is the base code: #include #include #include #include /*for the intptr_t */ #include /*for the sleep */

Please help me with this lab assignmentPlease help me with this lab assignment Here is the base code:#include #include #include #include /*for the intptr_t */ #include /*for the sleep

Here is the base code:

#include #include #include #include /*for the intptr_t */ #include /*for the sleep */ #include

#define BUFF_SIZE 5 /* total number of slots */ #define NPROD 3 /* total number of producers */ #define NCONSUM 3 /* total number of consumers */ #define ITEMSPROD 4 /* number of items produced/consumed */

typedef struct { int buf[BUFF_SIZE]; /* shared var */ int in; /* buf[in%BUFF_SIZE] is the first empty slot */ int out; /* buf[out%BUFF_SIZE] is the first full slot */ sem_t full; /* keep track of the number of full spots */ sem_t empty; /* keep track of the number of empty spots */ sem_t mutex; /* enforce mutual exclusion to shared data */ } sbuf_t;

sbuf_t shared;

void *Producer(void *arg) { int i, item, index; index = (intptr_t)arg; pthread_detach(pthread_self());

for (i=0; i

void *Consumer(void *arg) { /* Fill in the code here */ }

int main() { pthread_t idP, idC; int index;

sem_init(&shared.full, 0, 0); sem_init(&shared.empty, 0, BUFF_SIZE);

/* Insert code here to initialize mutex*/

for (index = 0; index

/* Insert code here to create NCONSUM consumers */

pthread_exit(NULL); }

The Producers-Consumers Problem In this assignment you will implement a solution to the producers-consumers problem discussed in class The problem describes two sets of processes, the producers and the consumers, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that a producer won't try to add data into a full buffer, and a consumer won't try to remove data from an empty buffer. Our solution to this problem will make use of POSIX semaphores. See the manual with man 7sem overview 1. Use as a base the incomplete producer-consumer (producerConsumer.c) code provided. 2. Compile the code and run it. You will notice that there is no output and the process seems blocked. Kill the process with CTRL-C or do so using the kill command Remember to link the POSIX pthread and real-time librari uses pthreads and posix semaphores es when compiling a program that gcc -o xfilename filename.c -1pthread-1rt 3. Add code to initialize the semaphore mutex in the main function, then repeat the step above You will see output from the producers. 4. Complete the code for the Consumer thread, then add code to create Consumer threads in the main function. The result should be a solution to the producer-consumer problem Comment well your code. Compile and run your program and observe the output. Make sure to label each line in the output by the identifier for each producer and consumer (PO, P1, P2, .. CO, C1, C2,...). The output of your program should be similar to the following Pe] Producing e [P1] Producing . [P2] Producing e [P2] Producing1 .- > [C2] consumed [C2] consumed 1 [P1] Producing1 .- > [C1] consumed [C1] consumed 1 Pe] Producing 1 The Producers-Consumers Problem In this assignment you will implement a solution to the producers-consumers problem discussed in class The problem describes two sets of processes, the producers and the consumers, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that a producer won't try to add data into a full buffer, and a consumer won't try to remove data from an empty buffer. Our solution to this problem will make use of POSIX semaphores. See the manual with man 7sem overview 1. Use as a base the incomplete producer-consumer (producerConsumer.c) code provided. 2. Compile the code and run it. You will notice that there is no output and the process seems blocked. Kill the process with CTRL-C or do so using the kill command Remember to link the POSIX pthread and real-time librari uses pthreads and posix semaphores es when compiling a program that gcc -o xfilename filename.c -1pthread-1rt 3. Add code to initialize the semaphore mutex in the main function, then repeat the step above You will see output from the producers. 4. Complete the code for the Consumer thread, then add code to create Consumer threads in the main function. The result should be a solution to the producer-consumer problem Comment well your code. Compile and run your program and observe the output. Make sure to label each line in the output by the identifier for each producer and consumer (PO, P1, P2, .. CO, C1, C2,...). The output of your program should be similar to the following Pe] Producing e [P1] Producing . [P2] Producing e [P2] Producing1 .- > [C2] consumed [C2] consumed 1 [P1] Producing1 .- > [C1] consumed [C1] consumed 1 Pe] Producing 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!