#include "main.h" //====================================================================== /* $ ps aux # a:all users, u:detailled, x:daemons // look at bash PID Unidirectional pipe : you have to choose the direction PARENT CHILD 1 >> pipe >> 0 X printf 0 << pipe << 1 <----- stdout << gunzip << stdin <-- inputFd */ //====================================================================== int main(int argc, char **argv) { int ok; int value=100; char *msg; char buffer[0x100]; int inputFd; bool usePause=false; if ( argc != 2 ) { printf("usage : ./prog11_proc_redir file_to_unzip \n"); return 0; } int pipeFd[2]; // Declare pipe, then create ok = pipe(pipeFd); if( ok == -1 ) { perror("pipe"); exit(1); } pid_t child=fork(); //>>>>>>>> CREATE CHILD >>>>>>>>>>>>>>>>>>>>>>>> switch(child) { case -1: perror("fork"); return EXIT_FAILURE; break; //############ CHILD CODE ###################################### case 0 : inputFd=open(argv[1],O_RDONLY); if(inputFd==-1) { perror("open"); exit(1); } ok = dup2(inputFd,0); if (ok==-1) { perror("dup2"); exit(1); } close(inputFd); // redirected so useless now close(pipeFd[0]); // child1 will not read from this pipe ok = dup2(pipeFd[1],1); if (ok==-1) { perror("dup2"); exit(1); } close(pipeFd[1]); // redirected so useless now char *args[]={ "sort", (char *)0 }; ok = execvp("sort",args); if (ok==-1) { perror("execvp"); exit(1); } break; //############ PARENT CODE ##################################### default : for(;;) { int r=read(pipeFd[0],buffer,0xFF); if(r==0) break; // end of file if(r==-1) { perror("read"); exit(1); } buffer[r]='\0'; printf("parent received [%s]\n",buffer); } close(pipeFd[0]); // done with reading int status; pid_t p=waitpid(child,&status,0); // PARENT MUST WAIT FOR HIS CHILD if(p==-1) { perror("waitpid"); return EXIT_FAILURE; } break; } return EXIT_SUCCESS; } //^^^^^^^^^^^^^^^^^^^^^^^^^^ EOF ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^