-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-producer-consumer.c
72 lines (62 loc) · 1.53 KB
/
basic-producer-consumer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
// When building, you must link with the external pthread library: for example, 'gcc basic-producer-consumer.c -lpthread'
#define BUFFER_SIZE 20
sem_t spaces;
sem_t items;
int counter = 0;
int* buffer;
int produce() {
++counter;
return counter;
}
void consume(int value) {
printf("Consumed: %d.\n", value);
return;
}
void* producer(void* arg) {
int p_index = 0;
while (counter < 10000) {
int v = produce();
sem_wait(&spaces);
buffer[p_index] = v;
p_index = (p_index + 1) % BUFFER_SIZE;
sem_post(&items);
}
pthread_exit(NULL);
}
void* consumer(void* arg) {
int c_index = 0;
int c_total = 0;
while (c_total < 10000) {
sem_wait(&items);
int temp = buffer[c_index];
buffer[c_index] = -1;
c_index = (c_index + 1) % BUFFER_SIZE;
sem_post(&spaces);
consume(temp);
++c_total;
}
pthread_exit(NULL);
}
int main(int argc, char** argv) {
buffer = malloc(BUFFER_SIZE * sizeof(int));
for (int i = 0; i < BUFFER_SIZE; i++) {
buffer[i] = -1;
}
sem_init(&spaces, 0, BUFFER_SIZE);
sem_init(&items, 0, 0);
pthread_t prod;
pthread_t con;
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&con, NULL, consumer, NULL);
pthread_join(prod, NULL);
pthread_join(con, NULL);
free(buffer);
sem_destroy(&spaces);
sem_destroy(&items);
pthread_exit(0);
}