-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntranceDoor.cpp
executable file
·320 lines (249 loc) · 8.08 KB
/
EntranceDoor.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
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
/*************************************************************************
EntranceDoor - description
-------------------
date : Feb. 19 2014
copyright : (C) 2014 Yannick Marion & Gustave Monod
e-mail : [email protected]
*************************************************************************/
//---- Realization of the <EntranceDoor> task (file EntranceDoor.cpp) ----
///////////////////////////////////////////////////////////////// INCLUDE
//--------------------------------------------------------- System include
#include <cstdio> // for perror
#include <unistd.h> // for exit
#include <signal.h> // for sigaction
#include <errno.h> // for EINTR
#include <map> // for child processes management
using namespace std;
#include <sys/ipc.h> // for all IPCS
#include <sys/shm.h> // for shmget
#include <sys/sem.h> // for semget
#include <sys/msg.h> // for msgget
#include <sys/wait.h>
//------------------------------------------------------- Personal include
#include "Heure.h"
#include "Outils.h"
#include "Information.h"
#include "EntranceDoor.h"
///////////////////////////////////////////////////////////////// PRIVATE
//-------------------------------------------------------------- Constants
//------------------------------------------------------------------ Types
//------------------------------------------------------- Static variables
static const unsigned int ENTRANCE_TEMPO = 1;
static TypeBarriere doorType;
//IPC
static struct ParkingLot *shmParkingLot;
static int shmMutexId;
static int mbCommandId;
static struct sembuf mutexAccess = MUTEX_ACCESS;
static struct sembuf mutexFree = MUTEX_FREE;
static int waitSemSetId;
static map<pid_t, TypeUsager> childrenPid;
static int const TEMPO = 0;
//------------------------------------------------------ Private functions
//------------------------------------------------------------- Init phase
static int init ( TypeBarriere type );
// How to use:
// Initialization process of the <EntranceDoor> task
//---------------------------------------------------------- Destroy phase
static void destroy ( );
// How to use:
// Destruction phase of the <EntranceDoor> task
static void endTask ( int signal );
// How to use:
// Handles the SIGUSR2 signal, by destroying the task and its children
//------------------------------------------------------------ Motor phase
static void carParked ( int signal );
// How to use:
// Handles the SIGCHLD signal, received when a car is parked
static void savePark ( unsigned int noParkingSpot, TypeUsager userType );
// How to use:
// Saves a car into the parking
// · Safe (mutex) write in the shared memory of the newly parked car
// · Displays all of its information in the right spot
//static type name ( parameter list )
// How to use:
//
// Contract:
//
// Algorithm:
//
//{
//} //----- End of name
static int init ( TypeBarriere type )
// Algorithm:
// Sets the signal handler, gets the command mailbox, attaches the shared
// memory to the shmParkingLot pointer, and returns its id
{
doorType = type;
struct sigaction action;
sigemptyset( &action.sa_mask );
action.sa_flags = 0;
action.sa_handler = endTask;
sigaction( SIGUSR2, &action, NULL );
action.sa_handler = carParked;
sigaction( SIGCHLD, &action, NULL );
// Getting the command mailbox
mbCommandId = msgget( ftok( PROGRAM_NAME, FTOK_CHAR ), RIGHTS );
// Getting the shared memory
// (characteristics can be found in Information module)
int shmId = shmget( ftok( PROGRAM_NAME, FTOK_CHAR ), SHM_SIZE, RIGHTS );
// Attaching shared memory
shmParkingLot = (struct ParkingLot *) shmat( shmId, NULL, 0 );
// Getting the shared memory mutex
shmMutexId = semget( ftok( PROGRAM_NAME, FTOK_CHAR ), MUTEX_NB, RIGHTS );
// Getting the semaphore set for the entrance doors to wait
waitSemSetId = semget( ftok( PROGRAM_NAME, FTOK_CHAR + 1 ),
NB_BARRIERES_ENTREE, RIGHTS );
return shmId;
} //----- End of init
static void destroy ( )
// Algorithm:
// Detaches the shared memory
{
// Detaching the shared memory
shmdt( shmParkingLot );
shmParkingLot = NULL;
_exit( EXIT_SUCCESS );
} //----- End of destroy
static void endTask ( int signal )
// Algorithm:
//
{
if ( SIGUSR2 == signal )
{
map<pid_t, TypeUsager>::iterator it;
for ( it = childrenPid.begin( ); it != childrenPid.end( ); ++it )
{
//Killing every child pid
kill( it->first, SIGUSR2 );
waitpid( it->first, NULL, 0 );
}
destroy( );
}
} //----- End of endTask
static void carParked ( int signal )
// Algorithm:
//
{
if ( SIGCHLD == signal )
{
pid_t child;
int status;
child = waitpid( -1, &status, WNOHANG );
if ( child > 0 && WIFEXITED( status ) )
{
savePark( WEXITSTATUS( status ), childrenPid[child] );
// Removes the pid from the "to be deleted" pid map
childrenPid.erase( child );
}
}
}
static void savePark ( unsigned int noParkingSpot, TypeUsager userType )
// Contract:
//
// Algorithm:
//
{
int status;
struct ParkedCar *pParkedCar =
&( shmParkingLot->parkedCars[noParkingSpot - 1] );
/* BEGIN shared memory exclusion */
do
{
status = semop( shmMutexId, &mutexAccess, 1 );
} while ( -1 == status && EINTR == errno );
pParkedCar->userType = userType; // parameter
pParkedCar->carNumber = ( shmParkingLot->nextCarNo )++;
pParkedCar->parkedSince = time( NULL );
semop( shmMutexId, &mutexFree, 1 );
/* END shared memory exclusion */
AfficherPlace( noParkingSpot, pParkedCar->userType,
pParkedCar->carNumber, pParkedCar->parkedSince );
} //----- End of savePark
static int waitForEmptySpot ( )
{
struct sembuf P = SemP( doorType - 1 );
// Waiting for a car to exit
return semop( waitSemSetId, &P, 1 );
} //----- End of waitForEmptySpot
static void request ( TypeUsager userType, time_t timeOfRequest )
{
int status;
struct WaitingCar *pWaitingCar =
&( shmParkingLot->waitingCars[doorType - 1] );
/* BEGIN shared memory exclusion */
do
{
status = semop( shmMutexId, &mutexAccess, 1 );
} while ( -1 == status && EINTR == errno );
pWaitingCar->userType = userType;
pWaitingCar->arrivalTime = timeOfRequest;
semop( shmMutexId, &mutexFree, 1 );
/* END shared memory exclusion */
AfficherRequete( doorType, userType, timeOfRequest );
} //----- End of request
static void clearRequest ( )
{
struct WaitingCar *pWaitingCar =
&( shmParkingLot->waitingCars[doorType - 1] );
/* BEGIN shared memory exclusion */
semop( shmMutexId, &mutexAccess, 1 );
pWaitingCar->userType = AUCUN;
semop( shmMutexId, &mutexFree, 1 );
/* END shared memory exclusion */
Effacer( ( TypeZone )( REQUETE_R1 + doorType - 1 ) );
} //----- End of request
////////////////////////////////////////////////////////////////// PUBLIC
//------------------------------------------------------- Public functions
// type Name ( parameter list )
// Algorithm:
//
//{
//} //----- End of Name
void EntranceDoor ( TypeBarriere type )
// Algorithm:
//
{
init( type );
for ( ; ; )
{
pid_t childPid;
struct EnterCommand command;
int status;
do
{
status = msgrcv( mbCommandId, &command, ENTER_CMD_SIZE,
doorType, 0 );
} while ( -1 == status && EINTR == errno );
DessinerVoitureBarriere( doorType, command.userType );
if ( shmParkingLot->fullSpots == NB_PLACES )
{
request( command.userType, time( NULL ) );
// Waiting for a car to exit
do
{
status = waitForEmptySpot( );
} while ( -1 == status && EINTR == errno );
clearRequest( );
}
/* BEGIN shared memory exclusion */
do
{
status = semop( shmMutexId, &mutexAccess, 1 );
} while ( -1 == status && EINTR == errno );
// Parking a car
if ( -1 == ( childPid = GarerVoiture( type ) ) )
{
perror( "Error trying to park a car" );
_exit( EXIT_FAILURE );
}
++( shmParkingLot->fullSpots );
semop( shmMutexId, &mutexFree, 1 );
/* END shared memory exclusion */
childrenPid.insert( make_pair( childPid, command.userType ) );
sleep( ENTRANCE_TEMPO ); // So as to avoid entrance collision
}
perror( "Error: exited the EntranceDoor loop" );
_exit( EXIT_FAILURE );
} //----- End of EntranceDoor