- I've a program which
popen()to another and alsodup()stdout - When called from another process (like the PHP example) or via SSH, the process does not exit.
process_test.c:
#include <stdio.h>
#include <unistd.h>
int main() {
int out;
out = dup(STDOUT_FILENO);
// close(out);
popen("sleep 10\0", "r");
}
Compile with gcc process_test.c, run with:
./a.out-> exits normallyruby -e 'system("./a.out");'-> exits normallyphp -r passthry("./a.out");-> hangsssh remotehost ./a.out-> hangs- when I don't
dupstdout or close the dup, it doesn't hang
This is the shortest reproducible code I could find which shows me a behavior I'd like to better understand.
It took hours to extract this from multiple PHP applications/frameworks using fork/pcntl/etc. to instrument their relations, i.e. I didn't wrote this or made this up; but obviously the whole sense of it got lost due me stripping everything apart.
Questions
- Why do some invocations hang (php, ssh) and other not (ruby) ?
- Even when I close the fd after the popen, my program hangs; why?