fclose()/pclose() may block on some file pointers

前端 未结 3 635
失恋的感觉
失恋的感觉 2020-12-11 08:50

Calling fclose() here after dup()ing its file descriptor blocks until the child process has ended (presumably because the stream has ended).

<
3条回答
  •  旧巷少年郎
    2020-12-11 09:42

    http://linux.die.net/man/3/popen

    The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4().

    Since pclose() wants to return the exit status, it has to wait for the child to terminate and generate one. Since fclose() calls pclose(), the same applies to fclose() for you.

    If you fork and exec and do the rest yourself, you don't end up calling pclose() (directly or indirectly), so there's no waiting at close time. Note however that unless your program is set to ignore SIGCHLD, your process won't terminate (instead it'll go zombie) until the child does. But at least your cost will run to exit first.

提交回复
热议问题