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

  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 07:42 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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, 01:22 AM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11: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