Data from Child to Parent

This is a discussion on Data from Child to Parent within the C++ Programming forums, part of the General Programming Boards category; This program right now sends data from the parent to the child. I want to do the inverse. Any suggestions? ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Data from Child to Parent

    This program right now sends data from the parent to the child. I want to do the inverse. Any suggestions?

    Code:
    /* The parent will send messages to the child that
     * will be read from the keyboard.
     *
     * MESSAGE FORMAT: First two bytes show the length of the message to read.
     *                 The message will follow the first two bytes
     */
    
    
    #include <unistd.h>
    
    #include <iostream>
    #include <string>
    
    // Headers for wait
    #include <sys/types.h>
    #include <sys/wait.h>
    
    // Header needed for atoi function
    #include <stdlib.h>
    
    using namespace std;
    
    
    #define READ_PIPE 0
    #define WRITE_PIPE 1
    
    int main( int argc, char * argv[] )
    {
      cout << "Original Process: PID=" << getpid() << " PPID=" << getppid() << endl;
    
      // Create a pipe to be used to talk to the child
      int pipeParentFD[2];
      if( pipe(pipeParentFD) == -1 ) {
        cout << "Error, cannot create pipe" << endl;
        return 0;
      }
    
      int pid = fork();
      if( pid != 0 ) {
        // This is the parent process
        cout << "Parent Process: PID=" << getpid() << " PPID=" << getppid() << endl;
    
        // We must close the side of the pipes that we do not need
        close( pipeParentFD[READ_PIPE] );
    
        // Read a line from the keyboard and send it to the child
        string input;
        while( input != "exit" ) {
          getline( cin, input );
          char message[100];
          sprintf( message, "%02d%s", input.length(), input.c_str() );
          cout << "Parent Process: Sending message: " << message << endl;
    
          // Send the message to the child
          write( pipeParentFD[WRITE_PIPE], message, input.length() + 2 );
        }
    
        // Wait for the child to finish before exiting
        int status;
        pid = wait( &status );
    
        // Close the remaining pipe
        close( pipeParentFD[WRITE_PIPE] );
      }
      else {
        // This is the child process
        cout << "Child Process: PID=" << getpid() << " PPID=" << getppid() << endl;
    
        // We must close the side of the pipe that we do not need
        close( pipeParentFD[WRITE_PIPE] );
    
        while( true ) {
          // Read the first two bytes. These two bytes tell us how long
          // the message is going to be
          char length[3] = "  ";
          read( pipeParentFD[READ_PIPE], length, 2 );
          int messageLength = atoi(length);
    
          // Read the rest of the message
          char longMessage[256];
          read( pipeParentFD[READ_PIPE], longMessage, messageLength );
          longMessage[messageLength] = '\0';
          string output = longMessage;
    
          cout << "Child Process: Read message: " << output << endl;
          if( output == "exit" ) {
    	break;
          }
        }
    
        // Close the remaining pipe
        close( pipeParentFD[READ_PIPE] );
      }
    
      cout << "Terminating Process PID=" << getpid() << endl;
      return 0;
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by queen_ayeka View Post
    This program right now sends data from the parent to the child. I want to do the inverse. Any suggestions?
    Make another pipe. The child gets the write end, the parent gets the read end -- opposite of what you have now.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just reverse the closing of the ends, and reverse who's writing and who's reading. The principle is exactly the same.


    You really should test fork's return against -1, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 10:17 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Creating Child with parent of Child
    By SilkySmooth in forum Windows Programming
    Replies: 3
    Last Post: 06-28-2003, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21