-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliftShaft.cpp
86 lines (63 loc) · 1.73 KB
/
liftShaft.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
#include "liftShaft.h"
#include "lift.h"
#include "liftController.h"
#include <sys/wait.h>
#include <iostream>
LiftShaft::LiftShaft(unsigned int cantidadPisos, unsigned int tiempoEntrePisos,
unsigned int capacidadAscensor, unsigned int cantidadDeAscensores) :
log("LiftShaft") {
this->capacidadAscensor = capacidadAscensor;
this->tiempoEntrePisos = tiempoEntrePisos;
this->cantidadPisos = cantidadPisos;
this->cantidadDeAscensores = cantidadDeAscensores;
}
int LiftShaft::run() {
pid_t pid; int status;
log.debug("run liftShaft");
switch (pid = fork()) {
case -1: return -1;
case 0: {
log.info("poniendo en marcha los ascensores!");
for( unsigned int i = 0; i<this->cantidadDeAscensores; i++ )
if( startNewLift(i) == 0 )
return 0;
waitAllLifts();
log.info("esperando que el controller termine");
return status;
}
default: {
log.debug("poniendo en marcha el Controller!");
LiftController liftController( this->cantidadDeAscensores );
status = liftController.work();
Logger::closeGlobalDebug(); // XXX: YUCK!
waitpid(pid, &status, 0);
return status;
}
}
}
int LiftShaft::startNewLift( int liftId ) {
pid_t pid;
switch (pid = fork()) {
case -1: return -1;
case 0: {
log.info("poniendo en marcha el ascensor!");
Lift lift(liftId, tiempoEntrePisos, capacidadAscensor);
lift.start();
return 0;
}
default: {
this->liftPids.push_back( pid );
return pid;
}
}
}
void LiftShaft::waitAllLifts() {
int status;
std::vector<pid_t>::iterator pids;
for(pids = liftPids.begin();
pids != liftPids.end();
pids++) {
waitpid((*pids), &status, 0);
}
liftPids.clear();
}