pipes in c

This is a discussion on pipes in c within the C Programming forums, part of the General Programming Boards category; hi, i am trying to connect a maximum of up to three processes in c using pipes as the means ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    1

    pipes in c

    hi,
    i am trying to connect a maximum of up to three processes in c using pipes as the means of communication, each process should be forked from a single parent process but i'm having trouble with the pipes. I can find plenty of examples where a parent process foks a child and communicates with it through a pipe but nothing using anymore than that, ie i need an array of pipes(i think!). Any examples or pointers to how to go about this would be great.

    thanks very much.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is a quick modification of the first pipe example in "Unix Nextwork Programming" by W. Richard Stevens, to handle arrays. His example is just a quick demonstration of pipes. I modified it quickly and crudely to work with an array, with a slight variation on his error logging, to use standard prinf instead.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
            int pipefdarray[3][2], n,p;
            char buff[3][100];
    
            for( n = 0; n < 3; n++ )
                    if( pipe( pipefdarray[n] ) < 0 )
                    {
                            printf( "Error calling pipe on %d.\n", n );
                            exit( EXIT_FAILURE );
                    }
    
            for( n = 0; n < 3; n++ )
                    printf("read fd[%d] = %d, write fd[%d] = %d\n",
                            n, pipefdarray[n][0], n, pipefdarray[n][1] );
    
            for( n = 0; n < 3; n++ )
                    if( write( pipefdarray[n][1], "hello world\n", 12 ) != 12 )
                            printf( "Error writing on pipe %d\n", pipefdarray[n][1] );
    
            for( n = 0; n < 3; n++ )
                    if( (p=read( pipefdarray[n][0], buff[n], sizeof( buff[n] ) ) ) <= 0 )
                            printf( "Error reading on pipe %d\n", pipefdarray[n][0] );
    
            for( n = 0; n < 3; n++ )
                    write( 1, buff[n], p );
    
    
            return 0;
    }
    Quick copy/paste from vi, so the formatting may not be to your liking. However, it demonstrates an array of pipes, which is what you were looking for. Very simple really.

    On an aside, I would highly recommend the book I mentioned.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Hey Quzah,

    That book you mentioned is most likely way beyond where I am at right now, but still I am curious - I just did a quick check on amazon and they list a few different editions - looks like there is more than one volume - is yours a multi-volume set?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by kermit
    Hey Quzah,

    That book you mentioned is most likely way beyond where I am at right now, but still I am curious - I just did a quick check on amazon and they list a few different editions - looks like there is more than one volume - is yours a multi-volume set?
    Vol 2 is for IPC, so I guess that's the one Quzah's refering to. They're listed on Stevens' home page here:
    http://www.kohala.com/start/
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have the earlier single volume work. Hard cover, just called "Unix Network Programming". I haven't picked up the new three piece set. It's (the original) actually quite good. You can get it fairly inexpensively also, compared to what it originally retailed for. It's worth a read. Not overly complex, and he's a good author with good examples.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    sorry to bump a really old thread, but how would you implement this using dup2s and closes?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just because you are sorry for bumping an old thread doesn't make it ok or right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 02:47 PM
  2. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 04:09 AM
  3. Help - newbie playing with pipes
    By kotoko in forum C Programming
    Replies: 14
    Last Post: 10-21-2008, 05:41 PM
  4. Need some help with pipes please.
    By carrja99 in forum C Programming
    Replies: 1
    Last Post: 05-05-2004, 05:13 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 07:46 AM

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