0% found this document useful (0 votes)
260 views

Write A Program To Implement Producer Consumer Problem Using Semaphore.

The document describes an experiment to implement the producer-consumer problem using semaphores. It defines the producer-consumer problem as having a fixed size buffer that a producer can use to place items and a consumer can remove. It uses two counting semaphores - Full and Empty - to track the number of items in the buffer and empty slots. The producer waits if the buffer is full and signals when adding an item. The consumer waits if the buffer is empty and signals when removing an item. Code is provided that implements this using semaphores and prints the items produced and consumed.

Uploaded by

Yashwant Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
260 views

Write A Program To Implement Producer Consumer Problem Using Semaphore.

The document describes an experiment to implement the producer-consumer problem using semaphores. It defines the producer-consumer problem as having a fixed size buffer that a producer can use to place items and a consumer can remove. It uses two counting semaphores - Full and Empty - to track the number of items in the buffer and empty slots. The producer waits if the buffer is full and signals when adding an item. The consumer waits if the buffer is empty and signals when removing an item. Code is provided that implements this using semaphores and prints the items produced and consumed.

Uploaded by

Yashwant Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment -2

Student Name: ROHIL NAGAR UID:18BCS2925


Branch: CSE Section/Group:18-BCS-12-A

Subject Name: Real Time System Lab Subject Code: CSP-457

Aim: Write a program to Implement producer Consumer problem using semaphore.

Algorithm:

Prerequisite – Semaphores in operating system, Inter Process Communication

Producer consumer problem is a classical synchronization problem. We can solve this problem by using
semaphores.

A semaphore S is an integer variable that can be accessed only through two standard operations : wait() and
signal().

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

wait(S){

while(S<=0); // busy waiting S--;

signal(S){

S++; }
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.

Problem Statement – We have a buffer of fixed size. A producer can produce an item and can place in the
buffer. A consumer can pick items and can consume them. We need to ensure that when a producer is placing
an item in the buffer, then at the same time consumer should not consume any item. In this problem, buffer is
the critical section.

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 = n // All slots are empty initially Solution

for Producer –

do{

//produce an item

wait(empty);
wait(mutex);

//place in buffer

signal(mutex);

signal(full);

}while(true)

When producer produces an item then the value of “empty” is reduced by 1 because one slot will be filled now.
The value of mutex is also reduced to prevent consumer to access the buffer. Now, the producer has placed the
item and thus the value of “full” is increased by 1. The value of mutex is also increased by 1 beacuse the task
of producer has been completed and consumer can access the buffer. Solution for Consumer –

do{

wait(full);

wait(mutex);

// remove item from buffer

signal(mutex);

signal(empty);

// consumes item

}while(true)
As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1 and the value is
mutex is also reduced so that the producer cannot access the buffer at this moment. Now, the consumer has
consumed the item, thus increasing the value of “empty” by 1. The value of mutex is also increased so that
producer can access the buffer now

Code:

#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:

You might also like