C实现shell管理的一个例子

通常情况下,一个进程(比如cat /tmp/tt.txt)的输出与输入是标准输入(stdin或者0)、标准输出(stdout或者1)

shell 获取tt.txt文件中包含aa的行记录,涉及两个进程,一个读文件,一个过滤

# cat /tmp/tt.txt |grep aa
aa

该方式的实现用到管道符,管道让一个进程的输出成为了另外一个进程的输入,管道更改了进程的输入输出;

下面使用C模拟管道的功能,主要用到fork与pipe两个函数,

fork函数被调用一次后,会产生两个进程,子进程号为0,父进程号大于0,虽然父子进程分别位于if和else中,但父子进程都会被执行,即fork后面的代码被执行了两次;

pipe函数就相当于管道的作用,在代码中重定向父子进程的输出输入,让子进程的输出变成父进程的输入

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

int test(){
    int n;
    const int maxline = 90;
    int fd[2];
    pid_t pid;
    char line[maxline];

    if(pipe(fd) < 0){
        perror("pipe");
        exit(1);
    }

    if((pid = fork()) < 0){
        perror("fork");
        exit(1);
    }

    if(pid == 0){
        close(fd[0]);  /*关闭子进程管道读,即管理的输出*/
        close(1);      /*关闭标子进程标准输出,非必须*/
        dup2(fd[1],1); /*复制子进程管道输入,把子进程的输出从标准输出重定向到管道的写,即管理的输入*/
        close(fd[1]);  /*关闭原来的管道写*/
        sleep(1);
        system("cat /tmp/tt.txt");
        return 0;
    }else{
        close(fd[1]);  /*关闭父进程的管道写*/
        close(0);      /*关闭父进程的标准输入*/
        dup2(fd[0],0); /*复制父进程管道输出,把父进程的输入从标准输入重定向到管道的读,即管理的输出*/
        close(fd[0]);  /*关闭原父进程的管理输出*/
        system("grep aa");
    }
    return 0;
}

int main(){
    test();
    return 0;
}
[root@itoracle mysql]# gcc test.cc -o test
[root@itoracle mysql]# ./test 
aa
原文地址:https://www.cnblogs.com/perfei/p/9991046.html