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
| /* Shared variable */
typedef struct {
int road1; //Number of cars on the secondary road
int road2; //Number of cars on the primary road
} CarNumber;
CarNumber *carNumber;
/* variables used by several process */
pid_t pid;
int mutex = 1;
int fire = 0;
int end = 0;
int progTime;
Car *tab1, *tab2; // Waiting files that help us to schedule process
typedef struct GUI {
SDL_Surface *screen, *background, *redFire, *greenFire;
SDL_Rect *posBackground, *posFire1, *posFire2;
} *GUI;
/**********************************************************************
* Function that draw a car on the screen
**********************************************************************/
void drawCar (GUI gui, Car const c) {
SDL_BlitSurface(c -> image, NULL, gui -> screen, c -> position);
}
void initGui (GUI gui) {
SDL_Init(SDL_INIT_VIDEO);
gui -> screen = SDL_SetVideoMode(500, 500, 32, SDL_HWSURFACE);
gui -> background = SDL_LoadBMP("images/imagefond.bmp");
gui -> redFire = SDL_LoadBMP("images/feurouge.bmp");
gui -> greenFire = SDL_LoadBMP("images/feuvert.bmp");
SDL_SetColorKey(gui -> redFire, SDL_SRCCOLORKEY, SDL_MapRGB(gui -> redFire->format, 255, 255, 255));
SDL_SetColorKey(gui -> greenFire, SDL_SRCCOLORKEY, SDL_MapRGB(gui -> greenFire->format, 255, 255, 255));
gui -> posBackground -> x = 0;
gui -> posBackground -> y = 0;
gui -> posFire1 -> x = 0;
gui -> posFire1 -> y = 180;
gui -> posFire2 -> x = 0;
gui -> posFire2 -> y = 280;
}
void drawGui (GUI gui, int nbCars1, int nbCars2, int fire1, int fire2) {
int i;
SDL_BlitSurface(gui -> background, NULL, gui -> screen, gui -> posBackground);
if (fire1 == 0) SDL_BlitSurface(gui -> redFire, NULL, gui -> screen, gui -> posFire1);
else if (fire1 == 1) SDL_BlitSurface(gui -> greenFire, NULL, gui -> screen, gui -> posFire1);
if (fire2 == 0) SDL_BlitSurface(gui -> redFire, NULL, gui -> screen, gui -> posFire2);
else if (fire2 == 1) SDL_BlitSurface(gui -> greenFire, NULL, gui -> screen, gui -> posFire2);
for (i = 0; i < nbCars1; i++) {
drawCar(gui, tab1[i]);
}
for (i = 0; i < nbCars2; i++) {
drawCar(gui, tab2[i]);
}
SDL_Flip(gui -> screen);
}
/**********************************************************************
* cars' process
**********************************************************************/
void car(int roadNumber, int id) {
Car c = malloc (sizeof (Car*));
c -> id = id;
c -> position -> x = 0; c -> position -> y = 0;
c -> image = SDL_LoadBMP("images/bleu.bmp");
SDL_SetColorKey(c -> image, SDL_SRCCOLORKEY, SDL_MapRGB(c -> image ->format, 255, 255, 255));
P(mutex);
printf("\tVOITURE : arrivée de la voiture %d sur la voie %d\n", id, roadNumber);
if (roadNumber == 1) {
carNumber->road1++;
tab1[carNumber->road1-1] = c;
}
else {
carNumber->road2++;
tab2[carNumber->road2-1] = c;
}
printf("\tVOITURE : la voiture %d attend le feu\n", id);
if (roadNumber == 1)
printf("\tVOITURE : il y a %d voitures en attente\n", carNumber->road1);
else
printf("\tVOITURE : il y a %d voitures en attente\n", carNumber->road2);
V(mutex);
P(fire);
}
/**********************************************************************
* fires' process
**********************************************************************/
void fires(int roadNumber, int t) {
while (end != 1) {
int k;
printf("CARREFOUR : Le feu %d passe au rouge\n", roadNumber);
sleep(t);
V(fire);
printf("CARREFOUR : Le feu %d passe au vert\n", roadNumber);
sortTable (tab1, carNumber->road1);
sortTable (tab2, carNumber->road2);
if (roadNumber == 1){
printf("CARREFOUR : On libère %d voitures\n", carNumber->road1);
for (k = 0; k < carNumber->road1; k++)
printf("\tVOITURE : la voiture %d est passée\n", tab1[k] -> id);
}
else {
printf("CARREFOUR : On libère %d voitures\n", carNumber->road2);
for (k = 0; k < carNumber->road2; k++)
printf("\tVOITURE : la voiture %d est passée\n", tab2[k] -> id);
}
P(mutex);
if (roadNumber == 1){
carNumber->road1 = 0;
}
else {
carNumber->road2 = 0;
}
V(mutex);
sleep(t);
}
}
/**********************************************************************
* programm's time
**********************************************************************/
void programmTime () {
GUI gui;
initGui (gui);
while (end == 0) {
drawGui (gui, carNumber->road1, carNumber->road2, 1, 0);
progTime++;
printf("Programm's time%d\n", progTime);
sleep (1);
}
}
/**********************************************************************
* Main function
**********************************************************************/
int main (int nArg, char **args) {
char *filename = NULL;
int paramA = 0, paramN = CARSNUMBER, paramT;
int automatic = 0;
//Entry verification
if (entryVerification (nArg, args, ¶mA, ¶mN, ¶mT, &filename) == -1 || nArg > 7) {
printUsage ();
return 1;
}
//Case of a configuration file
if (filename != NULL) {
int n;
char **args2 = str_split (loadParam (filename, &n), " ");
if (entryVerification (n, args2, ¶mA, ¶mN, ¶mT, &filename) == -1) {
printUsage ();
return 1;
}
//printf("n = %d, command = %s, chaine = %s, p1 = %d, %d, %d\n", n, args2[2], loadParam (filename, &n), paramA, paramN, paramT);
}
int key, kMutex, kFire;
pid_t pidFire1, pidFire2, pidCar, p;
key = ftok("/etc/passwd",0);
carNumber = (CarNumber*)shmalloc(key,sizeof(CarNumber));
tab1 = (Car*)shmalloc(key+1,sizeof(Car)*100);
tab2 = (Car*)shmalloc(key+2,sizeof(Car)*100);
if (!carNumber || !tab1 || !tab2) {
fprintf(stderr, "erreur de creation de memoire partagee\n");
return 1;
}
/* Shared area initialisation */
carNumber->road1 = 0; /* There is no car on the secondary road at beginning */
carNumber->road2 = 0; /* There is no car on the primary road at beginning */
/* semaphore mutex's creation */
kMutex = key+1;
mutex = mutalloc(kMutex);
if (mutex == -1) {
fprintf(stderr, "erreur de creation de semaphore\n");
shmfree(key);
return 2;
}
/* semaphore fire's creation */
kFire = key+2;
fire = semalloc(kFire,0);
if (fire == -1) {
fprintf(stderr, "erreur de creation de semaphore\n");
shmfree(key);
mutfree(kMutex);
return 2;
}
//Programm's time
switch (pid=fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
/************************************/
/* process child 1 : Programm's time */
programmTime ();
return 0;
}
//Secondary road's process
switch (pid=fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
/************************************/
/* process child 2 : Secondary road */
fires(1, paramT);
return 0;
}
pidFire1 = pid;
//Primary road's process
switch (pid=fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
/************************************/
/* process child 3 : Primary road */
fires(2, 2 * paramT);
return 0;
}
pidFire2 = pid;
/************************************/
/* Cars' process */
//Automatic mode
if (paramA != 0) {
int i;
for (i = 1; i <= paramN; i++) {
switch (pid = fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
/**********************************/
/* process child i : Car i */
srandom (pid);
car(random () % 2 + 1, i);
return 0;
}
}
}
else { // Interactive mode
int i = 1;
char c;
switch (p = fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
while (1) {
c = mygetchar();
if (c == 'q') {
return 0;
}
if (c == 'k'){//If k touch is typed, a car is generated on the secondary road
switch (pid=fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
/**********************************/
/* pprocess child i : Car i */
car(1, i);
return 0;
}
}
else if (c == 'l') { //If l touch is typed, a car is generated o, the primary road
switch (pid=fork()) {
case -1: perror("Creation de processus");
return 3;
case 0:
/**********************************/
/* process child i : Car i */
car(2, i);
return 0;
}
}
i++;
bzero(&c, sizeof(c));
}
return 0;
}
}
end = 1;
/******************/
/* Father process */
/* attente du Fire pour sortir : permet de tuer les fils en meme temps que le pere sur un ^C */
waitpid(pid,0,0);
puts("PERE : fin du pere");
/* recuperation IPC */
printf("PERE : destruction memoire partagee et semaphores: %d\n", shmfree(key) + mutfree(mutex) + semfree(fire));
return 0; |
Partager