/
terminaux.c
executable file
·330 lines (281 loc) · 8.61 KB
/
terminaux.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
/*
Projet LO41, printemps 2008
Fichier : comm_shm.h
Terminaux de chargement
Auteurs : Jean-Christophe HERR, Hyacinthe CARTIAUX
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include "comm_shm.h"
#include "semaphore.h"
struct camion_buf {
long m_type; /* 0 remorque, 1 tracteur, 2 tracteur + remorque */
int id;
};
/* Création de la i-ème file, et enregistrement de son id en shm */
int creation_file(int i);
/* Lit un message (struct camion_buf) de la file */
struct camion_buf * lecture_file(int num_file);
/* Code du terminal */
void terminal(int i);
/* Chargement du train */
void chargement_train(struct train_info * t, struct camion_buf * c);
/* Fonction appelée lors de l'arrêt du processus, tue tous les processus fils */
void fin_processus();
int main(int argc,char **argv)
{
struct processus_info * p = get_processus_info_addr();
struct terminaux_info * t = get_terminaux_info_addr();
p->p_terminaux = getpid();
int j;
initGet(18, "lo41");
/* création des 12 files*/
for(j = 1; j < 13; j++)
{
creation_file(j);
}
/* On remplit les files de chaque terminal de 3 camions */
for (j = 0; j < 36; j++)
{
t->term_to_load = j % 12 + 1;
V(S_GEN_CAMION);
P(S_TERMINAUX);
}
/* On lance les 12 terminaux, pour les mettre en attente d'autorisation de chargement du contrôleur*/
for (j = 0; j < 12; j++)
{
V(S_TERMINAUX);
}
sleep(1);
/* Lancement des terminaux dans des processus fils, créés par fork() */
if ( ( p->pid_t1 = fork() ) == 0 ) { p->pid_t1 = getpid(); terminal(1); exit(0); }
if ( ( p->pid_t2 = fork() ) == 0 ) { p->pid_t2 = getpid(); terminal(2); exit(0); }
if ( ( p->pid_t3 = fork() ) == 0 ) { p->pid_t3 = getpid(); terminal(3); exit(0); }
if ( ( p->pid_t4 = fork() ) == 0 ) { p->pid_t4 = getpid(); terminal(4); exit(0); }
if ( ( p->pid_t5 = fork() ) == 0 ) { p->pid_t5 = getpid(); terminal(5); exit(0); }
if ( ( p->pid_t6 = fork() ) == 0 ) { p->pid_t6 = getpid(); terminal(6); exit(0); }
if ( ( p->pid_t7 = fork() ) == 0 ) { p->pid_t7 = getpid(); terminal(7); exit(0); }
if ( ( p->pid_t8 = fork() ) == 0 ) { p->pid_t8 = getpid(); terminal(8); exit(0); }
if ( ( p->pid_t9 = fork() ) == 0 ) { p->pid_t9 = getpid(); terminal(9); exit(0); }
if ( ( p->pid_t10 = fork() ) == 0 ) { p->pid_t10 = getpid(); terminal(10); exit(0); }
if ( ( p->pid_t11 = fork() ) == 0 ) { p->pid_t11 = getpid(); terminal(11); exit(0); }
if ( ( p->pid_t12 = fork() ) == 0 ) { p->pid_t12 = getpid(); terminal(12); exit(0); }
/* Sémaphore initialisation passée */
V(S_INIT);
signal(SIGINT, fin_processus);
/* Attente des processus fils */
waitpid(p->pid_t1, NULL, 0);
waitpid(p->pid_t2, NULL, 0);
waitpid(p->pid_t3, NULL, 0);
waitpid(p->pid_t4, NULL, 0);
waitpid(p->pid_t5, NULL, 0);
waitpid(p->pid_t6, NULL, 0);
waitpid(p->pid_t7, NULL, 0);
waitpid(p->pid_t8, NULL, 0);
waitpid(p->pid_t9, NULL, 0);
waitpid(p->pid_t10, NULL, 0);
waitpid(p->pid_t11, NULL, 0);
waitpid(p->pid_t12, NULL, 0);
return 0;
}
int creation_file(int i)
{
struct terminaux_info * t = get_terminaux_info_addr();
key_t cle = ftok ("/etc", (char) i);
/* création de la i-ème file et enregistrement de son id dans la shm */
switch (i) {
case 1: return t->file_t1 = msgget(cle,IPC_CREAT | 0600); break;
case 2: return t->file_t2 = msgget(cle,IPC_CREAT | 0600); break;
case 3: return t->file_t3 = msgget(cle,IPC_CREAT | 0600); break;
case 4: return t->file_t4 = msgget(cle,IPC_CREAT | 0600); break;
case 5: return t->file_t5 = msgget(cle,IPC_CREAT | 0600); break;
case 6: return t->file_t6 = msgget(cle,IPC_CREAT | 0600); break;
case 7: return t->file_t7 = msgget(cle,IPC_CREAT | 0600); break;
case 8: return t->file_t8 = msgget(cle,IPC_CREAT | 0600); break;
case 9: return t->file_t9 = msgget(cle,IPC_CREAT | 0600); break;
case 10: return t->file_t10 = msgget(cle,IPC_CREAT | 0600); break;
case 11: return t->file_t11 = msgget(cle,IPC_CREAT | 0600); break;
case 12: return t->file_t12 = msgget(cle,IPC_CREAT | 0600); break;
default: return -1; break;
}
}
struct camion_buf * lecture_file(int num_file)
{
struct camion_buf * c = (struct camion_buf *) malloc(sizeof(struct camion_buf));
/* Lecture d'un camion sur la file num_file */
if (msgrcv(num_file, c, sizeof(struct camion_buf) - sizeof(long), 0, 0) == -1) { /* Erreur */
perror("msgrcv");
exit(1);
}
return c;
}
void terminal(int i)
{
/* Récupération des structures en shm */
struct terminaux_info * t = get_terminaux_info_addr();
struct train_info * train_info = get_train_info_addr();
struct camion_buf * c;
while (1)
{
P(S_TERMINAUX); // les files sont pleines
/* On lit le premier camion de la file du terminal, et on se place en attente d'autorisation de chargement par le contrôleur */
switch (i) {
case 1: c = lecture_file(t->file_t1);
t->nb_units_t1 = c->m_type ;
t->term_to_load = 1;
P(S_FILE_1); break;
case 2: c = lecture_file(t->file_t2);
t->nb_units_t2 = c->m_type ;
t->term_to_load = 2;
P(S_FILE_2); break;
case 3: c = lecture_file(t->file_t3);
t->nb_units_t3 = c->m_type ;
t->term_to_load = 3;
P(S_FILE_3); break;
case 4: c = lecture_file(t->file_t4);
t->nb_units_t4 = c->m_type ;
t->term_to_load = 4;
P(S_FILE_4); break;
case 5: c = lecture_file(t->file_t5);
t->nb_units_t5 = c->m_type ;
t->term_to_load = 5;
P(S_FILE_5); break;
case 6: c = lecture_file(t->file_t6);
t->nb_units_t6 = c->m_type ;
t->term_to_load = 6;
P(S_FILE_6); break;
case 7: c = lecture_file(t->file_t7);
t->nb_units_t7 = c->m_type ;
t->term_to_load = 7;
P(S_FILE_7); break;
case 8: c = lecture_file(t->file_t8);
t->nb_units_t8 = c->m_type ;
t->term_to_load = 8;
P(S_FILE_8); break;
case 9: c = lecture_file(t->file_t9);
t->nb_units_t9 = c->m_type ;
t->term_to_load = 9;
P(S_FILE_9); break;
case 10: c = lecture_file(t->file_t10);
t->nb_units_t10 = c->m_type ;
t->term_to_load = 10;
P(S_FILE_10); break;
case 11: c = lecture_file(t->file_t11);
t->nb_units_t11 = c->m_type ;
t->term_to_load = 11;
P(S_FILE_11); break;
case 12: c = lecture_file(t->file_t12);
t->nb_units_t12 = c->m_type ;
t->term_to_load = 12;
P(S_FILE_12); break;
}
V(S_GEN_CAMION); /* On envoie un camion sur la file lue */
usleep(1000000);
/* Affichage */
if(c->m_type == 1) printf("terminal %d : tracteur %d\n", i, c->id);
if(c->m_type == 2) printf("terminal %d : remorque %d\n", i, c->id);
if(c->m_type == 3) printf("terminal %d : tracteur+remorque %d\n", i, c->id);
// chargement du train
chargement_train(train_info, c);
free(c); /* Libération de la mémoire */
V(S_CONTROLLEUR); /* Chargement fini, on redonne la main au contrôleur */
}
}
void chargement_train(struct train_info * t, struct camion_buf * c)
{
int j;
int wagon[2];
int demiwagon = -1;
wagon[0] = -1;
wagon[1] = -1;
for (j = 0; j < t->nbr_wagon; j++)
{
if (t->w[j].id1 == -1 && t->w[j].id2 == -1)
{
if (wagon[0] == -1)
wagon[0] = j;
else if (wagon[1] == -1)
wagon[1] = j;
}
else if (t->w[j].id1 == -1 || t->w[j].id2 == -1)
{
demiwagon = j;
}
}
if (c->m_type == 3)
{
if (demiwagon != -1 && wagon[0] != -1)
{
if (t->w[demiwagon].id1 == -1)
{
t->w[demiwagon].id1 = c->id;
}
else if (t->w[demiwagon].id2 == -1)
{
t->w[demiwagon].id2 = c->id;
}
}
else if (demiwagon != -1 && wagon[0] != -1)
{
if (t->w[demiwagon].id1 == -1)
{
t->w[demiwagon].id1 = c->id;
}
else if (t->w[demiwagon].id2 == -1)
{
t->w[demiwagon].id2 = c->id;
}
t->w[wagon[0]].id1 = c->id;
t->w[wagon[0]].id2 = c->id;
}
else if (wagon[0] != -1 && wagon[1] != -1)
{
t->w[wagon[0]].id1 = c->id;
t->w[wagon[0]].id2 = c->id;
t->w[wagon[1]].id1 = c->id;
}
}
else if (wagon[0] != -1 && c->m_type == 2)
{
t->w[wagon[0]].id1 = c->id;
t->w[wagon[0]].id2 = c->id;
}
else if (c->m_type == 1 && demiwagon != -1)
{
if (t->w[demiwagon].id1 == -1)
{
t->w[demiwagon].id1 = c->id;
}
else if (t->w[demiwagon].id2 == -1)
{
t->w[demiwagon].id2 = c->id;
}
}
}
void fin_processus()
{
struct processus_info * pi = get_processus_info_addr();
/* On termine les processus fils */
kill(pi->pid_t1, SIGINT);
kill(pi->pid_t2, SIGINT);
kill(pi->pid_t3, SIGINT);
kill(pi->pid_t4, SIGINT);
kill(pi->pid_t5, SIGINT);
kill(pi->pid_t6, SIGINT);
kill(pi->pid_t7, SIGINT);
kill(pi->pid_t8, SIGINT);
kill(pi->pid_t9, SIGINT);
kill(pi->pid_t10, SIGINT);
kill(pi->pid_t11, SIGINT);
kill(pi->pid_t12, SIGINT);
printf("\nArrêt des terminaux\n");
exit(0);
}