Question

URGENT PLEASE HELP. could you make it easy to understand and in C++ thanks 1. (30...

URGENT PLEASE HELP. could you make it easy to understand and in C++ thanks

1. (30 points) Bounded-buffer Producer/Consumer problem: Implement a solution to the boundedbuffer producer/consumer problem. Implement the solution using synchronization primitives (pthread_mutex_t for mutual exclusion and sem_t for minding the buffer size) provided by the POSIX threads. Your implementation will consist of a control program that (i) initializes the buffer and the synchronization variables and (ii) creates and terminates the threads for the producer and the consumer. The producer generates printable characters and places them into the buffer and the consumer pulls the characters out of the buffer one at a time and prints them out. Let the control program simulate all possible conditions: buffer empty (consumer waiting), buffer full (producer waiting), buffer not empty (both producer and consumer working). Assume that production and consumption per character take the same amount of time.   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <pthread.h>
#include <semaphore.h>
#include <windows.h>
#include <chrono>
#include <iostream>
using namespace std;

#define BUFFERSIZE 4
#define FULL 0
#define EMPTY 0

char buffer[BUFFERSIZE];
int inIndex = 0;
int outIndex = 0;

// Producer semaphore
sem_t producerSemaphore;
// Consumer semaphore
sem_t consumerSemaphore;

void produce(char item)
{
// Gets the mutex to fill the buffer
sem_wait(&producerSemaphore);

buffer[inIndex] = item;
inIndex = (inIndex + 1) % BUFFERSIZE;
cout<<"\n Producer producing "<<item;

if(inIndex == FULL)
{
sem_post(&consumerSemaphore);
Sleep(1);
}
sem_post(&producerSemaphore);
}

void *Producer(void * arg)
{
for(int c = 0; c < 5; c++)
produce((char)('A'+ c % 26));
}

void consume()
{
char item;

// Gets the mutex to consume from buffer
sem_wait(&consumerSemaphore);

item = buffer[outIndex];
outIndex = (outIndex + 1) % BUFFERSIZE;
cout<<"\n Consumer consuming "<<item;
if(outIndex == EMPTY) //its empty
Sleep(1);

sem_post(&consumerSemaphore);
}

void * Consumer(void * arg)
{
int i;
for(i = 0; i < 10; i++)
consume();
}

int main()
{
pthread_t producerThreadid, consumerThreadtid;

// Initialize the semaphores
sem_init(&producerSemaphore, 0, 1);
sem_init(&consumerSemaphore, 0, 0);

// Creating producer threads
if(pthread_create(&producerThreadid, NULL,Producer, NULL))
{
cout<<"\n ERROR creating thread 1";
exit(1);
}
// Creating consumer threads
if(pthread_create(&consumerThreadtid, NULL,Consumer, NULL))
{
cout<<"\n ERROR creating thread 2";
exit(1);
}

if(pthread_join(producerThreadid, NULL)) /* wait for the producer to finish */
{
cout<<"\n ERROR joining thread";
exit(1);
}

if(pthread_join(consumerThreadtid, NULL)) /* wait for consumer to finish */
{
cout<<"\n ERROR joining thread";
exit(1);
}

sem_destroy(&producerSemaphore);
sem_destroy(&consumerSemaphore);

// Exits the main thread
pthread_exit(NULL);
return 0;
}

Sample Output:

Producer producing A
Producer producing B
Producer producing C
Producer producing D
Consumer consuming A
Consumer consuming B
Consumer consuming C
Consumer consuming D
Consumer consuming A
Producer producing E
Consumer consuming B
Consumer consuming C
Consumer consuming D
Consumer consuming E
Consumer consuming B

Add a comment
Know the answer?
Add Answer to:
URGENT PLEASE HELP. could you make it easy to understand and in C++ thanks 1. (30...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multit...

    Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is a common problem that requires cooperating processes or...

  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • operating system engineering , please read the question and solve on the given skeleton code ....

    operating system engineering , please read the question and solve on the given skeleton code . Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...

  • A university computer science department has a teaching assistant (TA) who helps undergraduate students with their...

    A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TA’s office is rather small and the TA can help only one student at a time in the office. There are two chairs in the hallway outside the office where students can sit and wait if the TA is currently helping another student. When there are no students who need help during office hours, the TA sits...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT