用execlp无法读取管道

时间:2016-02-23 19:55:27

标签: c++ linux bash pipe exec

对于作业,我应该实现linux终端。我的终端应该支持管道参数。像用户可以输入: ls | grep ".cpp" 到目前为止我所做的是:

pid = fork();
if(pid==0)
{
  close(fd[0]);
  dup2(fd[1],1);
  close(fd[1]);
  execlp("/bin/ls","ls");
}
wait(NULL);
pid=fork();
if(pid==0)
{
  close(fd[1]);
  dup2(fd[0],0);
  close(fd[0]);
  execlp("/bin/grep","grep",".cpp");
}

我的第一个孩子工作得很好,将ls的输出写入之前声明的管道,但是,我的第二个孩子运行grep,但显然无法从管道输入。为什么会这样? 当我运行它时,我的输出是     / root / OS $ ls | grep的

它就像这样被卡住了

1 个答案:

答案 0 :(得分:2)

不要忽略编译器警告:

$ gcc lol.c
lol.c: In function ‘main’:
lol.c:14:5: warning: not enough variable arguments to fit a sentinel [-Wformat=]
     execlp("/bin/ls","ls");
     ^
lol.c:23:5: warning: missing sentinel in function call [-Wformat=]
     execlp("/bin/grep","grep",".cpp");
     ^

此处man execlp

  

按照惯例,第一个参数应指向与正在执行的文件关联的文件名。参数列表必须由NULL终止          指针,并且,由于这些是可变参数函数,因此必须强制转换此指针(char *)NULL。

始终使用-Werror进行编译,以便在出现警告时编译失败,最好还有-Wall以获取所有潜在问题的警告。

这是你的程序,主要方法和标记添加:

#include <unistd.h>
#include <sys/wait.h>


int main() {
  int fd[2];
  int pid;

  pipe(fd);
  pid = fork();
  if(pid==0)
  { 
    close(fd[0]);
    dup2(fd[1],1);
    close(fd[1]);
    execlp("/bin/ls","ls", NULL);
  }
  wait(NULL);
  pid=fork();
  if(pid==0)
  { 
    close(fd[1]);
    dup2(fd[0],0);
    close(fd[0]);
    execlp("/bin/grep","grep",".cpp", NULL);
  }
  return 0;
}

以下是它如何运行:

$ gcc -Wall -Werror lol.c -o lol
$ ./lol
foo.cpp