Advertisement
Bewin

interProcessCommunication

Mar 12th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int i=0,j=0,count=0,p[3],max=3;
  4.  
  5. void Producer()
  6. {
  7.     p[j]=1;
  8.     j=(j+1)%max;
  9.     ++count;
  10.     printf("\nProducer produces item %d", count);
  11.  
  12. }
  13.  
  14. void Consumer()
  15. {
  16.     p[i]=0;
  17.     count--;
  18.     printf("\nConsumer consumes item %d",count+1);
  19.     i=(i+1)%max;
  20. }
  21. int main()
  22. {
  23.     int n;
  24.     while(1){
  25.         printf("\nEnter your choice\n1.Produce\n2.Consume\n");
  26.         scanf("%d",&n);
  27.         switch(n){
  28.             case 1:
  29.                 if(count<3)
  30.                     Producer();
  31.                 else
  32.                     printf("Memory full\n");
  33.                     break;
  34.             case 2:
  35.                 if(count>0)
  36.                     Consumer();
  37.                 else
  38.                     printf("No products to consume\n");
  39.                 break;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement