On the same machine, is the only way to run producer and consumer. In C/C++ use
ID: 3700581 • Letter: O
Question
On the same machine, is the only way to run producer and consumer. In C/C++ use shared memory and semaphores for implementation of the logical ring buffer that stores up to 50 data items and the synchronization? The producer can generate 100 integers randomly. The consumer reads all the data. Generated data from producer should be stored in a file by the producer and the data read by consumer must be stored in a file by the consumer. The files with each individual data must be printed to show that they cooperated correctly.(so the producer data file and consumer data file) Data should not be missing or consumed multiple times. On the same machine, is the only way to run producer and consumer. In C/C++ use shared memory and semaphores for implementation of the logical ring buffer that stores up to 50 data items and the synchronization? The producer can generate 100 integers randomly. The consumer reads all the data. Generated data from producer should be stored in a file by the producer and the data read by consumer must be stored in a file by the consumer. The files with each individual data must be printed to show that they cooperated correctly.(so the producer data file and consumer data file) Data should not be missing or consumed multiple times.Explanation / Answer
Code
int produce_item()
{
int item = rand()%50 +1;
printf("Producer produces %d ",item);
sleep(1);
return item;
}
void consume_item(int item)
{
printf("Consumer consumes %d ",item);
sleep(2);
}
void* consumer(void * arg)
{
int item, j=0;
while(1){
down(full);
down(mutex);
consume_item(item);
buffer[j++] = item;
up(mutex);
up(empty);
}
return 0;
}
void* producer(void* arg)
{
int item, i;
while(1){
item = produce_item();
down(empty);
down(mutex);
buffer[i++]=item;
up(mutex);
up(full);
sleep(1);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.