double_pipe.c
This commit is contained in:
@@ -14,56 +14,73 @@ ___( o)>
|
||||
Coic Coic
|
||||
*/
|
||||
|
||||
void clc(){
|
||||
int main(){
|
||||
|
||||
int n = 0;
|
||||
//pipes
|
||||
int pipe1[2];
|
||||
int pipe2[2];
|
||||
if(pipe(pipe2) == -1) return 1;
|
||||
if(pipe(pipe1) == -1) return 1;
|
||||
|
||||
char buffer[2048];
|
||||
|
||||
if(pipe(pipe2) == -1) return;
|
||||
if(pipe(pipe1) == -1) return;
|
||||
|
||||
fgets(buffer, 2047, stdin);
|
||||
n = atoi(buffer);
|
||||
pipe1[1] = n;
|
||||
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
if(pid < 0) return;
|
||||
if(pid < 0) return 1;
|
||||
|
||||
//Child
|
||||
if(pid == 0) {
|
||||
|
||||
while(1){
|
||||
close(pipe1[1]);
|
||||
close(pipe2[0]);
|
||||
|
||||
if(pipe1[0]){
|
||||
pipe2[1] = n* n;
|
||||
exit(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 {
|
||||
|
||||
if(n == -1) {
|
||||
close(pipe1[0]);
|
||||
close(pipe2[1]);
|
||||
return;}
|
||||
close(pipe1[0]);
|
||||
close(pipe2[1]);
|
||||
|
||||
pipe1[1] = n;
|
||||
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
|
||||
|
||||
printf("The Oracle says the square is: %i\n", pipe2[0]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
//int main() {
|
||||
|
||||
clc();
|
||||
// clc();
|
||||
|
||||
/*
|
||||
//[0] -> lecture | [1]-> ecriture
|
||||
@@ -105,4 +122,4 @@ int main() {
|
||||
|
||||
}*/
|
||||
|
||||
}
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user