-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicsquare.c
521 lines (460 loc) · 12.3 KB
/
magicsquare.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Pagès Louis-César 2018 CMI
*
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <ctype.h>
/******************************************************************************
* Gestion des erreurs
*/
void raler(int syserr, const char *fmt, ...)
{
va_list ap ;
va_start (ap, fmt) ;
vfprintf (stderr, fmt, ap) ;
fprintf (stderr, "\n") ;
va_end (ap) ;
if (syserr)
perror ("") ;
exit (EXIT_FAILURE) ;
}
/******************************************************************************
*Fonctions du programme
*/
//Ma structure Carré.
typedef struct s_square{
int ** quad1;
int ** quad2;
int ** quad3;
int ** quad4;
int size;
} * Square;
//initialise les quatres parties du carré.
Square initializeSquare( int n)
{
int a = n/2, b = n/2;
if(n>325002)
exit(EXIT_FAILURE);
//on initialise la première dimension
Square res = malloc(sizeof(struct s_square));
res->size = n;
res->quad1 = malloc(sizeof(int *) * (a)); //on vérifie que la mémoire est
if(res->quad1 == NULL) // bien allouée.
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
res->quad2 = malloc(sizeof(int *) * (a));
if(res->quad2 == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
res->quad3 = malloc(sizeof(int *) * (a));
if(res->quad3 == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
res->quad4 = malloc(sizeof(int *) * (a));
if(res->quad4 == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
//puis la deuxième.
for(--a; a>=0; a--)
{
res->quad1[a] = malloc(sizeof(int) * b);
if(res->quad1[a] == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
res->quad2[a] = malloc(sizeof(int) * b);
if(res->quad2[a] == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
res->quad3[a] = malloc(sizeof(int) * b);
if(res->quad3[a] == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
res->quad4[a] = malloc(sizeof(int) * b);
if(res->quad4[a] == NULL)
{
perror("Couldn't allocate memory");
exit(EXIT_FAILURE);
}
}
return res;
}
//libère la mémoire occupé par la structure.
void freeSquare( Square q)
{
int i;
for(i=0; i< (q->size/2) ; i++) //deuxième dimension
{
free(q->quad1[i]);
free(q->quad2[i]);
free(q->quad3[i]);
free(q->quad4[i]);
}
free(q->quad1); // première dimension
free(q->quad2);
free(q->quad3);
free(q->quad4);
free(q);
}
void freeMatrix( int ** mat, int size)
{
int i;
for(i=0; i<size; i++)
{
free(mat[i]);
}
free(mat);
}
//procède aux changements entre partie du carré.
void transformSquare(Square sq)
{
int i, j, s = sq->size/2;
int sup;
for(i=s/2; i>=1; i--)
{
sup = sq->quad1[s/2][i];
sq->quad1[s/2][i] = sq->quad3[s/2][i];
sq->quad3[s/2][i] = sup;
}
for(i=(s/2)-1; i>=0; i--)
{
for(j=(s/2)-1; j>=0; j--)
{
sup = sq->quad1[i][j];
sq->quad1[i][j] = sq->quad3[i][j];
sq->quad3[i][j] = sup;
}
}
for(i=(s/2)+1; i<s; i++)
{
for(j=0; j<(s/2); j++)
{
sup = sq->quad1[i][j];
sq->quad1[i][j] = sq->quad3[i][j];
sq->quad3[i][j] = sup;
}
}
for(i=0;i<s;i++)
{
for(j=s-1; j>((s/2)+1); j--)
{
sup = sq->quad2[i][j];
sq->quad2[i][j] = sq->quad4[i][j];
sq->quad4[i][j] = sup;
}
}
}
// Permet de générer une partie de la struct Square.
void generateSquare(int n, int ** magicSquare)
{
// Initialisation de la position
int i = n/2;
int j = n-1;
int num;
// Calcule de la valeur d'une case
for (num=1; num <= n*n; )
{
if (i==-1 && j==n)
{
j = n-2;
i = 0;
}
else
{
//vérifie si nous ne sommes pas sur le coté droit
if (j == n)
j = 0;
// vérifie si nous ne sommes pas au dessus.
if (i < 0)
i=n-1;
}
if (magicSquare[i][j])
{
j -= 2;
i++;
continue;
}
else
magicSquare[i][j] = num++; //applique la valeur
j++; i--;
}
}
//affichage d'une matrice
void printMatrix(int ** matrix, int size)
{
int i, j;
int n = size;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}
}
//affichage de la structure Square
void printSquare(Square sq, int size)
{
int i, j;
int n = size;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%d ", sq->quad1[i][j]);
for (j=0; j<n; j++)
printf("%d ", sq->quad2[i][j]);
printf("\n");
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%d ", sq->quad3[i][j]);
for (j=0; j<n; j++)
printf("%d ", sq->quad4[i][j]);
printf("\n");
}
}
//transforme une Matrice (a.k.a tableau 2 dimensions) en un tableau
int * matrixToArray(int ** matrix, int size_m)
{
int * array = malloc(sizeof(int) * (size_m*size_m) );
int i, j;
int n = size_m;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
array[j+i*n] = matrix[i][j];
}
}
return array;
}
//transforme un tableau en Matrice
int ** arrayToMatrix(int * array, int size_m)
{
int ** matrix = malloc(sizeof(int *) * (size_m) );
int i, j;
int n = size_m;
for(i=0 ;i<n ;i++ )
{
matrix[i] = malloc(sizeof(int) * n);
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
matrix[i][j] = array[j+i*n] ;
}
}
return matrix;
}
//multiplication par un scalaire du quadrant calculé
int * addScal(int v, int * m, int size){
int i;
for(i=0; i < size; i++)
{
m[i] = m[i] + v;
}
return m;
}
static int received = 0;
//fonction à exécuter lors d'un signal
static void handler(int sig)
{
if (sig == SIGUSR1)
{
received ++;
}
}
//*************************** main ********************************************
int main(int argc, char const *argv[])
{
if (argc != 2)
{
fprintf (stderr, "need one integer \n") ;
exit (EXIT_FAILURE) ;
}
//obtenir la valeur de l'argument
int arg;
sscanf (argv[1],"%d",&arg);
if( (arg % 4) == 0)
{
fprintf (stderr, "integer cannot be divided by 4 \n") ;
exit (EXIT_FAILURE) ;
}
if( (arg % 2) != 0)
{
fprintf (stderr, "integer cannot be an odd \n") ;
exit (EXIT_FAILURE) ;
}
if( arg < 0)
{
fprintf (stderr, "integer cannot be negative \n") ;
exit (EXIT_FAILURE) ;
}
Square mySquare = initializeSquare( arg);
int k;
int n = arg / 2;
int ** support = malloc(sizeof(int *) * n);
if( support == NULL ) //si l'allocation échoue
{
perror("couldn't allocate memory \n");
exit(1);
}
for(k=0; k<n; k++)
{
support[k]= malloc(sizeof(int) * n);
if(support[k] == NULL)
{
perror("couldn't allocate memory \n");
exit(1);
}
}
int tube[2];
if(pipe(tube) != 0)
{
raler (1, "initialise pipe failed");
}
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGUSR1, &sa, NULL) == -1)
{
raler (1, "initialise sigaction failed");
}
int i;
pid_t * f_pid = malloc(sizeof(pid_t)*4);
for(i=0; i<4; i++)
{
f_pid[i] = fork();
switch (f_pid[i]) {
case -1: //erreur
raler (1, "cannot fork child %d",
f_pid[i]);
case 0: //fils
freeSquare(mySquare);
if(close(tube[0]) == -1){
raler (1, "cannot close stdout of tube: %d",
tube[0]);
}
generateSquare(n, support);
int * arr = matrixToArray(support, n);
switch (i) {
case 0:
break;
case 1:
addScal(2*(n*n),arr,n*n);
break;
case 2:
addScal(3*(n*n),arr,n*n);
break;
case 3:
addScal(1*(n*n),arr,n*n);
break;
}
while(!received);
//attend le signal du père
int m = 0;
for(m=1; m<=n; m++) //on découpe les envoies dans le tube
{ //pour pouvoir envoyer des quantités
received =0; // importantes de données
write(tube[1], &arr[n*m-n], sizeof(int)*n);
while(!received);
}
for(k=0; k<n; k++) //libère la mémoire allouée du programme
{
free(support[k]);
}
free(support);
free(arr);
free(f_pid);
exit(getpid());
default:
break;
}
}
//on libère la mémoire allouée pour support
for(k=0; k<n; k++)
{
free(support[k]);
}
free(support);
if(close(tube[1]) == -1)
{
raler (1, "cannot close stdin of tube: %d",
tube[1]);
}
int raison;
for(i=0; i<4; i++)
{
int * read_int = malloc(sizeof(int) * (n*n));
if(kill(f_pid[i], SIGUSR1) == -1) //envoie du signal au fils
{
raler (1, "cannot send kill signal to child nb : %d",i);
}
for(k=1; k<=n; k++) //reception séparée des données dans le tube
{
read(tube[0], &read_int[n*k-n], sizeof(int)*n);
if(kill(f_pid[i], SIGUSR1) == -1)
{
raler (1, "cannot send kill signal to child nb : %d",i);
}
}
if(wait(&raison) == -1)
{
raler (1, "error while waiting for child nb: %d",i);
}
if (WIFEXITED (raison)){
WEXITSTATUS (raison);
}
switch (i) {
case 0:
freeMatrix(mySquare->quad1, n); //on remplace le quad1 etc ..
mySquare->quad1 = arrayToMatrix(read_int,n);
break;
case 1:
freeMatrix(mySquare->quad2, n);
mySquare->quad2 = arrayToMatrix(read_int,n);
break;
case 2:
freeMatrix(mySquare->quad3, n);
mySquare->quad3 = arrayToMatrix(read_int,n);
break;
case 3:
freeMatrix(mySquare->quad4, n);
mySquare->quad4 = arrayToMatrix(read_int,n);
break;
}
free(read_int);
}
if(close(tube[0]) == -1){
raler (1, "cannot close stdout of tube: %d", //on ferme le tube
tube[0]);
}
transformSquare(mySquare); // on procède au changements entre cadrants
printSquare(mySquare, n); //affiche notre structure
freeSquare(mySquare); // libère la mémoire allouée
free(f_pid);
return 0;
}