我正在尝试使用 execvp 运行 ls|wc。所以我创建了一个管道,然后创建了一个子管道。我关闭父/子中适当的(读/写)端,然后将另一端映射到标准输出/标准输入。然后我在父级中使用 execvp 运行 ls ,在子级中使用 wc 。当我运行该程序时,它显示
wc:standard input:bad file descriptor.
0 0 0
wc: -:Bad file descriptor
这是我的代码:
int main()
{
//int nbBytes = 0; //stream length
int pfd_1[2]; //file descriptor
//char buffer[MAX_FILE_LENGTH];
char* arg[MAX_FILE_LENGTH];
pid_t processPid;
//Create a pipe
if(pipe(pfd_1) == -1)
{
printf("Error in creating pipe");
return 0;
}
//Create a child
processPid = fork();
if(processPid == -1)
{
printf("Erro in fork");
exit(1);
}
else if(processPid == 0) //Child
{
//redirect read end file descriptor to standard input
dup2(pfd_1[0],0);
//Close the write end
if(close(pfd_1[1] == -1))
{
printf("Error in closing the write end file descriptor");
exit(1);
}
arg[0] = "wc";
//arg[1] = "-l";
arg[1] = '\0';
if(execvp(arg[0],arg) == -1)
{
printf("Error in executing ls");
}
}
else //Parent
{
//redirect standard output to the file descriptor
dup2(pfd_1[1],1);
//Close the read end
if(close(pfd_1[0] == -1))
{
printf("Error in closing the read end from parent");
exit(1);
}
//Command
arg[0] = "ls";
arg[1] = "/proc/1/status";
arg[2] = '\0';
if(execvp(arg[0],arg) == -1)
{
printf("Error in executing ls");
}
}
}
知道可能出了什么问题吗?为什么它会将标准输入视为错误的文件描述符?我的理解是,由于 stdin 和读取结束文件描述符是别名,因此 wc -l 会读取来自父进程的任何输出。我需要执行 scanf 才能从标准输入中读取数据吗?
最佳答案
问题出在这一行:
if(close(pfd_1[1] == -1))
您正在关闭pfd_1[1] == -1
的结果,它必然等于0
(因为它们永远不会相等)。正确的行可能是:
if (close(pfd_1[1]) == -1)
请注意,您稍后会在尝试关闭父进程中的读取端时再次执行此操作。
关于c - 如何使用管道运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300880/