Question: Everything seems to work but the problem is i can not get it to show the thread id of the producer and the consumer. please

Everything seems to work but the problem is i can not get it to show the thread id of the producer and the consumer. please help with that error?

The exact part that is missing -

In addition to the requirement in the textbook, print a message every time an item is produced or consumed in your producer and consumer threads. The message should also include the thread ID of the producer or consumer. To get a threads ID, you may call pthread_self() function and convert the result into an integer. For example, you can use printf("Consumer %u consumed %d ",(unsigned int)pthread_self(), consumed_number); for the consumer. You can use a similar code for the producer.

Everything seems to work but the problem is i can not get

it to show the thread id of the producer and the consumer.please help with that error? The exact part that is missing -In addition to the requirement in the textbook, print a message everytime an item is produced or consumed in your producer and consumer

hw3.c code finished

#include "buffer.h" #include #include #include #include #include

buffer_item buffer[BUFFER_SIZE]; pthread_mutex_t mutex; sem_t empty; sem_t full; int insert=0; int removal=0; int cou=0;

void *producer(void *param); void *consumer(void *param);

int insert_item(buffer_item item) { int suc=0; /* Acquire Empty Semaphore */ sem_wait(&empty);

/* Acquire mutex lock to protect buffer */ pthread_mutex_lock(&mutex);

/* Insert item into buffer */ if(cou!=BUFFER_SIZE) { buffer[insert]=item; insert=(insert+1)%BUFFER_SIZE; cou++; suc=0; } else { suc=-1; } /* Release mutex lock and full semaphore */

pthread_mutex_unlock(&mutex); sem_post(&full);

return suc; }

int remove_item(buffer_item *item) { /* Acquire Full Semaphore */ int suc;

/* Acquire mutex lock to protect buffer */ sem_wait(&full); pthread_mutex_lock(&mutex);

/* remove an object from buffer placing it in item */ if(cou!=0) { *item=buffer[removal]; removal=(removal+1)%BUFFER_SIZE; cou--; suc=0; } else { suc=-1; } /* Release mutex lock and empty semaphore */ pthread_mutex_unlock(&mutex); sem_post(&empty); return suc; }

int main(int argc, char *argv[]) { /*if (argc != 4){ printf("Not three arguments. "); exit(1); }*/ /* Get command line arguments argv[1],argv[2],argv[3] */ const long int sle_time = strtol(argv[1], NULL, 0); const long int no_producer =strtol(argv[2], NULL, 0); const long int no_consumer = strtol(argv[3], NULL, 0); /* Initialize buffer related synchronization tools */ int di=0; srand(time(NULL)); pthread_mutex_init(&mutex, NULL); sem_init(&empty, 0, BUFFER_SIZE); sem_init(&full, 0, 0); cou = insert = removal = 0;

/* Create producer threads based on the command line input */ pthread_t prod[no_producer]; /* Create consumer threads based on the command line input */ pthread_t cons[no_consumer]; /* Sleep for user specified time based on the command line input */ for(di = 0; di

void *producer(void *param) { buffer_item it; while(1){ sleep(rand() % 8 + 1); // Sleep randomly between 1 and 5 seconds it = rand(); if(insert_item(it)) printf("Error........ "); else printf("Produced item : %d ", it); }

}

void *consumer(void *param) { buffer_item it; while(1){ sleep(rand() % 8 + 1);

if(remove_item(&it)) printf("Error....... "); else printf("Consumed item: %d ", it); } }

BUFFER.H FILE

/* buffer.h*/

typedef int buffer_item; #define BUFFER_SIZE 5

int insert_item(buffer_item item); int remove_item(buffer_item *item);

the output for this code

threads. The message should also include the thread ID of the producer

Design and implement a solution for Producer-Consumer Problem using Pthreads library in Linux virtual machine Steps: Download buffer.h and the incomplete version of hw3.c from iCollege Read through Programming Project 3 of Chapter 5 (Producer-Consumer Problem) in the .Follow the suggestion in the textbook to complete the C program using Pthreads. Do NOT solve In addition to the requirement in the textbook, print a message every time an item is produced textbook. the problem using Windows API or consumed in your producer and consumer threads. The message should also include the thread ID of the producer or consumer. To get a thread's ID, you may call pthread_self() function and convert the result into an integer. For example, you can use printf("Consumer %u consumed %d ",(unsigned int)pthread-self(), consumed-number); for the consumer. You can use a similar code for the producer. Compile the C source file using gcc -pthread -o hw3 hw3.c Use ./hw3

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!