通标准输出孩子的父母标准输入 [英] Pass stdout of child to parents stdin
本文介绍了通标准输出孩子的父母标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在一个子进程传递一个程序的标准输出在父母的过程中,标准输入。
I try to pass the stdout of an program in a child process to the stdin in the parents process.
在bash中,这将是这样的:
In bash this would look like this:
wget的住址」|少
wget "adress"|less
我的code是这样的:
My code looks like this:
int fd[2];
pid_t child_id;
int status;
char *args[] = {"wget","-O -",argv[1], NULL};
int pipe(int fd[2]);
child_id = fork();
if (child_id == -1)
{
printf ("Fork error\n");
}
if (child_id == 0)
{
close(fd[0]);
int c = dup2(fd[1],1);
execl ("/usr/bin/wget", "wget", "-qO-",argv[1], NULL);
}
else{
waitpid(child_id,&status,0);
close(fd[1]);
int c2 = dup2(fd[0],STDIN_FILENO);
printf("%i\n",c2 ); //debugging
execl ("/usr/bin/less", "less", NULL);
}
请注意,的argv [1]应该是一个webadress。
但在运行程序dup2的调试输出( INT C2 = dup2(FD [0],STDIN_FILENO);
)时,在父母返回-1 - 因此它失败。
Please note that argv[1] should be an webadress.
But when running the program the debug output of the dup2 (int c2 = dup2(fd[0],STDIN_FILENO);
) in the parent returns -1 - so it fails.
我找不到PROGRAMM。
I can't find the programm.
推荐答案
你不叫管(2)
在你的程序。我想,你的
you do not call pipe(2)
in your program. I guess, your
int pipe(int fd[2]);
应
pipe(fd);
这篇关于通标准输出孩子的父母标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文