Hi All,
I have a question regarding the 2 ways pipes, which I'm having bit of trouble to get this working. I'm building test system framework for pre-build system. So I dont have the code to wrap up the system under test with my test framework. So I will have to use the binary. The system under test sometime required data from the user. But while testing it I need to send that data through the parent process. For example
Code://CHILD Process #include <stdio.h> #include <unistd.h> int main() { char buf[20]; printf("This is a test"); fgets(buf,20,stdin); printf("This is second string"); sleep(2); printf("This is thrid string"); return 0; }As you could see I trying to spawn child process and execv the child binary. And at some point the child requires user data which I'm trying to send through parent process. I haven't used pipes in ages and I'm missing something really fundamental. Could anyone point me out please?Code:// TEST FRAMEWRK #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <unistd.h> int main() { int readpipe[2]; int writepipe[2]; char buf[100]; int num; #define CHILD_WRITE readpipe[1] #define CHILD_READ writepipe[0] #define PARENT_READ readpipe[0] #define PARENT_WRITE writepipe[1] pipe(readpipe); pipe(writepipe); if( !fork() ) { //close(0); close(1); dup( CHILD_WRITE ); dup( CHILD_READ ); close( PARENT_READ ); close( PARENT_WRITE ); execl("./child","./child","","","",NULL); _exit(0); } else { printf("PARENT: reading from pipe\n"); close(0); close(1); dup( PARENT_READ ); dup( PARENT_WRITE ); close( CHILD_WRITE ); close( CHILD_READ ); while( (num = read( PARENT_READ, buf, sizeof buf ) ) ) // while( fgets( buf, sizeof buf, (FILE *)pfds1[0] ) ) { buf[num] = '\0'; open( 1 ); fprintf( stdout, "PARENT: read \"%s\"\n", buf); close(1); } write(PARENT_WRITE,"sad\n\r",5); while( (num = read( PARENT_WRITE, buf, sizeof buf ) ) ) // while( fgets( buf, sizeof buf, (FILE *)pfds1[0] ) ) { buf[num] = '\0'; open( 1 ); fprintf( stdout, "PARENT: read \"%s\"\n", buf); close(1); } } return 0; }
Thanks
Harish



LinkBack URL
About LinkBacks



