In: Computer Science
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.
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