Question

In: Computer Science

C++ ONLY WITH COMMENTS Question: Producer / Consumer Create a program where the application accepts 2...

C++ ONLY WITH COMMENTS

Question:

Producer / Consumer

Create a program where the application accepts 2 arguments form the command line. The first one is a number where it is a producer of threads and the second one is number of consumer threads. You are allowed to use vector as a buffer and if so then please consider it as the buffer infinite. The producers will create the widgets and will put them on the buffer however the consumer will eat and then remove them from the buffer.

Solutions

Expert Solution

#include<iostream.h>

#include<pthread.h>

#include<semaphore.h>

#include<unistd.h>

using namespace std;

#define BUFFER_SIZE 10;

int buffer[BUFFER_SIZE];

int index=0;

sem_t full , empty;

pthread_mutex_t mutex;

void* produce ( void* arg)

{

while (1)

{ sleep(1);

sem_wait(&empty);

pthread_mutex_lock(&mutex);

int item = rand()%100;

buffer[index++] = item;

cout<<"Produced"<<item<<endl;

pthread_mutex_unlock(&mutex);

sem_post(&full);

}

}

void* consume( void* arg)

{

while (1)

{ sleep(1)

sem_wait(&full);

pthread_mutex_lock(&mutex);

int item = buffer[--index];

cout<<"Consumed"<<item<<endl;

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

}

int main()

{

pthread_t producer , consumer;

sem_init( &empty , 0 , BUFFER_SIZE);

sem_init( &full , 0 , 0 );

pthread_mutex_init( &mutex , NULL);

pthread_create( &producer , NULL, produce ,NULL );

pthread_create( &consumer , NULL , consume , NULL );

pthread_exit(NULL);

}


Related Solutions

Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
C code only, not C++ or anything else // This program accepts the chest size from...
C code only, not C++ or anything else // This program accepts the chest size from the customer // and returns the proper shirt size as well as the price. // function prototype void check_size_price(int size_val, char *size_char, int *price); #include <stdio.h> int main() { int chest_size; //input - entered chest size char size_str; //output - size (S/M/L) int p; //output - price // validation loop for the input – the chest size must be a positive integer // Call...
In C# Create a windows application which accepts the month number and displays the month name...
In C# Create a windows application which accepts the month number and displays the month name in a label.   Use a nested if... else statement to determine the month name. For months not in the range 1-12 display the message "Not a valid month"
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: • Number of days on the trip • Amount of airfare, if any • Amount of car rental fees, if any • Number of miles driven, if a private vehicle was used • Amount of parking fees, if any • Amount of...
Question 2. Write a MARIE program that accepts an integer from the user, and if it...
Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and...
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
Card Shuffling and Dealing (C++) All in one file with comments explaining please! Create a program...
Card Shuffling and Dealing (C++) All in one file with comments explaining please! Create a program to shuffle and deal a deck of cards. The program should consist of a class Card, class DeckOfCards and a driver program. Class Card should provide: a)      Data members face and suit of type int. b)      A constructor that receives two ints representing the face and suit and uses them to initialize the data members. c)      Two static arrays of strings representing the faces...
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
2. Consumer Surplus and Producer Surplus Explain in words and graphically how consumer surplus, producer surplus...
2. Consumer Surplus and Producer Surplus Explain in words and graphically how consumer surplus, producer surplus and total surplus change when the minimum wage is removed. Assume the minimum wage is above the free market price. In your explanation please interpret the components of the changes in consumer surplus, producer surplus and total surplus; i.e. what each component represents. For additional points, what happens if the minimum wage is set below the free market price? please graph
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT