对于一项作业,我应该实现 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");
}
我的第一个 child 工作得很好,将 ls 的输出写入之前声明的管道,但是,我的第二个 child 运行 grep,但显然无法从管道获取输入。为什么会这样? 当我运行它时,我的输出是 /根/操作系统 $ ls |搜索
就这样卡住了
最佳答案
不要忽略编译器警告:
$ 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
:
The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
始终使用 -Werror
进行编译,以便在出现警告时编译失败,最好还使用 -Wall
来获取所有潜在问题的警告。
这是您的程序,其中添加了 main 方法和哨兵:
#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
关于c++ - 无法使用 execlp 从管道读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35586947/