Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

multiple producers and consumers c++ problem: the program will need thread for each producer and consumer. As each thread produces or consumes a data item,

multiple producers and consumers c++ problem:

the program will need thread for each producer and consumer. As each thread produces or consumes a data item, it will print its status.

please use sem_t empty; sem_t full; pthread_mutex_t mutex;

program:

void * producer(void * arg) {

int myNum = producer_num.fetch_add(1) + 1;

while(true) {

sleep(rand() % 5 + 1);

int item = rand() % 100 + 1;

// TODO: Implement the meat of the producer algorithm, using any semaphores and

// mutex locks that are needed to ensure safe concurrent access.

// After producing your item and storing it in the buffer, print a line

} }

void * consumer(void * arg) {

int myNum = consumer_num.fetch_add(1) + 1;

while(true) { TODO: Implement the meat of the consumer algorithm, using any semaphores and

// mutex locks that are needed to ensure safe concurrent access.

//

// After consuming your item and storing it in "item", print a line in the following format: : slept , produced in slot

} }

int main(int argc, char ** argv)

{

// Usage: bounded_buffer

// TODO: Allocate your buffer as an array of the correct size based on the command line argument

// TODO: Initialize the semaphore(s) you are using in your algorithm.Use the function sem_init()

srand(time(NULL));

pthread_attr_t attr;

pthread_attr_init(&attr);

for (int i = 0; i < num_producers; ++i) {

pthread_t thread_id;

pthread_create(&thread_id, &attr, producer, NULL); }

for (int i = 0; i < num_consumers; ++i) {

pthread_t thread_id;

pthread_create(&thread_id, &attr, consumer, NULL); }

sleep(run_time);

exit(0);

}

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions