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
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
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?
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:
My code is: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
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; }
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.
thank you. i will look in to that.
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
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