-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchand3.c
349 lines (258 loc) · 8.46 KB
/
chand3.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <time.h>
#include <mpi.h>
#define THREAD_NUM 3
#define CLOCK_QUEUE_SIZE 10
#define SNAPSHOT_MARKER -1
#define BUFFER_SIZE 10 // Tamanho do buffer para armazenar relógios
typedef struct {
int p[3];
int idProcess;
} Clock;
typedef struct {
int id;
Clock clock;
int snapshotPega;
} Process;
typedef struct {
Clock clock;
Clock in[BUFFER_SIZE];
Clock out[BUFFER_SIZE];
int flag; // Flag para identificar se é um marcador
} Snapshot;
pthread_mutex_t saidaMutex;
pthread_cond_t saidaCondEmpty;
pthread_cond_t saidaCondFull;
int saidaClockCount = 0;
Clock saidaClockQueue[CLOCK_QUEUE_SIZE];
pthread_mutex_t entradaMutex;
pthread_cond_t entradaCondEmpty;
pthread_cond_t entradaCondFull;
int entradaClockCount = 0;
Clock entradaClockQueue[CLOCK_QUEUE_SIZE];
void Event(int pid, Clock *clock) {
clock->p[pid]++;
printf("Processo: %d, Relógio: (%d, %d, %d)\n", pid, clock->p[0], clock->p[1], clock->p[2]);
}
Clock GetClock(pthread_mutex_t *mutex, pthread_cond_t *condEmpty, pthread_cond_t *condFull, int *clockCount, Clock *clockQueue) {
Clock clock;
pthread_mutex_lock(mutex);
while (*clockCount == 0) {
pthread_cond_wait(condEmpty, mutex);
}
clock = clockQueue[0];
for (int i = 0; i < *clockCount - 1; i++) {
clockQueue[i] = clockQueue[i + 1];
}
(*clockCount)--;
pthread_mutex_unlock(mutex);
pthread_cond_signal(condFull);
return clock;
}
void PutClock(pthread_mutex_t *mutex, pthread_cond_t *condEmpty, pthread_cond_t *condFull, int *clockCount, Clock clock, Clock *clockQueue) {
pthread_mutex_lock(mutex);
while (*clockCount == CLOCK_QUEUE_SIZE) {
pthread_cond_wait(condFull, mutex);
}
Clock temp = clock;
clockQueue[*clockCount] = temp;
(*clockCount)++;
pthread_mutex_unlock(mutex);
pthread_cond_signal(condEmpty);
}
void SendControl(int id, Clock *clock) {
Event(id, clock);
PutClock(&saidaMutex, &saidaCondEmpty, &saidaCondFull, &saidaClockCount, *clock, saidaClockQueue);
}
Clock* ReceiveControl(int id, Clock *clock) {
Clock* temp = clock;
Clock clock2 = GetClock(&entradaMutex, &entradaCondEmpty, &entradaCondFull, &entradaClockCount, entradaClockQueue);
if (clock2.idProcess == SNAPSHOT_MARKER) {
temp = &clock2;
return temp;
} //Se receber um marcador, retorna o marcador para sinalizar à thread Main que um marcador foi recebido
for (int i = 0; i < 3; i++) {
if (temp->p[i] < clock2.p[i]) {
temp->p[i] = clock2.p[i];
}
}
temp->p[id]++;
printf("Processo: %d, Relógio: (%d, %d, %d)\n", id, clock->p[0], clock->p[1], clock->p[2]);
return temp;
}
void Send(int pid, Clock *clock){
int mensagem[3];
mensagem[0] = clock->p[0];
mensagem[1] = clock->p[1];
mensagem[2] = clock->p[2];
//MPI SEND
MPI_Send(&mensagem, 3, MPI_INT, clock->idProcess, 0, MPI_COMM_WORLD);
}
void Receive(int pid, Clock *clock){
int mensagem[3];
//MPI RECV
MPI_Recv(&mensagem, 3, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
clock->p[0] = mensagem[0];
clock->p[1] = mensagem[1];
clock->p[2] = mensagem[2];
}
void InitiateSnapshot(Process* process) {
Snapshot snapshot;
snapshot.clock = process->clock;
for (int i = 0; i < BUFFER_SIZE; i++) {
snapshot.in[i] = entradaClockQueue[i];
snapshot.out[i] = saidaClockQueue[i];
}
process->snapshotPega = 1; //Sinaliza na estrutura que o processo já fez snapshot
Clock *clock = malloc(sizeof(Clock));
clock->idProcess = SNAPSHOT_MARKER;
//Imprime o Snapshot (Relógio, canal de entrada e canal de saída)
printf("Snapshot criado no processo %d. Clock: (%d, %d, %d)\n",
process->id, process->clock.p[0], process->clock.p[1], process->clock.p[2]);
printf("CANAL ENTRADA ");
for (int i = 0; i < entradaClockCount; i++){
printf("[(%d, %d, %d)] ", snapshot.in[i].p[0], snapshot.in[i].p[1], snapshot.in[i].p[2]);
}
printf("\nCANAL SAIDA: ");
for (int i = 0; i < saidaClockCount; i++){
printf("[(%d, %d, %d)] ", snapshot.out[i].p[0], snapshot.out[i].p[1], snapshot.out[i].p[2]);
}
printf("\n");
//Envia marcadores (-1) para os outros processos
for (int i = 0; i < 3; i++){
if (i != process->id){
clock->p[0] = -1;
clock->p[1] = -1;
clock->p[2] = -1;
clock->idProcess = i;
PutClock(&saidaMutex, &saidaCondEmpty, &saidaCondFull, &saidaClockCount, *clock, saidaClockQueue);
}
}
free(clock);
}
void *MainThread(void *args) {
long id = (long) args;
int pid = (int) id;
Clock* clock = malloc(sizeof(Clock));
// Inicializando os campos da estrutura Clock
clock->p[0] = 0;
clock->p[1] = 0;
clock->p[2] = 0;
clock->idProcess = pid;
Process process;
process.id = pid;
process.clock = *clock;
process.snapshotPega = 0;
if (pid == 0) {
Event(pid, clock);
clock->idProcess = 1;
SendControl(pid, clock);
clock = ReceiveControl(pid, clock);
clock->idProcess = 2;
SendControl(pid, clock);
if (!process.snapshotPega) {
InitiateSnapshot(&process);
}
clock = ReceiveControl(pid, clock);
clock->idProcess = 1;
SendControl(pid, clock);
Event(pid, clock);
} else if (pid == 1) {
clock->idProcess = 0;
SendControl(pid, clock);
clock = ReceiveControl(pid, clock);
clock = ReceiveControl(pid, clock);
} else if (pid == 2) {
Event(pid, clock);
clock->idProcess = 0;
SendControl(pid, clock);
clock = ReceiveControl(pid, clock);
}
return NULL;
}
void *SendThread(void *args) {
long pid = (long) args;
Clock clock;
while(1){
clock = GetClock(&saidaMutex, &saidaCondEmpty, &saidaCondFull, &saidaClockCount, saidaClockQueue);
Send(pid, &clock);
}
return NULL;
}
void *ReceiveThread(void *args) {
long pid = (long) args;
Clock clock;
while(1){
Receive(pid, &clock);
PutClock(&entradaMutex, &entradaCondEmpty, &entradaCondFull, &entradaClockCount, clock, entradaClockQueue);
}
return NULL;
}
// Representa o processo de rank 0
void process0(){
pthread_t thread[THREAD_NUM];
pthread_create(&thread[0], NULL, &MainThread, (void*) 0);
pthread_create(&thread[1], NULL, &SendThread, (void*) 0);
pthread_create(&thread[2], NULL, &ReceiveThread, (void*) 0);
for (int i = 0; i < THREAD_NUM; i++){
if (pthread_join(thread[i], NULL) != 0) {
perror("Falha ao juntar a thread");
}
}
}
// Representa o processo de rank 1
void process1(){
pthread_t thread[THREAD_NUM];
pthread_create(&thread[0], NULL, &MainThread, (void*) 1);
pthread_create(&thread[1], NULL, &SendThread, (void*) 1);
pthread_create(&thread[2], NULL, &ReceiveThread, (void*) 1);
for (int i = 0; i < THREAD_NUM; i++){
if (pthread_join(thread[i], NULL) != 0) {
perror("Falha ao juntar a thread");
}
}
}
// Representa o processo de rank 2
void process2(){
pthread_t thread[THREAD_NUM];
pthread_create(&thread[0], NULL, &MainThread, (void*) 2);
pthread_create(&thread[1], NULL, &SendThread, (void*) 2);
pthread_create(&thread[2], NULL, &ReceiveThread, (void*) 2);
for (int i = 0; i < THREAD_NUM; i++){
if (pthread_join(thread[i], NULL) != 0) {
perror("Falha ao juntar a thread");
}
}
}
int main(int argc, char* argv[]) {
int my_rank;
pthread_mutex_init(&entradaMutex, NULL);
pthread_mutex_init(&saidaMutex, NULL);
pthread_cond_init(&entradaCondEmpty, NULL);
pthread_cond_init(&saidaCondEmpty, NULL);
pthread_cond_init(&entradaCondFull, NULL);
pthread_cond_init(&saidaCondFull, NULL);
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank == 0) {
process0();
} else if (my_rank == 1) {
process1();
} else if (my_rank == 2) {
process2();
}
/* Finaliza MPI */
pthread_mutex_destroy(&entradaMutex);
pthread_mutex_destroy(&saidaMutex);
pthread_cond_destroy(&entradaCondEmpty);
pthread_cond_destroy(&saidaCondEmpty);
pthread_cond_destroy(&entradaCondFull);
pthread_cond_destroy(&saidaCondFull);
MPI_Finalize();
return 0;
}