管道实施

nun*_*nos 9 c linux shell pipe piping

我正在尝试实现支持管道的linux shell.我已经完成了简单的命令,在后台运行的命令,重定向,但仍然缺少管道.

我已经阅读了它并看到了一些代码片段,但仍然无法找到一个可行的解决方案.

到目前为止我所拥有的:

int fd[2];

pipe(fd);

pid_t pid = fork();

if (pid == -1)  
   return -1;  

if (pid == 0)  
{  
   close(fd[1]); //close write to pipe, in child
   execlp("cat", "cat", "names.txt", NULL);
}

else
{
   close(fd[0]); //close read from pipe, in parent
   execlp("sort", "sort", NULL);
}  
Run Code Online (Sandbox Code Playgroud)

我是一个新手程序员,你可能会说,当我编写一些我不太了解的东西时,显然就是这种情况,我喜欢从一些非常简单和具体的东西开始,然后从那里开始构建.

因此,在能够在管道中实现三个或更多不同的命令之前,我希望能够计算"ls names.txt | sort"或类似的东西,其中names.txt是一个名称非常无序的文件.

更新了代码,但仍然无效.

谢谢.

caf*_*caf 11

你需要将一个孩子的stdout替换为管道的写入端,将另一个孩子的stdin替换为读取端:

if (pid == 0)  
{  
   close(fd[0]); //close read from pipe, in parent
   dup2(fd[1], STDOUT_FILENO); // Replace stdout with the write end of the pipe
   close(fd[1]); // Don't need another copy of the pipe write end hanging about
   execlp("cat", "cat", "names.txt", NULL);
}
else
{
   close(fd[1]); //close write to pipe, in child
   dup2(fd[0], STDIN_FILENO); // Replace stdin with the read end of the pipe
   close(fd[0]); // Don't need another copy of the pipe read end hanging about
   execlp("sort", "sort", NULL);
} 
Run Code Online (Sandbox Code Playgroud)


unw*_*ind 5

查看pipe()标准库调用。这用于创建管道。当然,您必须完成部分工作fork(),以便子进程正确继承文件描述符。

另请注意参数的顺序dup2()

int dup2(int oldfd, int newfd);
Run Code Online (Sandbox Code Playgroud)

dup2() 使 newfd 成为 oldfd 的副本,必要时先关闭 newfd