How to avoid read() from hanging in the following situation?
问题 I have some code that forks a third-party application and redirects its standard output to the parent process, roughly as follows (no error handling here for brevity): char* args[] = {"/path/to/3rd/party/binary", "with", "args", NULL}; int fds[2]; pipe2(fds, O_CLOEXEC); pid_t pid = fork(); if (pid == 0) { dup2(fds[1], STDOUT_FILENO); execvp(args[0], args); _exit(1); } else { close(fds[1]); char buf[1024]; int bytes_read; while ((bytes_read = read(fds[0], buf, sizeof buf - 1)) > 0) { buf[bytes