/* File name: th1.c */ /* Compile: gcc -lpthread -o th1 th1.c */ /* Run: ./th1 */ #include "pthread.h" #include "stdio.h"
#define N 6 int c=0; int buf[N]; int in,out; void * f1(void *m); int main(){ pthread_t th; int d,flag=0; pthread_create(&th,NULL,f1,NULL); while(1){ sleep(1); printf("\nEnter the data : "); scanf("%d",&d); flag=0; while(c==N){ if(flag==0) printf("\nFull!\n"); flag=1; } //buffer is full buf[in]=d; in=(in+1)%N; c=c+1; } return 0; } void * f1(void *m){ int d,flag=0; while(1){ sleep(3); flag=0; while(c==0){ if(flag==0) printf("\nEmpty!\n"); flag=1; } d=buf[out]; printf("\nConsumed item : %d",d); out=(out+1)%N; c=c-1; }}
|
|
No comments:
Post a Comment