-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProdCons.c
More file actions
98 lines (87 loc) · 2.49 KB
/
Copy pathProdCons.c
File metadata and controls
98 lines (87 loc) · 2.49 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <semaphore.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define BUF_SIZE 100
int buf[BUF_SIZE];
sem_t sem_2, sem_3, sem_non3, sem_cons, sem_prod, sem_full, sem_empty;
pthread_t prod, cons;
void *produttore(void *arg) {
int val, index_2 = 0, index_3 = 3, index_non3 = 1, index;
while (1) {
val = abs(rand()) % 100 + 1;
sem_wait(sem_empty);
sem_wait(sem_prod);
if (val % 2 == 0) {
index = index_2;
buf[index_2] = val;
sem_post(sem_2);
index_2 = (index_2 + 2) % BUF_SIZE;
} else if (val % 3 == 0) {
index = index_3;
buf[index_3] = val;
sem_post(sem_3);
do {
index_3 = (index_3 + 3) % BUF_SIZE;
} while (index_3 % 2 != 0);
} else {
index = index_non3;
buf[index_non3] = val;
sem_post(sem_non3);
do {
index_non3 = (index_non3 + 1) % BUF_SIZE;
} while (index_non3 % 2 != 0 && index_non3 % 3 != 0);
}
sem_post(sem_prod);
sem_post(sem_full);
printf("PROD: wrote %d \t at index %d \n", val, index);
sleep(rand() % 5);
}
}
void *consumatore(void *arg) {
int val_read, index = 0;
while (1) {
sem_wait(sem_full);
sem_wait(sem_cons);
if (index % 2 == 0) {
sem_wait(sem_2);
} else if (index % 3 == 0) {
sem_wait(sem_3);
} else {
sem_wait(sem_non3);
}
val_read = buf[index];
index = (index + 1) % BUF_SIZE;
sem_post(sem_cons);
sem_post(sem_empty);
printf("CONS: read %d \t at index %d\n", val_read, index - 1);
sleep(rand() % 5);
}
}
int PC() {
srand(time(NULL)); // Initialization, should only be called once.
sem_init(&sem_2, 1, 0);
sem_init(&sem_3, 1, 0);
sem_init(&sem_non3, 1, 0);
sem_init(&sem_cons, 1, 1);
sem_init(&sem_prod, 1, 1);
sem_init(&sem_full, 1, 0);
sem_init(&sem_empty, 1, BUF_SIZE);
sleep(2);
pthread_create(&prod, NULL, produttore, NULL);
sleep(2);
pthread_create(&cons, NULL, consumatore, NULL);
sleep(2);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&sem_2);
sem_destroy(&sem_3);
sem_destroy(&sem_non3);
sem_destroy(&sem_cons);
sem_destroy(&sem_prod);
sem_destroy(&sem_full);
sem_destroy(&sem_empty);
return 0;
}