producer_consumer.c - #include#include...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<stdio.h> #include<stdlib.h> int consumer_waiting=0; int producer_waiting=0; int size=5; int showno=0; int empty=5; int full=0; int in=0; int out=0; int chk=-10; int i,n; void show (int arr[],int r) { printf("\n\n _________________________________________________________"); if(showno==0) printf("\nNOTHING TO SHOW "); else { printf("\n |"); for(n=0; n<showno; n++) {printf(" %d |",arr[r]); r=(r+1)%size; } } printf("\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" \"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n"); printf("\n NUMBER OF PRODUCER WATING ARE : %d NUMBER OF CONSUMER WAITING ARE: %d", producer_waiting, consumer_waiting); } int semwait(int a) { if(a<0) { a--; chk=0; return a; } else { a--; chk=a; return a; } } int semsignal(int a) { if(a<0) { a++; chk=0; return a; } else { a++; chk=a; return a; } } void consumer(int arr[], int c) {
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
int chk; int a=empty; int x=0; int h; int l,m,n; if(c>0) { { for(i=0; i<c; i++) { if(full<=0) { consumer_waiting++; printf("\n NOW %d CONSUMER WAITING...",consumer_waiting);} else { full=semwait(full); empty=semsignal(empty); printf("\n CONSUMER GET THE VALUE: %d",arr[out]); out=(out+1)%size; showno--; show(arr,out); if(producer_waiting >0) { l=producer_waiting; while(showno!=size && producer_waiting!=0) {printf("\n ONE PRODUCER FREE \n INSERT A VALUE FOR THAT FREE PRODUCER :"); scanf("%d",&h); arr[in]=h;
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern