-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feu.cpp
185 lines (172 loc) · 6.27 KB
/
Feu.cpp
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
/*************************************************************************
Feu - Gestion des feux du carrefour
-------------------
début : 19/03/2015
copyright : (C) 2015 par Bai Emilien
e-mail : emilien.bai@insa-lyon.fr
*************************************************************************/
//---------- Réalisation de la tâche <Feu> (fichier Feu.cpp) ---
///////////////////////////////////////////////////////////////// INCLUDE
//-------------------------------------------------------- Include système
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/sem.h>
//------------------------------------------------------ Include personnel
#include "Feu.h"
#include "Outils.h"
#include "Constantes.h"
/////////////////////////////////////////////////////////////////// PRIVE
//------------------------------------------------------------- Constantes
//------------------------------------------------------------------ Types
//---------------------------------------------------- Variables statiques
static int dureeNS;
static int dureeEO;
static int etatNS;
static int etatEO;
static int tempsNS;
static int tempsEO;
static int * memoire_partagee;
static int semaphore;
//------------------------------------------------------ Fonctions privées
//static type nom ( liste de paramètres )
// Mode d'emploi :
//
// Contrat :
//
// Algorithme :
//
//{
//} //----- fin de nom
static void fin_tache(int numerosignal)
{
shmdt(NULL);
exit(0);
}
static void initialisation(int mem, int semFeux)
{
memoire_partagee = (int*) shmat(mem, NULL, 0);
semaphore = semFeux;
//Direction du handler de fin de tache
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler = fin_tache;
sigaction(SIGUSR2, &action, NULL);
//initialisation des fragments de mémoire partagée
memoire_partagee[INDICE_ETAT_FEU_NS]= VERT;
memoire_partagee[INDICE_ETAT_FEU_EO] = ROUGE;
memoire_partagee[INDICE_TEMPS_NS] = DUREE_INIT_NS;
memoire_partagee[INDICE_TEMPS_EO] = DUREE_INIT_EO;
//initailisation des variables statiques
dureeNS = DUREE_INIT_NS;
dureeEO = DUREE_INIT_EO;
etatNS = VERT;
etatEO = ROUGE;
tempsNS = DUREE_INIT_NS;
tempsEO = DUREE_INIT_NS + 5;
//Mises à jour de l'affichage
Effacer(COULEUR_AXE_NS);
Afficher(COULEUR_AXE_NS, "Vert");
Effacer(COULEUR_AXE_EO);
Afficher(COULEUR_AXE_EO, "Rouge ", GRAS,INVERSE);
}
static void moteur()
{
struct sembuf reserver = {0, -1, 0};
struct sembuf liberer = {0, 1, 0};
for(;;)
{
//Quand une temporisation arrive à 0, on passe dans l'état suivant
if (tempsNS==0)
{
switch(etatNS){
case VERT:
semop (semaphore, &reserver, 1);
memoire_partagee[INDICE_ETAT_FEU_NS] = ORANGE;
semop (semaphore, &liberer, 1);
etatNS = ORANGE;
tempsNS = 3;
Effacer (COULEUR_AXE_NS);
Afficher(COULEUR_AXE_NS, "Orange",GRAS,INVERSE);
break;
case ORANGE:
semop (semaphore, &reserver, 1);
memoire_partagee[INDICE_ETAT_FEU_NS] = ROUGE;
semop (semaphore, &liberer, 1);
etatNS = ROUGE;
tempsNS = dureeEO + 7;
//7= 2s de rouge + 3s de orange + 2s de rouge en commun
//Mise à jour de l'affichage
Effacer(COULEUR_AXE_NS);
Afficher(COULEUR_AXE_NS, "Rouge ",GRAS,INVERSE);
break;
case ROUGE:
semop (semaphore, &reserver, 1);
memoire_partagee[INDICE_ETAT_FEU_NS] = VERT;
semop (semaphore, &liberer, 1);
etatNS = VERT;
tempsNS = dureeNS;
Effacer(COULEUR_AXE_NS);
Afficher(COULEUR_AXE_NS, "Vert");
break;
}
}
if (tempsEO==0)
{
switch(etatEO){
case VERT:
semop (semaphore, &reserver, 1);
memoire_partagee[INDICE_ETAT_FEU_EO] = ORANGE;
semop (semaphore, &liberer, 1);
etatEO = ORANGE;
tempsEO = 3;
Effacer (COULEUR_AXE_EO);
Afficher(COULEUR_AXE_EO, "Orange",GRAS,INVERSE);
break;
case ORANGE:
semop (semaphore, &reserver, 1);
memoire_partagee[INDICE_ETAT_FEU_EO] = ROUGE;
semop (semaphore, &liberer, 1);
etatEO = ROUGE;
tempsEO = dureeNS + 7;
Effacer(COULEUR_AXE_EO);
Afficher(COULEUR_AXE_EO, "Rouge ",GRAS,INVERSE);
break;
case ROUGE:
semop (semaphore, &reserver, 1);
memoire_partagee[INDICE_ETAT_FEU_EO] = VERT;
semop (semaphore, &liberer, 1);
etatEO = VERT;
tempsEO = dureeEO;
Effacer(COULEUR_AXE_EO);
Afficher(COULEUR_AXE_EO, "Vert");
break;
}
}
//Mise à jour de l'affichage
Afficher(TEMPS_AXE_NS, tempsNS--);
Afficher(TEMPS_AXE_EO, tempsEO--);
//Lecture des durées en mémoire
semop (semaphore, &reserver, 1);
dureeNS = memoire_partagee[INDICE_TEMPS_NS];
dureeEO = memoire_partagee[INDICE_TEMPS_EO];
semop (semaphore, &liberer, 1);
//Mise à jour de l'affichage
Effacer(DUREE_AXE_NS);
Afficher(DUREE_AXE_NS, dureeNS);
Afficher(DUREE_AXE_EO, dureeEO);
//On boucle toute les secondes
sleep(1);
}
}
////////////////////////////////////////////////////////////////// PUBLIC
//---------------------------------------------------- Fonctions publiques
void Feu(int SharedMemory, int semFeux)
// Algorithme :
//
{
initialisation(SharedMemory, semFeux);
moteur();
} //----- fin de Nom