Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<omp.h>
- #define MAX 10
- void produce(int *q,int *rear,int *count)
- {
- int x=0;
- while(1)
- {
- if(*count<MAX)
- {
- *rear=(*rear+1)%MAX;
- q[*rear]=++x;
- printf("Producer produced %d\n",x);
- (*count)++;
- }
- else
- printf("Buffer full\n");
- fgetc(stdin);
- }
- }
- void consume(int *q,int *front,int *count)
- {
- while(1)
- {
- if(*count)
- {
- printf("Consumer consumed %d\n",q[*front]);
- *front=(*front+1)%MAX;
- (*count)--;
- }
- else
- printf("Buffer empty!\n");
- fgetc(stdin);
- }
- }
- int main()
- {
- omp_set_dynamic(0);
- int q[MAX],rear=-1,front=0,count=0;
- #pragma omp parallel num_threads(2)
- if(!omp_get_thread_num())
- produce(q,&rear,&count);
- else
- consume(q,&front,&count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment