shell如何实现管道符号‘|‘

在 Shell 中使用的管道符 | 属于 无名管道(Unnamed Pipes)。它用于将一个命令的输出直接传递给另一个命令作为输入,实现了进程间的数据流转。

工作原理

当你在 Shell 中使用 | 时,操作系统会创建一个无名管道,并将左边命令的标准输出(stdout)重定向到这个管道的写入端,同时将右边命令的标准输入(stdin)重定向到这个管道的读取端。

实现机制

假设我们在 Shell 中运行以下命令:

bash 复制代码
ls | grep "file"

这一操作会执行如下步骤:

  1. 创建无名管道 :操作系统通过 pipe 系统调用创建一个无名管道。pipe 系统调用会返回两个文件描述符,一个用于写入数据,一个用于读取数据。

  2. 执行第一个命令 :Shell 使用 fork 系统调用创建一个子进程来运行 ls 命令。ls 命令的标准输出会被重定向到管道的写入端。

  3. 执行第二个命令 :Shell 再次使用 fork 创建另一个子进程来运行 grep "file" 命令。grep 命令的标准输入会被重定向到管道的读取端。

  4. 数据传递ls 命令的输出(例如文件列表)通过管道写入端进入管道,然后 grep 命令从管道的读取端读取数据并进行过滤。

  5. 管道关闭:当所有数据被写入和读取后,管道会被关闭,两个子进程分别结束。

例子

以下是上述原理的简单实现示例,使用 C 语言模拟 Shell 中的 ls | grep "file" 操作:

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    int pipefd[2];
    pid_t pid;

    // 创建无名管道
    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    // 创建第一个子进程执行 `ls`
    if ((pid = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) {
        // 子进程 1:执行 `ls`
        close(pipefd[0]);  // 关闭读取端
        dup2(pipefd[1], STDOUT_FILENO);  // 重定向 stdout 到管道写入端
        execlp("ls", "ls", NULL);  // 执行 `ls`
        perror("execlp ls");
        exit(EXIT_FAILURE);
    } else {
        // 创建第二个子进程执行 `grep`
        if ((pid = fork()) == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }

        if (pid == 0) {
            // 子进程 2:执行 `grep`
            close(pipefd[1]);  // 关闭写入端
            dup2(pipefd[0], STDIN_FILENO);  // 重定向 stdin 到管道读取端
            execlp("grep", "grep", "file", NULL);  // 执行 `grep`
            perror("execlp grep");
            exit(EXIT_FAILURE);
        } else {
            // 父进程:关闭管道两端,并等待子进程结束
            close(pipefd[0]);
            close(pipefd[1]);
            wait(NULL);  // 等待第一个子进程结束
            wait(NULL);  // 等待第二个子进程结束
        }
    }

    return 0;
}

这个示例中,pipe 系统调用用于创建无名管道,fork 创建了两个子进程,分别执行 lsgrep,并通过无名管道连接两个进程。Shell 使用类似的方式来实现 | 管道操作符。

疑问:exec系统调用会清空子进程内存,为什么管道的文件描述符依然可用?

答:
exec replaces the instruction and data segments by those inferred from the indicated file and starts the process running. The system data segment is unaltered. So the PID, current working directory and file descriptors are unaltered. Open files remain open except if fcntl(2) has been used to set the close-on-exec flag.

也就是说,exec会用指定文件替换内存的指令段和数据段,而系统数据段不会改变,因此,例如pid,当前运行目录和文件描述符不会变;或者说,子进程内存是父进程内存的完全拷贝,因此也有文件描述符,这些文件描述符也指向父进程打开的文件。打开的文件会保持打开状态,除非利用fcntl系统调用设置了close-on-exec

相关推荐
你好潘先生20 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
orion571 天前
Missing Semester Class1:course overview and introduction of shell
linux
用户120487221612 天前
Linux驱动编译与加载
linux·嵌入式
程序员老赵2 天前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
vivo互联网技术2 天前
从 10 分钟到 1 秒:ES 深度分页任意跳页的三轮优化实战
服务器·数据库·redis·elasticsearch·深度分页
用户805533698032 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux