Producer and Consumer Code in C++
The program will contain two methods- one each for the producer and consumer. Both methods share access to an integer array buffer of size 50 with all values initialized to 0 at the beginning and an integer variable counter initialized to 0. As given below, the producer accesses the buffer elements and updates the element to 1 ( indicating production). The consumer changes a buffer elements to 0 (indicating consumption).
Enhancements and modifications:
-The random number for the producer indicates to the producer how many elements to produce.
-The number for the consumer indicates the number of elements to be consumed by the consumer.
Within the loop, the methods will be called randomly – based on a random number. For example, if the number generated is 0 then the producer method is called and if the number generated is 1, the consumer is called.
Pseudo Code:
//PRODUCER- producing one element
{
/* produce an item in next produced
*/
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] =1; // next_produced
in = (in + 1) % BUFFER_SIZE;
counter++;
}
//CONSUMER—consuming one element
{
while (counter == 0)
; /* do nothing */
buffer[out]= 0;// next_consumed
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Here is the complete program:
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define BUFFER_SIZE 50
int buffer[BUFFER_SIZE];
using namespace std;
void producer(int& counter, int& in)
{
int prod_count = rand() % 5 + 1;
cout << "\nItems before producer call: "
<< counter << "\n";
for (int i = 0; i < prod_count && counter
< BUFFER_SIZE; i++)
{
buffer[in] = 1;
counter += 1;
in = (in + 1) % BUFFER_SIZE;
}
if(counter == BUFFER_SIZE)
cout << "\nItems after
producer call: " << BUFFER_SIZE << " (Buffer
full)\n";
else
cout << "\nItems after
producer call: " << counter << "\n";
}
void consumer(int& counter, int& out)
{
int prod_count = rand() % 5 + 1;
cout << "\nItems before consumer call: "
<< counter << "\n";
for (int i = 0; i < prod_count && counter
> 0; i++)
{
buffer[out] = 0;
counter -= 1;
out = (out + 1) %
BUFFER_SIZE;
}
if(counter == 0)
cout << "\nItems after
consumer call: 0 (Buffer empty)\n";
else
cout << "\nItems after
consumer call: " << counter << "\n";
}
int main()
{
int x;
srand(time(0));
int in = 0, out = 0;
cout << "Enter the number of times the loop
should run: ";
cin >> x;
memset(buffer, 0, sizeof(buffer));
int random_int, counter = 0;
for (int i = 0; i < x; i++)
{
random_int = rand() % 2;
if (random_int)
{
consumer(counter, in);
}
else
{
producer(counter, out);
}
}
}
Here is a sample where the loop runs for 10 times:
Producer and Consumer Code in C++ The program will contain two methods- one each for the...
9. Here are the codes for producer and consumer problems Producer: while (true) { /* produce an item in next produced */ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; } Consumer: while (true) { while (counter == 0) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /*...
Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...
You are given a sample code that when edited/ improved/completed -will work as a producer consumer synchronized solution. In addition to the counter, you will implement a circular linked list as a buffer of size 200 that stores data items 0 or 1. The producer and consumer take *random* turns to produce and consume- *random* numbers of elements ( turning 1 to a 0 and visa-versa-each time, as discussed in class). After each turn of the producer and/or consumer, your...
write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...
I need to creatre a C++ program that can modify the producer and consumer. My main programmig language is C++. Modify "Producer and Consumer Problem" from lecture note so that it can use all buffer space, not "buffersize – 1" as in the lecture note. This program should work as follows: 1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The main...
Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows: 1.The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...
Consider the Producer-Consumer problem Assume that there are 2 producers (P1 and P2) and 1 consumer (C1). The maximum number of items in the queue is 100. Consider the following solution The functions qfull enque and deque are the standard functions discussed in class Assume they have been implemented correctly by one of the methods Semaphore ni=0, mutex=1; /* ni= number of items currently in the queue.*/ /* mutex is used to provide critical section. */ Code of a Producer...
I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...
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...
In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....