Thursday, 15 July 2010

unix - pipe() from 1 parent to multiple child processes in separate c file -


I have a program that creates more than one child procedure using fork () is. The program starts in the parent.c section, after the foreing, calls the parent to execute Excel to execute child.c. How do I share a pipe between the two separate programs? I know that I would have to make a pipe for parent's process in parent.c:

  int myPipe [nChildren] [2]; Int i; For (i = 0; i kids; i ++) {if (pip ([i]) == -1) {mirror ("pipe error \ n"); Exit (1); } Closed (pipes [ii] [0]); / Parents do not need to be read}  

But what do I need to do in child.c?

To communicate the pipe fd to the hair process in the execl ad program the wanted. The easiest way to use dup2 to move the pipe to FD0 ( stdin ). For example:

  pid = fork (); If (pid == 0) {// in child dup2 (pipe [i] [0], 0); Execl (...); }  

Alternatively, you can use a command-line argument in child.c to accept the pipe's FD number. For example:

  pid = fork (); If (pid == 0) {// Children in sprint (pipe, "% d", pipe [i] [0]); Execl ("child", "child", pipe, (four *) faucet); }  

use the argv [1] to use the child code for atoi or strtoul You will need to use an integer and then input as FD.


No comments:

Post a Comment