Same pipe to multiple processes?

This is a discussion on Same pipe to multiple processes? within the C Programming forums, part of the General Programming Boards category; With several forks i plan to make several child processes. Then i need to send the same message to all ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Same pipe to multiple processes?

    With several forks i plan to make several child processes.
    Then i need to send the same message to all the child processes.

    Can i do this with one single pipe, from witch each child can read, or do i need one pipe to each child?

    Thank You

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yep.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    but do i need to send the message several times or just once?

    once a message is read from the pipe in one of the childs, is it still available for the other processes?

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    87
    I'm not sure the answer, but my initial was if the children all get an exact copy of the parent's file descriptors (and then by transitivity, the same as each other), then if one child reads from the pipe, that advances the position for all children.

    I cooked up a quick example to try and convince myself of this, and have succeeded.

    Running the code below gives me the following output:
    Code:
    Child 1's first read: test1
    Child 1's second read: test2
    Child 2's first read: test3
    Child 2's second read: test4
    My code is:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main(int argc, char **argv) {
      int  pfd[2];
      int  pid[2];
      int  status;
      char buf[6];
      
    
      if (pipe(pfd) < 0) {
        fprintf(stderr, "Error creating pipe.\n");
        exit(0);
      }
       
      /* First Child */
      if ((pid[0] = fork())== 0) {
        /* Check for first message */
        read(pfd[0], buf, 5);
        buf[5] = '\0';
        printf("Child 1's first read: %s\n", buf);
    
        /* Check for second message */
        read(pfd[0], buf, 5);
        buf[5] = '\0';
        printf("Child 1's second read: %s\n", buf);
    
        exit(0);
      }
    
      /* Second Child */
      if ((pid[1] = fork()) == 0) {
        /* Check for first message */
        read(pfd[0], buf, 5);
        buf[5] = '\0';
        printf("Child 2's first read: %s\n", buf);
    
        /* Check for second message */
        read(pfd[0], buf, 5);
        buf[5] = '\0';
        printf("Child 2's second read: %s\n", buf);
    
        exit(0);
      }
    
      /* Parent */
      write(pfd[1], "test1", 5);
      write(pfd[1], "test2", 5);
      write(pfd[1], "test3", 5);
      write(pfd[1], "test4", 5);
    
      waitpid(pid[0], &status, 0);
      waitpid(pid[1], &status, 0);
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    87
    I was just thinking...maybe an alternative to creating a pipe for each child would be to do multicasting with sockets? I couldn't tell you how to do it, but the concept might be what you are looking for.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    thank you. i will look in to that.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jason_m View Post
    I was just thinking...maybe an alternative to creating a pipe for each child would be to do multicasting with sockets? I couldn't tell you how to do it, but the concept might be what you are looking for.
    I think multicasting requires an IP address and so is not suitable for local sockets, but I could be wrong.

    However, using sockets could be an option if you don't want to create more than one file on disk (otherwise, you might as well create a pipe for each child and loop through those). You will still have to loop through and transmit to each connected child, tho, so it will amount to much the same thing.
    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

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    My proposition is to use shared memory: one process writes to it and the other read without any copying. You can use pipes as a synchronization mechanism if you need to, but message will not be copied or send multiple times.

    Daper
    http://www.linuxprogrammingblog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-06-2008, 01:17 PM
  2. Fork multiple processes()??
    By Paul22000 in forum C Programming
    Replies: 8
    Last Post: 11-12-2008, 03:47 PM
  3. shared libraries, datasegment and multiple processes
    By ashim_k1 in forum Linux Programming
    Replies: 1
    Last Post: 02-28-2008, 01:23 PM
  4. Multiple processes
    By cpsh007 in forum C Programming
    Replies: 10
    Last Post: 11-22-2007, 04:30 AM
  5. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM

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