double_pipe.c
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <err.h>
|
||||
|
||||
|
||||
|
||||
void clc(){
|
||||
|
||||
|
||||
//pipes
|
||||
int pipe1[2];// PIPE A
|
||||
int pipe2[2];// PIPE B
|
||||
if(pipe(pipe1) == -1) return;
|
||||
if(pipe(pipe2) == -1) return;
|
||||
|
||||
|
||||
char buffer[2048];
|
||||
char buffer2[2048];
|
||||
//fgets(buffer, 2047, stdin);
|
||||
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
if(pid < 0) return;
|
||||
if (pid == 0){
|
||||
|
||||
while(read(pipe1[0],buffer ,2047 )){
|
||||
int n = atoi(buffer);
|
||||
|
||||
if(n == -1) exit(0);
|
||||
char buf = n *n;
|
||||
|
||||
write(pipe2[1], &buf , 1);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
else{
|
||||
|
||||
read(pipe2[0], buffer2, 2048);
|
||||
if(strcmp(buffer2, "-1") == 0) {
|
||||
|
||||
close(pipe1[0]);
|
||||
close(pipe1[1]);
|
||||
close(pipe1[0]);
|
||||
close(pipe1[1]);
|
||||
return;
|
||||
}
|
||||
printf("The Oracle says the square is: %s\n", buffer2);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
clc();
|
||||
|
||||
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
}*/
|
||||
|
||||
}
|
||||
//}
|
||||
|
||||
@@ -20,11 +20,12 @@ int duper(char **argv, char *output){
|
||||
|
||||
//int fd[2];
|
||||
//pipe(fd);
|
||||
|
||||
if(!argv || !*argv || !output) return 1;
|
||||
|
||||
int wstatus;
|
||||
pid_t pid = fork();
|
||||
|
||||
if(pid < 0) return -1;
|
||||
if(pid < 0) return 1;
|
||||
|
||||
//Child
|
||||
else if (pid == 0){
|
||||
|
||||
@@ -60,6 +60,8 @@ char** split(char* line){
|
||||
|
||||
int exec_cmd(char** args){
|
||||
|
||||
if(!args || !*args) return -1;
|
||||
|
||||
int wstatus;
|
||||
pid_t pid = fork();
|
||||
|
||||
@@ -85,7 +87,7 @@ int exec_cmd(char** args){
|
||||
if(waitpid(pid, &wstatus, 0) == -1) return -1;
|
||||
|
||||
if( WIFEXITED(wstatus)){
|
||||
if(WEXITSTATUS(wstatus) == 255) return 1;
|
||||
if(WEXITSTATUS(wstatus) == 255) return -1;
|
||||
else return WEXITSTATUS(wstatus);
|
||||
}}
|
||||
return 0;
|
||||
@@ -135,7 +137,7 @@ int execution_loop(){
|
||||
//execution du run <commande> via le child
|
||||
else if (strcmp(args[0], "run") == 0){
|
||||
//printf("test\n");
|
||||
last_code = exec_cmd(args);
|
||||
if(args[1]) last_code = exec_cmd(args);
|
||||
}
|
||||
|
||||
//interrogation du last_code
|
||||
@@ -158,11 +160,11 @@ int is_terminal()
|
||||
{
|
||||
return isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
|
||||
}
|
||||
/*
|
||||
int main() {
|
||||
printf("\n Code de fin : %i\n",execution_loop());
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
int main(){
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user