Question

In: Computer Science

The bounded buffer problem is a producer-consumer problem where a producer process adds new items to...

The bounded buffer problem is a producer-consumer problem where a producer process adds new items to a shared buffer and a consumer process consumes items from that buffer (in the first-in-first-out (FIFO) order). When a producer adds a new item, the buffer size grows by one, and whenever a consumer consumes an item from the buffer, the size is reduced by one. The producer process must wait when the buffer is full likewise the consumer process must wait when the buffer is empty. You are to implement the bounded buffer (bb) problem using semaphores using POSIX APIs.

Use the following code:

// Synchronization tools  
// Bounded buffer program with semaphores
// Author name: _______________________
// Author email: ___________________


#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <fcntl.h> 
#include <sys/shm.h> 
#include <sys/mman.h> 
#include <sys/stat.h> 
#include <semaphore.h>
#include <unistd.h>
#include <sys/wait.h>


const int SIZE = 4;
int __item = 1001;
int IN;
int OUT;

int *count;
int *buffer;


void init() {
  // TODO: Create semaphores



  // Creating shared memory
  int shm_fd = shm_open("PROD_CONS", O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, 4096);
  int *sbuf = (int *)mmap(0, 4096, PROT_WRITE, MAP_SHARED, shm_fd, 0);

  count = sbuf;
  buffer = sbuf + 1;
  *count = 0;
  IN = OUT = 0;
} 

/* Generate a new item */
int generate_new_item() {
  return __item++;
}

void write_buffer() {
  //TODO: Put new_item into the buffer
  int new_item = generate_new_item();
  


  printf("Writing value, current buffer size %d\n",*count);
}

void read_buffer() {
  // TODO: read an item from the buffer
  


  printf("Reading value, current buffer size %d\n", *count);
}

void producer() {  
  for (int i = 0; i < 10; i++) {
    printf("PRODUCER:: ");
    write_buffer();
    sleep(2);
  }
}

void consumer() {  
  for (int i = 0; i < 10; i++) {
    printf("\t\tCONSUMER:: ");
    read_buffer();
    sleep(1);
  }
}

int main(void) {
  printf("Bounded buffer program\n");
  init();

  int pid = fork();
  if (pid < 0) {
    exit(0);
  }
  else if (pid == 0) {
    consumer();
    exit(0);
  }
  else if (pid > 0) {
    producer();
    wait(NULL);

    // Unlink shared memory
    shm_unlink("PROD_CONS");

    // TODO: unlink the semaphores


    exit(0);
  }

  return 0;
}

The skeleton code starts two processes (one as a producer and the other one as a consumer). A shared buffer (called "buffer") is created and the shared variable "count" counts the number of items in the buffer (the maximum allowed buffer size is defined by a const int SIZE, set to 4). Your task is to complete read_buffer() and write_buffer() functions (marked by TODO).

NOTE that you need to update IN, OUT, and count variables as items are added and removed from the buffer. You also need to add appropriate semaphore calls around those updates for proper synchronization.

Refer to Figure 7.1 (producer process) and Figure 7.2 (consumer process) in the textbook for details.

Solutions

Expert Solution

Producer consumer problem is also known as bounded buffer problem. In this problem we have two processes, producer and consumer, who share a fixed size buffer. Producer work is to produce data or items and put in buffer. Consumer work is to remove data from buffer and consume it. The solution can be achieved using semaphores.

Please Note: I have written the program with my actual understanding of PC problem using mutex. Kindly have a look. Please make necessary changes if it is required.

Program

#include<stdio.h>

#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main()

{

int n;

void producer();

void consumer();

int wait(int);

int signal(int);

printf("\n1.Producer\n2.Consumer\n3.Exit");

while(1)

{

printf("\nEnter your choice:");

scanf("%d",&n);

switch(n)

{

case 1: if((mutex==1)&&(empty!=0))

producer();

else

printf("Buffer is full!!");

break;

case 2: if((mutex==1)&&(full!=0))

consumer();

else

printf("Buffer is empty!!");

break;

case 3:

exit(0);

break;

}

}

return 0;

}

int wait(int s)

{

return (--s);

}

int signal(int s)

{

return(++s);

}

void producer()

{

mutex=wait(mutex);

full=signal(full);

empty=wait(empty);

x++;

printf("\nProducer produces the item %d",x);

mutex=signal(mutex);

}

void consumer()

{

mutex=wait(mutex);

full=wait(full);

empty=signal(empty);

printf("\nConsumer consumes item %d",x);

x--;

mutex=signal(mutex);

}

Output

1.Producer
2.Consumer
3.Exit
Enter your choice:1

Producer produces the item 1
Enter your choice:2

Consumer consumes item 1
Enter your choice:2
Buffer is empty!!
Enter your choice:1

Producer produces the item 1
Enter your choice:1

Producer produces the item 2
Enter your choice:1

Producer produces the item 3
Enter your choice:1
Buffer is full!!
Enter your choice:3


Related Solutions

Below is an attempt to solve the producer-consumer problem. Is this a correct solution? If yes,...
Below is an attempt to solve the producer-consumer problem. Is this a correct solution? If yes, what is achieved and how? If not, what is the problem? Explain your answer in either case. (Note: Assume no syntax or compilation errors. You’re asked about the concurrency logic/correctness and the use of semaphores for this.) #define N 100                 /* number of slots in the buffer */ semaphore mutex = 1;          /* controls access to critical */ semaphore empty = N;          /* counts...
Find the consumer and producer surpluses by using the demand and supply functions, where p is...
Find the consumer and producer surpluses by using the demand and supply functions, where p is the price (in dollars) and x is the number of units (in millions). Demand Function Supply Function p = 1005 − 25x p = 42x consumer surplus $ producer surplus $
Synchronize producer and consumer threads that use 10-item buffer for communication. Write pseudo code for both...
Synchronize producer and consumer threads that use 10-item buffer for communication. Write pseudo code for both threads. Assume that you can use void Buffer::store(Item item) and Item Buffer::get() methods, but you need to explicitly ensure that you never store more than 10 items in the buffer (to prevent overflow).
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in the lecture notes. The solution presented in the notes uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this assignment, standard counting semaphores will be...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads.A brief overview is below. This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be “java ProducerConsumer...
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...
List and discuss the stages in the consumer decision process for new products. Give examples.
List and discuss the stages in the consumer decision process for new products. Give examples.
The five steps of the consumer buying process are: problem recognition, information search, evaluation of alternatives,...
The five steps of the consumer buying process are: problem recognition, information search, evaluation of alternatives, purchase decision, and post purchase evaluation. Think of a large purchase you‘ve recently made. Did you follow the steps of the consumer buying process when deciding? Evaluate each step and discuss whether you passed through each step or stage during your purchase decision. If you did not, which steps were skipped and why?
Reflect back to this week’s lecture and discussion where we learned about the consumer decision-making process....
Reflect back to this week’s lecture and discussion where we learned about the consumer decision-making process. In your own words, explain the process and how you would utilize it in creation of a marketing plan
In taking a new business into Egypt Define the problem: who is affected? Where are they?...
In taking a new business into Egypt Define the problem: who is affected? Where are they? What are the costs that an individual suffering from the problem bears?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT