-
Notifications
You must be signed in to change notification settings - Fork 0
/
programando_onlinely.c
126 lines (90 loc) · 2.6 KB
/
programando_onlinely.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
//
// Created by grilo on 11/05/2020.
//
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdnoreturn.h>
#define N_PHILOSOFER 5
pthread_t tid[N_PHILOSOFER];
int forks[N_PHILOSOFER];
#define TABLE -1
int phil[N_PHILOSOFER];
int philState[N_PHILOSOFER];
pthread_mutex_t forkMtx[N_PHILOSOFER];
#define THINKING 0
#define EATING 1
int getLeftForkID(int philID)
{
return philID;
}
int getRightForkID(int philID)
{
return (philID+1) % N_PHILOSOFER;
}
noreturn void* philosofer(void* id)
{
int myID = *((int *) id);
int lForkID = getLeftForkID(myID);
int rForkID = getRightForkID(myID);
int *leftFork = &(forks[lForkID]);
int *rightFork = &(forks[rForkID]);
//printf("\t\t\tp%d L f%d R f%d\n", myID, getLeftForkID(myID), getRightForkID(myID));
printf("p%d alive\n", myID);
while(1)
{
philState[myID] = THINKING;
printf("\t\tp%d thinking\n", myID);
pthread_mutex_lock(&(forkMtx[lForkID]));
*leftFork = myID;
// do
// {
// if (*leftFork == TABLE)
// {
// *leftFork = myID;
// }
//
// //printf("p%d try L f%d [owner = %d]\n", myID, getLeftForkID(myID), *leftFork);
// } while (*leftFork != myID);
printf("p%d got L f%d\n", myID, getLeftForkID(myID));
pthread_mutex_lock(&(forkMtx[rForkID]));
*rightFork = myID;
// do
// {
// if (*rightFork == TABLE)
// {
// *rightFork = myID;
// }
//
// //printf("p%d try L f%d [owner = %d]\n", myID, getRightForkID(myID), *rightFork);
// } while (*rightFork != myID);
printf("p%d got R f%d\n", myID, getRightForkID(myID));
philState[myID] = EATING;
printf("\t\tp%d eating\n", myID);
usleep(2000000);
*leftFork = TABLE;
*rightFork = TABLE;
pthread_mutex_unlock(&(forkMtx[lForkID]));
pthread_mutex_unlock(&(forkMtx[rForkID]));
}
}
int main()
{
printf("lancando as threads\n");
for(int i = 0; i < N_PHILOSOFER; ++i)
{
forks[i] = TABLE;
pthread_mutex_init(&(forkMtx[i]), NULL);
}
for(int i = 0; i < N_PHILOSOFER; ++i)
{
phil[i] = i;
pthread_create(&(tid[i]), NULL, philosofer, &(phil[i]));
}
for(int i = 0; i < N_PHILOSOFER; ++i)
{
pthread_join(tid[i], NULL);
printf("p%d finished\n", i);
}
printf("acabaram as threads\n");
}