sending the output of one process, to the input of another

This is a discussion on sending the output of one process, to the input of another within the C Programming forums, part of the General Programming Boards category; UPDATE: managaed to get this working thanks to a similar thread, and some adjustments here and there. So far in ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    sending the output of one process, to the input of another

    UPDATE: managaed to get this working thanks to a similar thread, and some adjustments here and there.


    So far in my program i:

    -Fork a new process

    inside the child, i exec a program entered by the user.

    I want to send the ouput of this program, down a pipe ( pipe 1 ), rather than to stdout.

    I then want to exec another program, sending it the information in pipe 1, and then outputting to stdout.

    Code:
    pid = fork();
    	if (pid == -1)
    	  {
    	    perror("dumbshell:fork");
    	    exit(1);
    	  }
    	
    	/* child */
    	if (pid == 0)
    	  {
    	    /* dont need the read end */
    	    close(fd[0]);
    	    /* output will now be send down out pipe */
    	    dup2(fd[1], STDOUT_FILENO);
    	    
    	    execvp(clist->exe_path,clist->arg);
    	    fprintf(stderr,"No such command: %s\n",clist->exe_path);		
    	    exit(1);
    	    
    	  }
    	else
    	  /* parent */	  
    	  {
    
    	    do
    	      {
    		npid = wait(&stat);
    		//printf("Process %d exited with status %d\n",npid,stat);
    	      }
    	    while (npid != pid);

    After the child exec's and exits, in the parent, i figure i create another child process, and exec in the newly created child.

    How though, do i set it up so the new process reads from pipe 1, and outputs to stdout.

    Hope I explained well enough for some to understand

    Appreciate the help.
    Last edited by purdy; 11-30-2005 at 06:42 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Code:
    dup2(fd[1], STDOUT_FILENO);
    You might want to see if that line is failing. I'm assuming you've already called pipe() and you've closed stdout.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  4. file input and output
    By isaac in forum C Programming
    Replies: 3
    Last Post: 06-04-2002, 04:41 PM
  5. Multi input to Output.
    By Krullt in forum C Programming
    Replies: 3
    Last Post: 09-25-2001, 02:07 PM

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