-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorra_cinese_sem.c
277 lines (203 loc) · 6.25 KB
/
morra_cinese_sem.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"../Lezioni/lib-misc.h"
#include <semaphore.h>
#include <stdbool.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
typedef enum{PLAYER1, PLAYER2, GIUDICE, TAB}thread_n;
typedef enum{CARTA=0, FORBICE, SASSO}mossa;
typedef struct{
mossa mossa[2];
bool done;
int winner;
sem_t sem[4];
}shared;
typedef struct{
pthread_t pid;
unsigned id;
int numero_partite;
shared *sh;
}thread_data;
void init_shared(shared *sh){
int err;
sh->done = 0;
if((err = sem_init(&sh->sem[PLAYER1], 0,1)) != 0){
exit_with_err("sem_init", err);
}
if((err = sem_init(&sh->sem[PLAYER2], 0,1)) != 0){
exit_with_err("sem_init", err);
}
if((err = sem_init(&sh->sem[GIUDICE], 0,0)) != 0){
exit_with_err("sem_init", err);
}
if((err = sem_init(&sh->sem[TAB], 0,0)) != 0){
exit_with_err("sem_init", err);
}
}
void destroy(shared *sh){
for(int i=0; i<4; i++){
sem_destroy(&sh->sem[i]);
}
free(sh);
}
void player(void *arg){
thread_data *td = (thread_data *)arg;
int err;
char mosse_txt[3][10];
strncpy(mosse_txt[0], "CARTA",6);
strncpy(mosse_txt[1], "FORBICE",8);
strncpy(mosse_txt[2], "SASSO",6);
while(1){
if((err = sem_wait(&td->sh->sem[td->id])) != 0){
exit_with_err("sem_wait", err);
}
if(td->sh->done){
break;
}
td->sh->mossa[td->id] = rand() % 3;
printf("[PLAYER %d] ha lanciato: %s \n", td->id +1, mosse_txt[td->sh->mossa[td->id]] );
//signal sul Giudice per far capire che ho inviato la mia mossa
if((err = sem_post(&td->sh->sem[GIUDICE])) != 0){
exit_with_err("sem_post", err);
}
}
pthread_exit(NULL);
}
int whowin(mossa *mosse){
if(mosse[0] == mosse[1]){
return 0;
}
if((mosse[0] == CARTA && mosse[1] == SASSO)
|| (mosse[0]== FORBICE && mosse[1] == CARTA)
|| (mosse[0]== SASSO && mosse[1] == FORBICE)){
return 1;
}
return 2;
}
void judge(void *arg){
thread_data *td = (thread_data *)arg;
int err;
char winner;
while(1){
//attendo il primo giocatore
if((err = sem_wait(&td->sh->sem[GIUDICE])) != 0){
exit_with_err("sem_wait_giudice", err);
}
//attendo il secondo giocatore
if((err = sem_wait(&td->sh->sem[GIUDICE])) != 0){
exit_with_err("sem_wait_giudice", err);
}
if(td->sh->done){
break;
}
//valuto chi vince
winner = whowin(td->sh->mossa);
if(winner == 0){
printf("[JUDGE] Pareggio \n");
if((err = sem_post(&td->sh->sem[PLAYER1])) != 0){
exit_with_err("sem_post", err);
}
if((err = sem_post(&td->sh->sem[PLAYER2])) != 0){
exit_with_err("sem_post", err);
}
}else{
td->sh->winner = winner;
if((err = sem_post(&td->sh->sem[TAB])) != 0){
exit_with_err("sem_post tab", err);
}
}
}
pthread_exit(NULL);
}
/*Il processo T, in caso di vittoria, aggiornerà la propria classifica interna e avvierà,
se necessario, una nuova partita. Sempre T, alla fine della serie di partite, decreterà
l'eventuale vincitore.*/
void tab(void *arg){
thread_data *td = (thread_data *)arg;
int err;
int score[2] = {0,0};
for(int i = 0 ; i<td->numero_partite; i++)
{
if((err = sem_wait(&td->sh->sem[TAB])) != 0){
exit_with_err("sem_post", err);
}
score[td->sh->winner -1] ++;
if(i< td->numero_partite-1){
printf("Ecco la classifica provvisoria: %d, %d \n", score[0], score[1]);
if((err = sem_post(&td->sh->sem[PLAYER1])) != 0){
exit_with_err("sem_post", err);
}
if((err = sem_post(&td->sh->sem[PLAYER2])) != 0){
exit_with_err("sem_post", err);
}
}
}
printf("Ecco la classifica finale %d, %d \n", score[0], score[1]);
if(score[0] == score[1]){
printf("La partita è finita in pareggio \n");
}
if(score[0] < score[1]){
printf("Ha vinto il Player2 \n");
}else if(score[0] > score[1]){
printf("Ha vinto il Player1 \n");
}
/*if((err = sem_wait(&td->sh->sem[TAB])) != 0){
exit_with_err("tab", err);
}*/
td->sh->done = 1;
if((err = sem_post(&td->sh->sem[PLAYER1])) != 0){
exit_with_err("sem_post", err);
}
if((err = sem_post(&td->sh->sem[PLAYER2])) != 0){
exit_with_err("sem_post", err);
}
if((err = sem_post(&td->sh->sem[GIUDICE])) != 0){
exit_with_err("sem_post", err);
}
if((err = sem_post(&td->sh->sem[GIUDICE])) != 0){
exit_with_err("sem_post", err);
}
pthread_exit(NULL);
}
int main(int argc, char **argv){
thread_data td[4];
shared *sh = malloc(sizeof(shared));
init_shared(sh);
int err;
srand(time(NULL));
if(argc != 2 && atoi(argv[1]) == 0){
printf("Usage %s <numero_partite> \n Il numero delle partite deve essere maggiore di 0 \n", argv[0]);
exit(1);
}
int numero_partite = atoi(argv[1]);
//inizializzare la struttra thread_data
for(int i = 0; i<4; i++){
td[i].id = i;
td[i].sh = sh;
td[i].numero_partite = numero_partite;
}
//creo i vari thread
if((err = pthread_create(&td[PLAYER1].pid, NULL, (void *)player, &td[PLAYER1])) != 0){
exit_with_err("create", err);
}
if((err = pthread_create(&td[PLAYER2].pid, NULL, (void *)player, &td[PLAYER2])) != 0){
exit_with_err("create", err);
}
if((err = pthread_create(&td[GIUDICE].pid, NULL, (void *)judge, &td[GIUDICE])) != 0){
exit_with_err("create", err);
}
if((err = pthread_create(&td[TAB].pid, NULL, (void *)tab, &td[TAB])) != 0){
exit_with_err("create", err);
}
//pthread join
for(int i = 0; i<4; i++){
if((err = pthread_join(td[i].pid, NULL)) != 0){
exit_with_err("create", err);
}
}
destroy(sh);
exit(EXIT_SUCCESS);
}