#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 >> PtC >> 0 X 0 << PtC << 1 X 0 << CtP << 1 X 1 >> CtP >> 0 X */ //====================================================================== int main(int argc, char **argv) { int ok; int value=100; char *msg; char buffer[0x100]; bool usePause=false; // Parse Args for(int i=1;i>>>>>>> CREATE CHILD >>>>>>>>>>>>>>>>>>>>>>>> switch(child) { case -1: perror("fork"); return EXIT_FAILURE; break; //############ CHILD CODE ###################################### case 0 : close(childToParent[0]); // child will not read from this pipe close(parentToChild[1]); // child will not write to this pipe msg="first message from child to parent\n"; write(childToParent[1],msg,strlen(msg)); if(usePause) { sleep(1); } msg="second message from child to parent\n"; write(childToParent[1],msg,strlen(msg)); close(childToParent[1]); // done with writing for(;;) { int r=read(parentToChild[0],buffer,0xFF); if(r==0) break; // end of file if(r==-1) { perror("read"); exit(1); } buffer[r]='\0'; printf("child received [%s]\n",buffer); } close(parentToChild[0]); // done with reading break; //############ PARENT CODE ##################################### default : close(parentToChild[0]); // parent will not read from this pipe close(childToParent[1]); // parent will not write to this pipe for(;;) { int r=read(childToParent[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(childToParent[0]); // done with reading msg="first message from parent to child\n"; write(parentToChild[1],msg,strlen(msg)); if(usePause) { sleep(1); } msg="second message from parent to child\n"; write(parentToChild[1],msg,strlen(msg)); close(parentToChild[1]); // done with writing 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^