Thread: Redirecting STDOUT to STDIN of a child process

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    Redirecting STDOUT to STDIN of a child process

    Hello, I'm trying to implement a basic shell in c, and so far i have the basic functions and command line input working great, and the & works find, however I'm trying to get pipes to work, my problem is that I'm getting a SIGPIPE when using the pipes

    here is a code snip it:
    Code:
    /* Fork the process to allow program being executed a new PID and 
     * Protect the integrity of the shell process.
     */
      pid = fork();
      pipe(data_pipe);   
      switch (pid) 
      {
         case -1:	/* fork failed. */
            perror("fork");
            return 0;
         case 0:		/* inside child process.  */
            close(STDIN_FILENO);
            dup2(data_pipe[1],STDIN_FILENO);
            close(data_pipe[0]);
            //close(data_pipe[1]); // may or may not need to do this... 
              
         /* creat a new command struct so we dont over write parent
          * processes shared memory
          */ 
            struct command_t command2;
            int lcv = 0;
    
           /* Initalize the command structure and allocate into memory */
            command2.name =  (char*)malloc(MAX_ARG_LEN);
            command2.argc = 0;
            command2.opv = (char*)malloc(MAX_ARG_LEN);
            for(lcv=0; lcv<MAX_ARGS;lcv++)
            {  command2.argv[lcv] = (char*)malloc(MAX_ARG_LEN);
            }          
                
            strcat(command->opv,"\n");    // hack job to make parsing work
     
            exeCommand(pathv,&command2,&command->opv);
                
         default:	/* inside parent process. */
            close(STDOUT_FILENO);
            dup2(data_pipe[1],STDOUT_FILENO);
            close(data_pipe[0]);
            //close(data_pipe[1]); // may or may not be needed.
            execvp(command->name,command->argv);
      }
    I've been working on this for days because from what i can tell it should be working fine... but I'm kinda new to some of these things so I'm sure I'm missing it. heck i might be doing this entirely wrong for all i know , i didn't want to post here cuz well i wanted to figure it out on my own but after a couple of days i'm starting get sleepy. if all the coded is needed i can post that too...

    any help anyone can offer would be much appreciated.
    Last edited by 0xception; 09-13-2005 at 10:51 PM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    This belongs in Linux programming.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    What you are doing now is

    child

    close stdin
    make pipeout as stdin
    close pipein

    parent

    close stdout
    make pipeout as stdout
    close pipein
    what you need to do is

    child

    close stdout
    make pipeout as stdout
    close stdin
    execute the command

    parent
    close stdin
    make pipein as stdin
    close pipeout
    execute the command

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Also, checkout this

    Pipes

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Okay thanks a lot for the replay i'll give that switch around a try and read up at that site, looks useful (bookmarked now)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Redirecting stdout, stderr and stdin
    By iwabee in forum Linux Programming
    Replies: 9
    Last Post: 05-16-2005, 05:42 PM