Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

PRACTICAL-5

Aim: Prodecer Consumer Problem with Semaphores.


The producer consumer problem is a synchronization problem. There is a fixed size buffer
and the producer produces items and enters them into buffer. The consumer removes the
items from the buffer and consumes them.
A producer should not produce items into the buffer when the consumer is consuming an item
from the buffer and vice versa. So the buffer should only be accessed by the producer or
consumer at a time.
The producer consumer problem can be resolved using semaphores.

 Semaphores are of two types:


1. Binary Semaphore – This is similar to mutex lock but not the same thing. It can have
only two values – 0 and 1. Its value is initialized to 1. It is used to implement the
solution of critical section problem with multiple processes.
2. Counting Semaphore – Its value can range over an unrestricted domain. It is used to
control access to a resource that has multiple instances.

 The wait() operation reduces the value of semaphore by 1 and the signal()
operation increases its value by 1.

To solve this problem, we need two counting semaphores – full and empty. “full” keeps
track of number of items in the buffer at any given time and “empty” keeps track of number
of unoccupied slots.

Initialization of semaphores :
mutex = 1 full = 0 // Initially, all slots are empty. Thus full
slots are 0 empty = 3 // All slots are empty initially and capacity
of buffer is 3

Code:
#include<iostream>
using namespace std;
int mutex=1, full=0, impty=3, x=0;

int wait(int s) {
return (--s);
}
int signal(int s) {
return(++s);
}
Submitted By: Gurveer Singh Chauhan
Roll no. UE218044 Class: IT (4th semester) S-1, G-3
void producer() {
impty=wait(impty);
mutex=wait(mutex);
x++;
cout<<endl<<"Producer produces the item "
<<x<<endl; mutex=signal(mutex); full=signal(full);
}

void consumer() {
full=wait(full);
mutex=wait(mutex);
cout<<endl<<"Consumer consumes item
"<<x<<endl; x--;
mutex=signal(mutex);
impty=signal(impty);
}

int main(){
int n; void
producer();
void consumer();
int wait(int); int
signal(int);
cout<<endl<<"1.Producer"<<endl<<"2.Consumer"<<endl<<"3.Exit"<<endl; while(1){
cout<<endl<<"Enter your choice: ";
cin>>n;

switch(n) {
case 1:
if((mutex==1)&&(impty!=0))
producer();
else cout<<"Buffer is full!!";
break;

case 2:
if((mutex==1)&&(full!=0))
consumer();
else cout<<"Buffer is empty!!";
break;

case 3:
exit(0);
break;
}

}
Submitted By: Gurveer Singh Chauhan
Roll no. UE218044 Class: IT (4th semester) S-1, G-3
return 0;
}

OUTPUT:

1.Producer
2.Consumer
3.Exit

Enter your choice: 1


Producer produces the item 1
Enter your choice: 1
Producer produces the item 2
Enter your choice: 2
Consumer consumes item 2
Enter your choice: 1
Producer produces the item 2
Enter your choice: 1
Producer produces the item 3
Enter your choice: 3

Submitted By: Gurveer Singh


Roll no. Class: IT (4th semester) S-1,

You might also like