help c programming

This is a discussion on help c programming within the C Programming forums, part of the General Programming Boards category; hi, i have a program that needs to connect 3 programs through a pipe. program1 stdout > stdin program2 stdout ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    help c programming

    hi,

    i have a program that needs to connect 3 programs through a pipe.

    program1 stdout > stdin program2 stdout >stdin program 3.


    i have so far created the following code below. iam just stuck on how to add the 3rd program and create a pipe to program 3

    any suggestions to point me in the right direction would be great.

    thanks heaps.

    Code:
    
    #include	<stdlib.h>
    #include	<stdio.h>
    #include	<unistd.h>
    #include 	<stdio.h>
    
    
    int main()
    {
    	int	pid,pid2 ;
    	int fd[2];
    	
    	pipe(fd);	
    
    	
    	if( (pid = fork() ) == -1 )
    	{
    		perror("fork"); 
    		exit(1);
    	}
    
    
    	/* child */
    	if ( pid == 0 )
    	{	
    		close(fd[1]);	
    		dup(fd[0]); // sets stdin to this end of pipe
    		close(fd[0]);
    						
    		execlp( "./prog1", "prog1", NULL );		/* and run	*/
    		perror("execlp");
    		exit(1);
    	}
    
    	/* parent waits then reports */
    	if ( pid != 0 )
    	{	
    		
    		close(fd[0]);
    		
    		
    		dup(fd[1]); // sets stdout to this end of pipe
    		close(fd[1]);
    
    		execlp("./prog2", "prog2", NULL);
    		wait(NULL);
    	
    	}
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    For the second program in the parent, use popen() instead of exec(). popen() will give you a pipe from the process opened, and you can take that output and apply it to prog3.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > dup(fd[0]); // sets stdin to this end of pipe
    No it doesn't.
    man dup(2)

    You either need to close(0) first, or use dup2()

    Also, only the child should do an exec() call, the parent should just wait.

    But you might want the parent to send something down pipe[1] so that the child can read it.
    Otherwise, not a lot is going to happen.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    6
    will i have to fork another process for this?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    6
    thanks for the response i will fix that up.

    so to run 3 programs is it best to create 3 child proccesses?

    iam just bit confused on how to pipe 3 processes.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    The parent will need two children, two pipes.
    Both children do lots of close, dup and exec.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    6
    sorry, just to clarify.

    will the best way would be to create 2 child proccesses for the first 2 processes and put the 3rd process in the parent and pipe them all together?

    thanks in advance

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    6
    anyone?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Yes, try it and see what happens.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    6
    thanks heaps, i will give it a shot.

Popular pages Recent additions subscribe to a feed

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