Note that there are some explanatory texts on larger screens.

plurals
  1. POChild process is blocked by full pipe, cannot read in parent process
    text
    copied!<p>I have roughly created the following code to call a child process:</p> <pre><code>// pipe meanings const int READ = 0; const int WRITE = 1; int fd[2]; // Create pipes if (pipe(fd)) { throw ... } p_pid = fork(); if (p_pid == 0) // in the child { close(fd[READ]); if (dup2(fd[WRITE], fileno(stdout)) == -1) { throw ... } close(fd[WRITE]); // Call exec execv(argv[0], const_cast&lt;char*const*&gt;(&amp;argv[0])); _exit(-1); } else if (p_pid &lt; 0) // fork has failed { throw } else // in th parent { close(fd[WRITE]); p_stdout = new std::ifstream(fd[READ])); } </code></pre> <p>Now, if the subprocess does not write too much to stdout, I can wait for it to finish and then read the stdout from <code>p_stdout</code>. If it writes too much, the write blocks and the parent waits for it forever. To fix this, I tried to wait with <code>WNOHANG</code> in the parent, if it is not finished, read all available output from p_stdout using <code>readsome</code>, sleep a bit and try again. Unfortunately, <code>readsome</code> never reads anything:</p> <pre><code>while (true) { if (waitid(P_PID, p_pid, &amp;info, WEXITED | WNOHANG) != 0) throw ...; else if (info.si_pid != 0) // waiting has succeeded break; char tmp[1024]; size_t sizeRead; sizeRead = p_stdout-&gt;readsome(tmp, 1024); if (sizeRead &gt; 0) s_stdout.write(tmp, sizeRead); sleep(1); } </code></pre> <p>The question is: Why does this not work and how can I fix it?</p> <p><b>edit:</b> If there is only child, simply using <code>read</code> instead of <code>readsome</code> would probably work, but the process has multiple children and needs to react as soon as one of them terminates.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload