c++ - Why does this code stop running when it hits the pipe? -


When this program is run then the parent goes through the loop, then when he writes the pipe The child switches.

Parents 4741 14087 (only 5 lines are expected)

Expected production (with randomly generated numbers):

Parents 4741 14087

Child 4740 47082

Parents 4741 11345

Children 4740 99017

Parents 4741 96744 < / P>

Hair 4740 98653 (when Variable 3 is given and the final number is a randomly generated number)

  #include & lt; Stdio.h & gt; # Include & lt; Iostream & gt; # Include & lt; Unistd.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Time.h> # Include & lt; Ctime & gt; using namespace std; Int main (int argc, char * argv []) {int pid = fork (), temp, randNum, count, pipeName [2], pipeName2 [2]; String converter; Pipes (pipeName); Conver = azr [1]; Temp = atoi (conver.c_str ()); Four letters; If (pid == 0) {// Child srand ((unsigned) time (zero) * getpid ()); // Close off unused pipe (pipeline 2 [1]); Close (pipeName [0]); // To switch for input loop (int i = 0; i   

The program ends when the child reads from the pipeline in the child.

your main mistake fork () before starting the pipe Both the parents and the child have their own personal (not shared through FD inheritance) pipe pair pipeName , and only parents start with the pipe FD PipeName2 .

For parents, there is no data to read behind pipe-names [0] . For the child ... who knows what the FD is writing? pipeName2 [1] ? If you are lucky that fail with EBADF.

Then, first pipes () twice, and then fork () , and see if things improve it happens.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -