Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please post screenshot of your code working In this project you will create a pr

ID: 3824544 • Letter: P

Question

Please post screenshot of your code working

In this project you will create a program called producer-consumer.c that will do the following: 1.-when the -p command line argument is passed, it will act as a producer 2.- when the -c command line argument is passed, it will act as a consumer 3.-the message produced by the producer will be given by the command line argument -m "string" 4.-the depth of the queue used by your producer consumer solution will be given by the q integer command line argument combination. 5. -Your program will either use unix socket (-u) or shared memory (-s) depending on user input in the command line argument 6.-Your program must use linux semaphores to protect critical sections. 7.-when given the -e option, your program should print the string being produced or consumed followed by a new line each time the string is produced or consumed.

Explanation / Answer

#include<iostream>

#include<stdio.h>

#include<fstream>

#include<sstream>

#include<stdlib.h>

#include<string>

#include<map>

#include<vector>

# include <stdio.h>

# include <pthread.h>

using namespace std;

void *Producer(char *s);

void *Consumer();

int BufferIndex=0, BufferSize=0;

char **BUFFER;

pthread_cond_t Buffer_Not_Full=PTHREAD_COND_INITIALIZER;

pthread_cond_t Buffer_Not_Empty=PTHREAD_COND_INITIALIZER;

pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;

char latest[100];

int main() {

pthread_t ptid,ctid;

char c;

int q;

char s[100];

  

pthread_create(&ptid,NULL,Producer,NULL);

pthread_create(&ctid,NULL,Consumer,NULL);

  

pthread_join(ptid,NULL);

pthread_join(ctid,NULL);

while(true) {

cin>>c;

if(c=='p' or c=='u' or c=='s') {

  

} else if(c=='c') {

Consumer();

} else if(c=='m') {

cin>>s;

Producer(s);

} else if(c=='q') {

cin>>BufferSize;

BUFFER=(char **) malloc(sizeof(char*) * BufferSize);

} else {

cout<<"Last produced or consumed string "<<latest<<endl;

}

}

return 0;

}

void *Producer(char *s) {

for(;;) {

pthread_mutex_lock(&mVar);

if(BufferIndex==BufferSize) {

pthread_cond_wait(&Buffer_Not_Full,&mVar);

}

strcpy(latest,s);

BUFFER[BufferIndex]=(char*)malloc(sizeof(char)*strlen(s));

BUFFER[BufferIndex++]=s;

printf("Produce : %d ",BufferIndex);

pthread_mutex_unlock(&mVar);

pthread_cond_signal(&Buffer_Not_Empty);

}

}

void *Consumer() {

for(;;) {

pthread_mutex_lock(&mVar);

if(BufferIndex==-1) {

pthread_cond_wait(&Buffer_Not_Empty,&mVar);

}

strcpy(latest,BUFFER[BufferSize]);

free(BUFFER[BufferSize--]);

pthread_mutex_unlock(&mVar);

pthread_cond_signal(&Buffer_Not_Full);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote