126 lines
2.1 KiB
C
126 lines
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <err.h>
|
|
|
|
/*
|
|
__
|
|
___( o)>
|
|
\ <_. )
|
|
`---'
|
|
Coic Coic
|
|
*/
|
|
|
|
int main(){
|
|
|
|
//pipes
|
|
int pipe1[2];
|
|
int pipe2[2];
|
|
if(pipe(pipe2) == -1) return 1;
|
|
if(pipe(pipe1) == -1) return 1;
|
|
|
|
char buffer[2048];
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid < 0) return 1;
|
|
|
|
//Child
|
|
if(pid == 0) {
|
|
|
|
close(pipe1[1]);
|
|
close(pipe2[0]);
|
|
|
|
int n;
|
|
|
|
while(read(pipe1[0], &n, sizeof(int))){
|
|
|
|
int rslt = n * n;
|
|
write(pipe2[1], &rslt, sizeof(int));
|
|
}
|
|
|
|
close(pipe1[0]);
|
|
close(pipe2[1]);
|
|
exit(0);
|
|
|
|
}
|
|
|
|
//Teacher ? WTF what are you doing with my uncle ? Why do you have an eggplant in your right hand and a riding whip in your left hand ? WTF is going on here
|
|
else {
|
|
|
|
close(pipe1[0]);
|
|
close(pipe2[1]);
|
|
|
|
int nbr = 0;
|
|
while(fgets(buffer, 2047, stdin)){
|
|
|
|
nbr = atoi(buffer);
|
|
|
|
if(nbr == -1) break;
|
|
|
|
write(pipe1[1], &nbr, sizeof(int));
|
|
|
|
int rslt;
|
|
read(pipe2[0], &rslt, sizeof(int));
|
|
printf("The Oracle says the square is: %i\n", rslt);
|
|
}
|
|
|
|
close(pipe1[1]);
|
|
close(pipe2[0]);
|
|
|
|
wait(NULL); //on attends le Child
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
//int main() {
|
|
|
|
// clc();
|
|
|
|
/*
|
|
//[0] -> lecture | [1]-> ecriture
|
|
int pipe1[2];
|
|
int pipe2[2];
|
|
|
|
if(pipe(pipe1) == -1) return 1;
|
|
if(pipe(pipe2) == -1) return 1;
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid < 0) return 1;
|
|
|
|
//Child
|
|
else if (pid == 0) {
|
|
|
|
|
|
}
|
|
|
|
//Teacher ? WTF what are you doing here ?
|
|
else{
|
|
|
|
close(pipe1[0]);
|
|
close(pipe2[1]);
|
|
|
|
char buffer[2048];
|
|
fgets(buffer, 2047, stdin);
|
|
|
|
|
|
|
|
if(strcmp(buffer, "-1") == 0){
|
|
|
|
close(pipe2[0]);
|
|
close(pipe1[1]);
|
|
return 1;
|
|
}
|
|
|
|
pipe1[1] = buffer;
|
|
|
|
}*/
|
|
|
|
//}
|