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...
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...
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
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
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
C++ only Large Program Create a magical creature (or monster) zoo inventory program that will allow...
C++ only Large Program Create a magical creature (or monster) zoo inventory program that will allow a zookeeper to add magical creatures (either manually or from a file), delete a creature, and display all creatures in the zoo. The zoo creatures will be organized in a linked list. ---------------------------------------------------------------------------- Zoo.cpp – this file will contain your main function that will allow the user to enter, delete, and print creatures. The main function will create the linked list of creatures. Creature.h...
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT