-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEP2.c
More file actions
63 lines (48 loc) · 1.05 KB
/
EP2.c
File metadata and controls
63 lines (48 loc) · 1.05 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define TRUE 1
#define FALSE 0
#define MAX 100
static int count = 0;
static int vez = 0;
void secaoCritica(){
count++;
printf("Count = %i. \n", count);
}
void *p0 (){
int meuId = 0;
int outro = 1;
int abacates = 0;
while(count < MAX){
while(vez != meuId){
abacates++;
}
secaoCritica();
vez = outro;
printf("Eu tenho %i abacates \n", abacates);
}
}
void* p1(){
int meuId = 1;
int outro = 0;
int bananas = 0;
while(count < MAX){
while(vez != meuId){
bananas++;
}
secaoCritica();
vez = outro;
printf("Eu tenho %i bananas! \n", bananas);
}
}
int main(){
pthread_t id1;
pthread_create(&id1, NULL, p0, NULL);
pthread_t id2;
pthread_create(&id2, NULL, p1, NULL);
int vezes = 0;
pthread_join(id1, NULL);
pthread_join(id2, NULL);
return 0;
}