IPC(Interprocess communication)

IPC PROGRAM,PRODUCER-CONSUMER PROGRAM,DOWNLOAD IPC PROGRAM.

#include<stdio.h>
int n=3,m,item,buf[3],i=0;
wait()
{
m=1;
printf("\n entering the critical section...");
}
signal()
{
m=0;
printf("\n ...quiting the critical section");
}
producer()
{
wait();
printf("\n enter the data to be produced");
scanf("%d",&item);
buf[i]=item;
i++;
printf("\n data produced");
signal();
}
consumer()
{
wait();
printf("\n The data %d has been consumed",buf[i-1]);
i--;
signal();
}
int main()
{
int ch;
clrscr();
start:
printf("\n1-producer");
printf("\n2-consumer");
printf("\n Enter choice:");
scanf("%d",&ch);
if(ch==1)
{
if(i==n)
printf("\n buffer is full");
else
producer();
goto start;
}

else if(ch==2)
{
if(i==0)
printf("\n no item to consume");
else
consumer();
goto start;
}
else
exit(0);
return 0;
}





Output

 PRODUCER CONSUMER PROBLEM

         Operations Are
1.PRODUCER –write
2.CONSUMER - get
3.BUFFER
4.EXIT

 Enter The choice of Operation1
 Enter The Value To Produce: 2
 Do you want to continue(1/2): 1
 Enter The Value To Produce: 4
 Do you want to continue(1/2): 2

         Operations Are
1.PRODUCER -write
2.CONSUMER - get
3.BUFFER
4.EXIT

 Enter The choice of Operation3
 The Buffer Has:2       4

         Operations Are
1.PRODUCER -write
2.CONSUMER - get
3.BUFFER
4.EXIT

 Enter The choice of Operation2
 Consumer Is Consuming the Item0
 Do You want to Continue(1/2)2

Operations Are
1.PRODUCER -write
2.CONSUMER - get
3.BUFFER
4.EXIT
 Enter The choice of Operation: 4

1 comment:

Retrospect said...

Thanks again for the codes. Indeed super helpful. This link with free GATE mock tests might be just the thing: http://thegateacademy.com/gate-free-test/