AIM : To implement producer consumer problem
ALGORITHM :
algorithm for wait(s)
step 1 : decrement the value of s
step 2 : return s
algorithm for signal(s)
step 1 : increment the value of s
step 2 : return s
algorithm for producer
step 1 : set mutex = wait(mutex)
step 2 : set full = signal(full)
step 3 : set empty = wait(empty)
step 4 : increment x
step 5 : display producer produces the the item x
algorithm for consumer
step 1 : set mutex = wait(mutex)
step 2 : set full = wait(full)
step 3 : set empty = signal(empty)
step 4 : display consumer consumes item x;
step 5 : decrement x
step 6 : set mutex = signal(mutex)
algorithm for main
step 1 : create a menu driven option
1.1 : producer
1.2 : consumer
1.3 : exit
step 2 : read the choice n
step 3 : if ch==1.1 then
3.1 : if mutex==1 and empty!=0
3.1.1 : call producer
3.2 : else display buffer full
step 4 : if ch==1.2 then
4.1 : if mutex==1 and full!=0 then
4.1.1 : call consumer
4.2 : else display buffer is empty
step 5 : if ch==1.3 then
5.1 : exit()
step 1 : decrement the value of s
step 2 : return s
step 1 : increment the value of s
step 2 : return s
algorithm for producer
step 1 : set mutex = wait(mutex)
step 2 : set full = signal(full)
step 3 : set empty = wait(empty)
step 4 : increment x
step 5 : display producer produces the the item x
algorithm for consumer
step 1 : set mutex = wait(mutex)
step 2 : set full = wait(full)
step 3 : set empty = signal(empty)
step 4 : display consumer consumes item x;
step 5 : decrement x
step 6 : set mutex = signal(mutex)
algorithm for main
step 1 : create a menu driven option
1.1 : producer
1.2 : consumer
1.3 : exit
step 2 : read the choice n
step 3 : if ch==1.1 then
3.1 : if mutex==1 and empty!=0
3.1.1 : call producer
3.2 : else display buffer full
step 4 : if ch==1.2 then
4.1 : if mutex==1 and full!=0 then
4.1.1 : call consumer
4.2 : else display buffer is empty
step 5 : if ch==1.3 then
5.1 : exit()
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=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("\nthe consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}
void main()
{
int ch;
do
{
printf("\n1.producer 2.consumer 3.exit\n");
printf("\nenter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if(mutex==1 && empty!=0)
producer();
case 2 : if(mutex==1 && full!=0)
consumer();
else
printf("\nbuffer is empty");
case 3 : exit(0);
}
}while(ch<=3);
}
int mutex=1,full=0,empty=3,x=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("\nthe consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}
void main()
{
int ch;
do
{
printf("\n1.producer 2.consumer 3.exit\n");
printf("\nenter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if(mutex==1 && empty!=0)
producer();
case 2 : if(mutex==1 && full!=0)
consumer();
else
printf("\nbuffer is empty");
case 3 : exit(0);
}
}while(ch<=3);
}
No comments:
Post a Comment