Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you please help me in fixing this code inorder to get below mentioned output. help me in the below mentioned code in c only

can you please help me in fixing this code inorder to get below mentioned output.

help me in the below mentioned code in c only so that i can understand my mistakes how can i fix them ..please help me correctly dont copy any code just analyze the give code.

% osproj3 30 3 2 2 yes Starting Threads... (buffers occupied: 0) buffers: -1 -1 -1 -1 -1 ---- ---- ---- ---- ---- WR Producer 12348 writes 31 (buffers occupied: 1) buffers: 31 -1 -1 -1 -1 ---- ---- ---- ---- ---- R W Producer 12349 writes 4 (buffers occupied: 2) buffers: 31 4 -1 -1 -1 ---- ---- ---- ---- ---- R W Consumer 12350 reads 31 * * * PRIME * * * (buffers occupied: 1) buffers: 31 4 -1 -1 -1 ---- ---- ---- ---- ---- R W ...SOME TIME GOES BY... Consumer 12350 reads 4 (buffers occupied: 0) buffers: 3 4 19 31 97 ---- ---- ---- ---- ---- WR All buffers empty. Consumer 12351 waits. All buffers empty. Consumer 12350 waits. ...SOME TIME GOES BY... Producer 12348 writes 41 (buffers occupied: 5) buffers: 28 41 23 45 6 ---- ---- ---- ---- ---- RW All buffers full. Producer 12349 waits. ...SOME TIME GOES BY... Producer 12349 writes 10 (buffers occupied: 1) buffers: 11 10 18 68 94 ---- ---- ---- ---- ---- R W Consumer 12350 reads 10 (buffers occupied: 0) buffers: 11 10 18 68 94 ---- ---- ---- ---- ---- WR ...SOME TIME GOES BY... PRODUCER / CONSUMER SIMULATION COMPLETE ======================================= Simulation Time: 30 Maximum Thread Sleep Time: 3 Number of Producer Threads: 2 Number of Consumer Threads: 2 Size of Buffer: 5 Total Number of Items Produced: 50 Thread 1: 30 Thread 2: 20 Total Number of Items Consumed: 48 Thread 3: 22 Thread 4: 26 Number Of Items Remaining in Buffer: 2 Number Of Times Buffer Was Full: 3 Number Of Times Buffer Was Empty: 4 

can you please help me in fixing this code inorder to get above mentioned output.

typedef int buffer_item;

#define BUFFER_SIZE 5

#include

#include

#include

#include

#include

#include "buffer.h"

int buffer_in_count=0;

int buffer_out_count=0;

pthread_mutex_t mutex;

sem_t full, empty;

buffer_item buffer[BUFFER_SIZE];

int counter;

int maxThreadsleeptime;

#define buffer_print();

pthread_t tid;

pthread_attr_t attr;

int *producer_counts;

int *consumer_counts;

int totalproducer_counts=0;

int totalconsumer_counts=0;

void *producer(void *param);

void *consumer(void *param);

void buffer_print();

int checkPrime(buffer_item *in);

int timeoutreached=0;

void buffer_buffer_print() {

printf("(buffers occupied: %d) ",counter);

printf("buffers:");

int i=0;

for(int i=0; i

printf("%6d",buffer[i]);

}

printf(" ");

for(int i=0; i

printf(" ----");

}

printf(" ");

}

void initializeBuffer() {

int i;

pthread_mutex_init(&mutex, NULL);

sem_init(&empty, 0, BUFFER_SIZE);

sem_init(&full, 0, 0);

pthread_attr_init(&attr);

for(i=0;i

}

int checkPrime(buffer_item *in)

{

int prime = 1;

int i=0;

for(int i=2; i<= (int)*in / 2; i++)

{

if (((int)*in % i) == 0)

{

prime = 0;

break;

}

}

return prime;

}

void *producer(void *param) {

buffer_item item;

int* index = (int*)param;

while(timeoutreached==0) {

unsigned int seed = time(NULL);

int sleeptime = rand_r(&seed)%(maxThreadsleeptime-1) + 1;

sleep(sleeptime);

seed = time(NULL);

unsigned int pid =(int) (long)pthread_self();

unsigned int seedfinal = time(NULL) + pid;

item = rand_r(&seedfinal)%(500-1) + 1;

sem_wait(&empty);

pthread_mutex_lock(&mutex);

if(buffer_insert_item(item)) {

producer_counts[*index]++;

totalproducer_counts++;

printf("Producer %d writes %d ",pthread_self(), item);

buffer_print();

} else {

printf("All buffers full. Producer %d waits.",pthread_self());

}

pthread_mutex_unlock(&mutex);

sem_post(&full);

}

}

void *consumer(void *param) {

buffer_item item;

int* index = (int*)param;

while(timeoutreached==0) {

unsigned int seed = time(NULL);

int sleeptime = rand_r(&seed)%(maxThreadsleeptime-1) + 1;

sleep(sleeptime);

seed = time(NULL);

sem_wait(&full);

pthread_mutex_lock(&mutex);

if(buffer_remove_item(&item)==1) {

consumer_counts[*index]++;

totalconsumer_counts++;

printf("Consumer %d reads %d", pthread_self(),item);

if(checkprime(&item)) {

printf(" * * * PRIME * * *");

}

printf(" ");

buffer_print();

}

else {

printf("All buffers emptysemaphore. Consumer %d waits.",pthread_self());

}

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

}

int buffer_insert_item(buffer_item item) {

if(counter < BUFFER_SIZE) {

buffer[counter] = item;

counter = counter+1;

return 1;

}

else {

buffer_in_count++;

return 0;

}

}

int buffer_remove_item(buffer_item *item) {

if(counter > 0) {

*item = buffer[(counter-1)];

buffer[(counter-1)] = -1;

counter = counter-1;

return 1;

}

else {

buffer_out_count++;

return 0;

}

}

int main(int argc, char *argv[]) {

if(argc != 6) {

printf("Invalid usage");

}

//Get command line arguments

int mainthreadruntime = atoi(argv[1]);

maxThreadsleeptime = atoi(argv[2]);

int producers = atoi(argv[3]);

int consumers = atoi(argv[4]);

char* buffersnapshots = argv[5];

//Initialize buffer

initializeBuffer();

int i;

producer_counts = (int*)malloc(sizeof(int) * producers);

consumer_counts = (int*)malloc(sizeof(int) * consumers);

for(i = 0;i < producers; producer_counts[i++]==0);

for(i = 0;i < consumers; consumer_counts[i++]==0);

printf("Starting Threads... ");

buffer_print();

//Create producer thread(s)

for(i = 0; i < producer_counts; i++) {

pthread_create(&tid,&attr,producer,(void*)&i);

}

//Create consumer thread(s)

for(i = 0; i < consumer_counts; i++) {

pthread_create(&tid,&attr,consumer,(void*)&i);

}

//Sleep

sleep(mainthreadruntime);

timeoutreached =1;

//Join Threads

// pthread_join(tid,NULL);

sleep(1);

printf(" PRODUCER / CONSUMER SIMULATION COMPLETE");

printf(" Simulation Time: %d",mainthreadruntime);

printf(" Maximum Thread Sleep Time: %d",maxThreadsleeptime);

printf(" Number of Producer Threads: %d",producers);

printf(" Number of Consumer Threads: %d",producers);

printf(" Size of Buffer: %d",BUFFER_SIZE);

printf(" ");

printf("Total Number of Items Produced: %d ",totalproducer_counts);

for(i = 0; i < producers; i++) {

printf(" Thread %d: %d ",(i+1),producer_counts[i]);

}

printf("Total Number of Items Consumed: %d ",totalconsumer_counts);

for(i = 0; i < producers; i++) {

printf(" Thread %d: %d ",(i+1),consumer_counts[i]);

}

printf("Number Of Items Remaining in Buffer: %d ",counter);

printf("Number Of Times Buffer Was Full: %d ",buffer_in_count);

printf("Number Of Times Buffer Was Empty: %d ",buffer_out_count);

//Exit

exit(0);

}

MY OUTPUT IS :

lil.c:30:1: warning: useless type name in empty declaration void buffer_print(); ^ lil.c: In function producer: lil.c:93:12: warning: implicit declaration of function buffer_insert_item [-Wimplicit-function-declaration] if(buffer_insert_item(item)) { ^ lil.c: In function consumer: lil.c:119:12: warning: implicit declaration of function buffer_remove_item [-Wimplicit-function-declaration] if(buffer_remove_item(&item)==1) { ^ lil.c:123:7: warning: implicit declaration of function checkprime [-Wimplicit-function-declaration] if(checkprime(&item)) { ^ lil.c: In function main: lil.c:191:18: warning: comparison between pointer and integer for(i = 0; i < producer_counts; i++) { ^ lil.c:196:18: warning: comparison between pointer and integer for(i = 0; i < consumer_counts; i++) { ^ /tmp/ccaWnvIN.o: In function `consumer': lil.c:(.text+0x3aa): undefined reference to `checkprime' collect2: error: ld returned 1 exit status

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

More Books

Students also viewed these Databases questions

Question

5. Identify three characteristics of the dialectical approach.

Answered: 1 week ago