-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.c
130 lines (98 loc) · 2.68 KB
/
broadcast.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "broadcast.h"
/* START GLOBALS*/
int buffer[SIZE];
int totalReaders[SIZE];
// Counters
int nextSend = 0;
int waitSend = 0;
// Receivers Info
int n_receivers;
int* current;
// Lock for waitSend
sem_t lock;
sem_t waitSender;
// Mutual Exclusion
sem_t mExclusionW;
sem_t mExclusionR;
// Semaphores for Full and Empty Buffer
sem_t bufferFull[SIZE];
sem_t bufferEmpty[SIZE];
/* END GLOBALS */
int inicia(int transmissores, int receptores) {
int i;
int rc = 0;
n_receivers = receptores;
current = malloc(n_receivers * sizeof(int*));
for(i = 0; i < n_receivers; i++)
current[i] = 0;
for(i = 0; i < SIZE; i++)
totalReaders[i] = 0;
rc = sem_init(&lock, 0, 1);
checkResults("sem_init()\n", rc);
rc = sem_init(&waitSender, 0, 0);
checkResults("sem_init()\n", rc);
rc = sem_init(&mExclusionW, 0, 1);
checkResults("sem_init()\n", rc);
rc = sem_init(&mExclusionR, 0, 1);
checkResults("sem_init()\n", rc);
for(i = 0; i < SIZE; i++) {
rc = sem_init(&bufferFull[i], 0, 0);
checkResults("sem_init()\n", rc);
}
for(i = 0; i < SIZE; i++) {
rc = sem_init(&bufferEmpty[i], 0, 1);
checkResults("sem_init()\n", rc);
}
return TRUE;
}
void envia(int val) {
int i, tmpSend;
sem_wait(&mExclusionW);
tmpSend = nextSend % SIZE;
nextSend++;
sem_post(&mExclusionW);
sem_wait(&bufferEmpty[tmpSend]);
buffer[tmpSend] = val;
printf(">Inserting %d to position %d\n", val, tmpSend);
/*!<
< await(bufferEmpty[tmpSend] > 0) bufferEmpty[tmpSend]--; >
buffer[tmpSend] = val;
< bufferFull[tmpSend] = n_receivers; >
*/
for(i = 0; i < n_receivers; i++)
sem_post(&bufferFull[tmpSend]);
sem_wait(&lock);
for(i = 0; i < waitSend; i++)
sem_post(&waitSender);
waitSend = 0;
sem_post(&lock);
}
int recebe(int id) {
int item, position;
position = current[id] % SIZE;
/*!<
< await(current[id] < nextSend and bufferFull[position] > 0) bufferFull[position]--; >
item = buffer[position];
current[id]++;
*/
sem_wait(&lock);
if(current[id] >= nextSend) {
waitSend++;
sem_post(&lock);
sem_wait(&waitSender);
} else {
sem_post(&lock);
}
sem_wait(&bufferFull[position]);
item = buffer[position];
printf("<(Thread %d)Reading %d from position %d\n", id, item, position);
current[id]++;
sem_wait(&mExclusionR);
totalReaders[position]++;
if(totalReaders[position] >= n_receivers) {
totalReaders[position] = 0;
sem_post(&bufferEmpty[position]);
}
sem_post(&mExclusionR);
return item;
}