How to create new process as its own parent process?

Can parent process(reading) and several child processes(writing) share one anonymous pipe?

  • My application is that a parent process creates a anonymous pipe and several child processes using fork(). Each child process sends data to the parent process through the write-end of the same anonymous pipe, while the parent process keeps reading from it. The sample code is just as belows.It seems to work, however I don't know the feasibility of the usage. Thanks for any suggestion. int fds[2]; pipe(fds); int i=0; char buf[][7]="child1","child2","child3","child4","child5"}; for(i=0; i<5; i++)     { if(fork()==0)         { close(fds[0]); write(fds[1], buf[i], strlen(buf[i])); printf("%s %d\n",buf[i], strlen(buf[i])); close(fds[1]); exit(0); } } while(1)     { //wait(0); read(fds[1], buf_r, strlen(buf_r)); printf("read:%s %d\n", buf_r, strlen(buf_r)); }

  • Answer:

    As far as I know, this is quite fine and safe to do.  Nice, tight example by the way. (except you omitted a definition for buf_r) You might look into when and where you call flush(), or better still you might want to use setlinebuf() to help ensure that writes dont interleave in ugly ways. When they do interleave inappropriately, it won't break anything but the data read by the parent will be less useful than otherwise. For example, child 1 decides to write back a big long string with only [a-z] characters - lowercase - followed by a carriage return, while child 2 decides to write back a big long string with only [A-Z] characters - uppercase - followed by a carriage return.  The parent could well receive this when reading from the pipe: ahsdfjkdshjksdahfJDAKFLADSHJFKHDSAJKFLHADSJKFLhdsjksafhjklsadhjksahjADSHFJKSHJFKLHSAJK asdfdsafds ...rather than: ashdfjklashfjklshdjfkldshjfklhsadjklhasjfklhdasjkflhjdskalfhjkdlsahfjdsaklhf HASJDKFLHJAKSDLHFJKDASLHFJKLDSAHJFKLHASJKLFHJASDKLHFJA Is this production-ready?  You can find out, for your particular OS, as follows. Rework the example some, so you can test it.  Make the program take 4 arguments - numchildren, writebufsize, numwrites, readbufsize. Each child will fill its buffer with writebufsize-1 'A'+childnumber characters, followed by a '\n'. It'll then write the buffer numwrites times, in a loop, before exiting. The parent simply loops, reading and printing readbufsize characters forever, or maybe until all of the children have exited. So, that's not too far from what you have now, and it lets you experiment. See how it handles edge cases like these: numchildren=10 writebufsize=10 numwrites=1 readbufsize=101  numchildren=1000 writebufsize=100 numwrites=100 readbufsize=10  numchildren=1000 writebufsize=100 numwrites=10 readbufsize=1000  ...etc.  Push the edges/limits.  See what happens with buffer sizes like 1023, 1024, 1025.  What happens if you tell 10,000 children to write 1 character each, 2 times?    A harness like that will let you learn how your IO logic works under stress. There's no better proof of how bulletproof it is than to actually test it.

Paul Reiber at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.