OPERATING SYSTM’s ALGORITHM (In LINUX OS) - PRODUCER CONSUMER PROBLEM
PRODUCER CONSUMER PROBLEM
PROGRAM:
#include<stdio.h>
#include<string.h>
main()
{
int i,size,count=0,c,x=1,no,h,buf[100];
printf(“\nEnter the buffer size”);
scanf(“%d”,&size);
do
{
printf(“\n1.Produce\n2.Consume\n3.View produced item\n4.Exit”);
printf(“\nEnter your choice”);
scanf(“%d”,&c);
switch(c)
{
case 1:
if(count>=size)
printf(“\nBuffer is full”);
else
{
printf(“\nEnter the number of elements to be produced”);
scanf(“%d”,&no);
if((count+no)>size)
printf(“\nEnter the number less than %d”,(size-count+1));
else
{
h=count;
for(i=++h;i<=(h+no-1);i++)
{
printf(“\nEnter the value”);
scanf(“%d”,&buf[i]);
count++;
}
for(i=++h;i<=(h+no-1);i++)
{
printf(“\nEnter the value”);
scanf(“%d”,&buf[i]);
count++;
}
}
}
case 2:
if(count==0)
printf(“\nBuffer is empty”);
else
{
for(i=1;i<=count;i++)
buf[i]=buf[i+1];
count--;
buf[i]=buf[i+1];
count--;
}
break;
case 3:
for(i=1;i<=count;i++)
printf(“\n%d”,buf[i]);
printf(“\nNumber of items in the buffer = %d”,count);
break;
printf(“\n%d”,buf[i]);
printf(“\nNumber of items in the buffer = %d”,count);
break;
}
printf(“\nTo continue press 1 else press 0”);
scanf(“%d”,&x);
printf(“\nTo continue press 1 else press 0”);
scanf(“%d”,&x);
}while(c!=4);
}
Comments
Post a Comment