为什么在这里使用进程替换会导致挂起?
Why does using process substitution here cause a hang?
我有一个程序需要启动其他程序,可能用文件和管道替换它们的 stdio。虽然在 "work" 看来,子进程确实从源管道中获取了 I/O,但不幸的是,它也会导致挂起。子流程似乎永远不会得到 EOF
.
这里是代码的最小复制,为什么打印后挂起[=13=]?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
switch (pid_t pid = fork()) {
case 0: {
// in child
// replace the child's stdin with whatever filename is given as argv[1]
freopen(argv[1], "r+b", stdin);
// construct an argv array for to exec, no need for anything except
// argv[0] since we want it to use stdin
char path[] = "/bin/cat";
char *args[] = {path, NULL};
// run it!
execv(args[0], args);
abort(); // we should never get here!
}
case -1:
// error
return -1;
default: {
// in parent, just wait for the sub-process to terminate
int status;
const auto r = waitpid(pid, &status, __WALL);
if (r == -1) {
perror("waitpid");
return -1;
}
break;
}
}
}
# runs printf creating a pipe, which is then passed as the argv of my test program
./test >(printf "Hello\n")
./test <(printf "Hello\n")
切换到 >(...)
到 <(...)
以从 printf
读取而不是写入它。
freopen(argv[1], "rb", stdin);
不要使用 r+
。您只是从文件中读取,所以将其设置为 r
.
我有一个程序需要启动其他程序,可能用文件和管道替换它们的 stdio。虽然在 "work" 看来,子进程确实从源管道中获取了 I/O,但不幸的是,它也会导致挂起。子流程似乎永远不会得到 EOF
.
这里是代码的最小复制,为什么打印后挂起[=13=]?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
switch (pid_t pid = fork()) {
case 0: {
// in child
// replace the child's stdin with whatever filename is given as argv[1]
freopen(argv[1], "r+b", stdin);
// construct an argv array for to exec, no need for anything except
// argv[0] since we want it to use stdin
char path[] = "/bin/cat";
char *args[] = {path, NULL};
// run it!
execv(args[0], args);
abort(); // we should never get here!
}
case -1:
// error
return -1;
default: {
// in parent, just wait for the sub-process to terminate
int status;
const auto r = waitpid(pid, &status, __WALL);
if (r == -1) {
perror("waitpid");
return -1;
}
break;
}
}
}
# runs printf creating a pipe, which is then passed as the argv of my test program
./test >(printf "Hello\n")
./test <(printf "Hello\n")
切换到 >(...)
到 <(...)
以从 printf
读取而不是写入它。
freopen(argv[1], "rb", stdin);
不要使用 r+
。您只是从文件中读取,所以将其设置为 r
.