匿名管道通信案例

实现ps aux | grep xxx父子进程间通信

    子进程:ps aux,子进程结束后,将数据发送给父进程

    父进程:获取到数据,过滤

    pipe()

    execlp()

    将标准输出 stdout_fileno 重定向到管道的写端,dup2()

#include <unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<wait.h>
int main() {
    int fd[2];
    int ret = pipe(fd);
    if(ret == -1) {
        perror("pipe");
        exit(0);
    }
    pid_t pid = fork();
    if(pid > 0) {
        //父进程
        //关闭写端
        close(fd[1]);
        //从管道中读取
        char buf[1024] = {0};
        int len = -1;
        while((len = read(fd[0], buf, sizeof(buf) - 1)) > 0) {
            //过滤数据输出
            printf("%s", buf);
            memset(buf, 0, 1024);
        }
        wait(NULL);
    } else if(pid == 0) {
        //子进程,写管道
        //关闭读端
        close(fd[0]);
        //文件描述符的重定向 stdout_fileno -> fd[1]
        dup2(fd[1], STDOUT_FILENO);
        //执行ps aux
        execlp("ps", "ps", "aux", NULL);
        perror("execlp");
        exit(0);
    } else {
        perror("fork");
        exit(0);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ME_Liao_2022/article/details/133176269