SCR/SCR3.1/devoir27/ex3/ex3.c
2023-12-10 15:48:26 +01:00

87 lines
1.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int ordre(int a, int b){
if (a < b)
return 1;
else
return 0;
}
int main(int argc, char** argv){
if (argc != 5){
printf("Usage : %s <entier1> <entier2> <entier3> <entier4>\n", argv[0]);
return EXIT_FAILURE;
}
pid_t x1, x2;
int p1[2], p2[2],tmp, a, b, c, d;
int x3 = strtod(argv[1], NULL), x4 = strtod(argv[2], NULL);
pipe(p1);
pipe(p2);
x1=fork();
x2=fork();
if (x1 > 0){
close(p1[0]);
write(p1[1],&x3, sizeof(int));
sleep(2);
write(p1[1],(int*) &x4, sizeof(int));
x3 = strtod(argv[3], NULL);
x4 = strtod(argv[4], NULL);
write(p2[1], &x3, sizeof(int));
sleep(2);
write(p2[1], &x3, sizeof(int));
close(p1[1]);
close(p2[1]);
dup(p1[0]);
read(p1[0], &a, sizeof(int));
sleep(2);
read(p1[0], &b, sizeof(int));
close(p1[0]);
dup(p2[0]);
read(p2[0], &c, sizeof(int));
sleep(2);
read(p2[0], &d, sizeof(int));
close(p2[0]);
printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
}
if (x1 == 0){
close(p1[1]);
read(p1[0], &x3, sizeof(int));
sleep(2);
read(p1[0], &x4, sizeof(int));
if (!ordre(x3, x4)){
tmp = x4;
x4 = x3;
x3 = tmp;
}
printf("x3 = %d, x4 = %d", x3, x4);
close(p1[0]);
write(p1[1], &x3, sizeof(int));
sleep(2);
write(p1[1], &x4, sizeof(int));
}
if (x2 == 0){
close(p2[1]);
read(p2[0], &x3, sizeof(int));
sleep(2);
read(p2[0], &x4, sizeof(int));
ordre(x3, x4);
if (!ordre(x3, x4)){
tmp = x3;
x3 = x4;
x4 = x3;
}
dup(p2[1]);
close(p2[0]);
write(p2[1], &x3, sizeof(int));
sleep(2);
write(p2[1], &x4, sizeof(int));
}
}