Tuesday, May 22, 2018

Program to demonstrate Producer-Consumer Problem

/* Program to demonstrate producer-consumer problem using shared memory.
*/

#include<stdio.h>
int n;
void main()
{

int Q[30];
int front,rear;
int value;
char rr;
int ch;
printf("Enter the size of the Buffer=");
scanf("%d",&n);
printf("\n");
n++;

front=rear=0;
do
{
printf("\nMENU");
printf("\n1.Producer");
printf("\n2.Consume");
printf("\n3.Display Inventory Status");
printf("\nEnter Your Choice=");
scanf("%d",&ch);
if(ch==1)
{

int value=1;
 if ((rear+1)%n==front)
{
printf("The Buffer is full\n");
printf("Unable to Produce\n");
}
 else
 {
rear=(rear+1)%n;
Q[rear]=value;
printf("\nItem Produced");
 }

}
if(ch==2)
{

 int val;
 if(front!=rear)
 {
front=(front+1)%n;
val=Q[front];
printf("\nItem Consumed");
//printf("The Consumed value is: "<<val;
 }
 else
 {
 printf("The Inventory is empty");
 printf("\nUnable to Consume Item");
 }

}
if(ch==3)
{

 int i;
 i=front;
 while(i!=rear)
 {
 i=(i+1)%n;
 printf("\n");
 printf("%d",Q[i]);
 printf("  ");
 }
}

printf("\nWANT TO RUN IT AGAIN=");
scanf("%s",&rr);
}while(rr=='y'||rr=='Y');

}
/*
void add(int Q[],int front,int &rear,int value)
 {
 if ((rear+1)%n==front)
{
printf("The Buffer is full\n");
printf("Unable to Produce\n");
}
 else
 {
rear=(rear+1)%n;
Q[rear]=value;
printf("\nItem Produced");
 }
}


 void del(int Q[],int &front,int rear)
 {
 int val;
 if(front!=rear)
 {
front=(front+1)%n;
val=Q[front];
printf("\nItem Consumed");
//printf("The Consumed value is: "<<val;
 }
 else
 {
 printf("The Inventory is empty");
 printf("\nUnable to Consume Item");
 }
 }


 void display(int Q[],int front,int rear)
 {
 int i;
 i=front;
 while(i!=rear)
 {
 i=(i+1)%n;
 cout<<endl;
 printf("%d",Q[i]);
 printf("  ");
 }
 }




OUTPUT SCREEN:


No comments:

Post a Comment

Project on Library Management System (C++)

=> The project titled Library Management system is Library management software for monitoring and controlling the transactions in a lib...