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

前端 未结 3 636
失恋的感觉
失恋的感觉 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:28

    The FILE* returned by popen() should be closed by pclose(), not by fclose(). Then, the documentation for pclose() specifies:

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

    So the waiting is one of the two things pclose() does, besides closing the file descriptor. close() does just one thing: it closes the descriptor.

    In response to your second question, I think you can use the descriptor returned by fileno(). There's no need to dup() it. After you're done with it, pclose() the original.

提交回复
热议问题