Thread: pipes problem

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    pipes problem

    Hi all,

    I have been having problem with the unix ipc in a while. Cant really figure it out why i'm not able to read anything of the pipe at the parents process end, until the child gets killed. As you could see from the following snap of code which gets executed in the child. Here I create the child process and exec atec.exe. Thats a standalone process which accepts commands and prints the results to stdout. In which case i'm expecting it to be written on to the pipe, so that i can capture in the parent process. NOTE: atec will be in an infinite loop accepting commands, its never quit's until quit command is applied.

    Code:
    if( !fork() )
        {
            __LOG("Fork child(atec) - Sucess");
            __LOG("Redirecting child stdout & stdin");
            
            Close_Tunnel( 1);
            Close_Tunnel( 0 ); 
            
            if( ( dup2( __CHILD_WRITE,1 ) < 0 ) || ( dup2( __CHILD_READ,0 ) < 0 ) )
            {
                __ERROR( strerror( errno ) );
                __ERROR("Process exiting..." );
                return EXIT_FAILURE;
            }  
            Close_Tunnel( __PARENT_READ );
            Close_Tunnel( __PARENT_WRITE );
            
                                  
            __LOG("Starting process");
            
            if( execlp( __CHILD_PROCESS_PATH,__CHILD_PROCESS_PATH, NULL) == -1 )
            {  
                dup2( 1, __CHILD_WRITE );
                dup2(0, __CHILD_READ );
                close( __CHILD_WRITE );
                close( __CHILD_READ );
                __ERROR( strerror( errno ) );
                __ERROR("Process exiting..." );
                return EXIT_FAILURE; 
            }
        }
    Thats my Child code

    Code:
            if( ( StreamContainer = ReadTunnel( __PARENT_READ ) ) > 0 )
            {
                printf( StreamContainer );
                free( StreamContainer ); 
            } 
            WriteTunnel( __PARENT_WRITE, "QueryStatus" );
            
            if( ( StreamContainer = ReadTunnel( __PARENT_READ ) ) > 0 )
            {
                printf( StreamContainer );
                free( StreamContainer );
            }
    Parent Code.

    So when the command 'QueryStatus' is sent, i could see that child has received it and prints the results back onto stdout, which i should be able to read it in the parent. NOTE: I'm able to read message "starting process" which was written by child before exec the 'atec' process. But when I send the command 'quit' to 'atec' from parent. I'm able to read all the prints printed by atec after atec is quited. Why such a behaviour? How do re-direct exec stdout and in to child stdout and stdin? My thoughts were that it should anyway be using the child stdout and stdin?

    TIA

    ssharish
    Last edited by ssharish2005; 11-13-2010 at 06:40 PM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > __CHILD_WRITE
    a) what are all these things?
    b) Ewww on using underscores at the start of names. Don't you know they're reserved names?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Thanks for th reply Salem. Perhaps shouldn't have used underscores in this case. But they are all macros defines as follow

    Code:
    #define __CHILD_WRITE     ReadPipe[1]
    #define __CHILD_READ      WritePipe[0]
    #define __PARENT_READ     ReadPipe[0]
    #define __PARENT_WRITE    WritePipe[1]
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM