【发布时间】:2014-11-06 20:15:21
【问题描述】:
我正在编写一个简单的代码来实现 unix/linux shell 的管道功能。
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void
cisshPipe(char* command1[], char* command2[])
{
int fd[2];//create array for two file descritors
pid_t childPid;//To set for child process
pipe(fd);//To create pipeline here
if((childPid=fork())==-1)
{
perror("fork here");
exit(1);
}
//The below is the real meat for this subroutine
if(childPid==0)//If child process
{
close(fd[0]);//To close the input of child
dup(fd[0]);//To duplicate the input, for the later process
}
else//For the real output
{
close(fd[1]);//To close the parent output first
execvp(command2[],command2);
}
}
但是,对于此处的预期表达式,我在“execvp(command2[],command2)”上遇到了一些编译错误。我认为这是由于我用来将子输出传递给父输入的 dup() 函数。请问有什么解决办法吗?
一些更新:
感谢约翰的回答。我修复了编译问题。但是当我输入“ls | sort”时它正在执行管道功能,我认为这里仍然是dup()的传递问题。
【问题讨论】:
-
请发布确切的消息。错误是告诉你什么是错误的......
-
感谢您的回复。立即修复。
-
您是否阅读过您正在调用的函数的文档,例如execvp(3)?您应该按照记录调用它...