unix - Creating multiple child processes with a single pipe -
i need create 3 child processes, each of reads string command line arguments , writes string single pipe. parent read strings pipe , display 3 of them on screen. tried doing 2 processes test , printing 1 of strings twice opposed both of them.
#include <stdio.h> #include <unistd.h> int main (int argc, char *argv[]) { char *character1 = argv[1]; char *character2 = argv[2]; char inbuf[100]; //creating array max size of 100 int p[2]; // pipe descriptor array pid_t pid1; // defining pid1 of type pid_t pid_t pid2; // defining pid2 of type pid_t if (pipe(p) == -1) { fprintf(stderr, "pipe failed"); // pipe fail } pid1 = fork(); // fork if (pid1 < 0) { fprintf(stderr, "fork failed"); // fork fail } else if (pid1 == 0){ // if child process 1 close(p[0]); // close read end write(p[1], character1, sizeof(&inbuf[0])); // write character 1 pipe } else { // if parent, create second child process, child process 2 pid2 = fork(); if (pid2 < 0) { fprintf(stderr, "fork failed"); // fork fail } if (pid2 = 0) { // if child process 2 close(p[0]); // close read end write(p[1], character2, sizeof(&inbuf[0])); // write character 2 pipe } else { // if parent process close(p[1]); // close write end read(p[0], inbuf, sizeof(&inbuf[0])); // read pipe both children write printf("%s\n", inbuf); // print read(p[0], inbuf, sizeof(&inbuf[0])); // read pipe both children write printf("%s\n", inbuf); // print } } }
your code doesn't keep looping until there's no more data read. single read. doesn't check value returned read(), should.
i've abstracted fork() , write() (and error check) code function. seems work:
#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> static void child(int fd, const char *string) { pid_t pid = fork(); int len = strlen(string); if (pid < 0) { fprintf(stderr, "%.5d: failed fork (%d: %s)\n", (int)getpid(), errno, strerror(errno)); exit(1); } else if (pid > 0) return; else if (write(fd, string, len) != len) { fprintf(stderr, "%.5d: failed write on pipe %d (%d: %s)\n", (int)getpid(), fd, errno, strerror(errno)); exit(1); } else exit(0); } int main (int argc, char *argv[]) { char inbuf[100]; //creating array max size of 100 int p[2]; // pipe descriptor array if (argc != 4) { fprintf(stderr, "usage: %s str1 str2 str3\n", argv[0]); return 1; } if (pipe(p) == -1) { fprintf(stderr, "pipe failed"); // pipe fail return 1; } (int = 0; < 3; i++) child(p[1], argv[i+1]); int nbytes; close(p[1]); // close write end while ((nbytes = read(p[0], inbuf, sizeof(inbuf))) > 0) printf("%.*s\n", nbytes, inbuf); // print return 0; } i ran command multiple times, each time using command line:
./p3 'message 1' 'the second message' 'a third message third process' on 1 run, output was:
the second messagemessage 1 third message third process on another, got:
the second messagemessage 1a third message third process and on another, got:
message 1 second messagea third message third process (this on macbook pro intel core i7, running mac os x 10.8.3, , using gcc 4.7.1.)
Comments
Post a Comment