病例样本:
Calling fclose()
here after dup()
ing its file descriptor blocks until the child process has ended (presumably because the stream has ended).
FILE *f = popen("./output", "r");
int d = dup(fileno(f));
fclose(f);
However by manually performing the pipe()
, fork()
, execvp()
of the popen()
, and then dup()
ing the pipe's read file descriptor, closing the original does not block.
int p[2];
pipe(p);
switch (fork()) {
case 0: {
char *argv[] = {
"./output", NULL};
close(p[0]);