13 static int buf
[BUF_SIZE
];
15 void *prod(void *arg
);
16 void *cons(void *arg
);
22 sem_init(&args
.sem_free
, 0, BUF_SIZE
);
23 sem_init(&args
.sem_busy
, 0, 0);
24 pthread_create(&t1
, NULL
, prod
, &args
);
25 pthread_create(&t2
, NULL
, cons
, &args
);
26 pthread_join(t1
, NULL
);
27 pthread_join(t2
, NULL
);
34 int ip
= 0, nbprod
= 0, obj
= 1001;
35 args_t
*args
= (args_t
*) arg
;
36 while (nbprod
< MAX
) {
37 sem_wait(&args
->sem_free
);
39 sem_post(&args
->sem_busy
);
40 printf("prod: buf[%d]=%d\n", ip
, obj
);
43 ip
= (ip
+ 1) % BUF_SIZE
;
50 int ic
= 0, nbcons
= 0, obj
;
51 args_t
*args
= (args_t
*) arg
;
52 while (nbcons
< MAX
) {
54 sem_wait(&args
->sem_busy
);
56 sem_post(&args
->sem_free
);
57 printf("cons: buf[%d]=%d\n", ic
, obj
);
59 ic
= (ic
+ 1) % BUF_SIZE
;