我正在写一个简单的代码来实现unix/linux外壳的管道功能。简单的外壳与管道()函数
#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()问题的传递。
请张贴的确切消息。错误是什么告诉你什么是错的... – clcto 2014-11-06 20:17:29
谢谢你的回复。现在修复它。 – 2014-11-06 20:21:21
您是否阅读过您要拨打的功能的文档[execvp(3)](http://man7.org/linux/man-pages/man3/execvp.3.html)?您应该按照记录调用它... – 2014-11-06 20:23:20