Solve the producer and consumer problem with inter thread communication (join(), wait(), sleep() etc.) modifying the given C code.   #include    #include

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

Solve the producer and consumer problem with inter thread communication (join(), wait(), sleep() etc.) modifying the given C code.

 

#include <pthread.h>  

#include <stdio.h>

#include <string.h>

#define MAX 10 //producers and consumers can produce and consume upto MAX

#define BUFLEN 6

#define NUMTHREAD 2      /* number of threads */

void * consumer(int *id);

void * producer(int *id);

 

char buffer[BUFLEN];

char source[BUFLEN]; //from this array producer will store it's production into buffer

int pCount = 0;

int cCount = 0;

int buflen;

 

//initializing pthread mutex and condition variables

pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t nonEmpty  = PTHREAD_COND_INITIALIZER;

pthread_cond_t full  = PTHREAD_COND_INITIALIZER;

int thread_id[NUMTHREAD]  = {0,1};

int i = 0;

int j = 0;

 

main(){

    int i;

    /* define the type to be pthread */

    pthread_t thread[NUMTHREAD];

 

    strcpy(source,"abcdef");

    buflen = strlen(source);

    /* create 2 threads*/

    /* create one consumer and one producer */

    /* define the properties of multi threads for both threads  */

    //Write Code Here

 

    

   

 

}

 

 

void * producer(int *id){

   /*

  1. Producer stores the values in the buffer (Here copies values from source[] to buffer[]).
  2. Use mutex and thread communication (wait(), sleep() etc.) for critical section.
  3. Print which producer is storing which values using which thread inside the critical section.
  4. Producer can produce upto MAX

*/

//Write code here

}

 

void * consumer(int *id)

{

     /*

  1. Consumer takes out the value from the buffer and makes it empty.
  2. Use mutex and thread communication (wait(), sleep() etc.) for critical section
  3. Print which consumer is taking which values using which thread inside the critical section.
  4. Consumer can consume upto MAX

   */

//Write code here

}

 

Example Output:

0 produced  a  by Thread  0

1 produced  b  by Thread  0

0 consumed    by Thread  1

2 produced  c  by Thread  0

1 consumed  b  by Thread  1

3 produced  d  by Thread  0

4 produced  e  by Thread  0

2 consumed  c  by Thread  1

5 produced  f  by Thread  0

6 produced  a  by Thread  0

3 consumed  d  by Thread  1

7 produced  b  by Thread  0

8 produced  c  by Thread  0

9 produced  d  by Thread  0

4 consumed  e  by Thread  1

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

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