8.28练习

使用两个有名管道实现双向通信

使用两个有名管道(FIFO)实现双向通信。父进程负责写入信息到管道,子进程负责从管道读取信息并显示到终端上。使用信号处理来确保进程在接收到 “quit” 命令时正确退出。

进程A

#include <myhead.h>

// 信号处理函数,收到信号时退出
void handle_signal(int sig) {
    exit(EXIT_SUCCESS);
}

int main(int argc, const char *argv[]) {
    signal(SIGTERM, handle_signal); // 注册信号处理函数

    pid_t pid;
    pid = fork();
    if (pid > 0) { // 父进程, 发送信息
        char buff[1024];
        int send = open("./path1", O_WRONLY);
        if (send == -1) {
            perror("open send");
            return -1;
        }
        while (1) {
            memset(buff, 0, sizeof(buff));
            int len = read(0, buff, sizeof(buff));
            write(send, buff, len); // 写入管道
            if (strcmp(buff, "quit\n") == 0) {
                kill(getpid(), SIGTERM); // 通知子进程退出
                break;
            }
        }
        close(send);
        exit(EXIT_SUCCESS);

    } else if (pid == 0) { // 子进程,读取信息
        char buff[1024];
        int receive = open("./path2", O_RDONLY);
        if (receive == -1) {
            perror("open receive");
            return -1;
        }

        while (1) {
            memset(buff, 0, sizeof(buff));
            int len = read(receive, buff, sizeof(buff)); // 读取管道数据
            write(1, buff, len);
            if (strcmp(buff, "quit\n") == 0) {
                kill(getppid(), SIGTERM); // 通知父进程退出
                break;
            }
        }
        close(receive);
        exit(EXIT_SUCCESS);
    } else {
        perror("fork");
        return -1;
    }

    return 0;
}

进程B

#include <myhead.h>

// 信号处理函数,收到信号时退出
void handle_signal(int sig) {
    exit(EXIT_SUCCESS);
}

int main(int argc, const char *argv[]) {
    signal(SIGTERM, handle_signal); // 注册信号处理函数

    pid_t pid;
    pid = fork();
    if (pid > 0) { // 父进程, 发送信息
        char buff[1024];
        int send = open("./path2", O_WRONLY);
        if (send == -1) {
            perror("open send");
            return -1;
        }
        while (1) {
            memset(buff, 0, sizeof(buff));
            int len = read(0, buff, sizeof(buff));
            write(send, buff, len); // 写入管道
            if (strcmp(buff, "quit\n") == 0) {
                kill(getpid(), SIGTERM); // 通知子进程退出
                break;
            }
        }
        close(send);
        exit(EXIT_SUCCESS);

    } else if (pid == 0) { // 子进程,读取信息
        char buff[1024];
        int receive = open("./path1", O_RDONLY);
        if (receive == -1) {
            perror("open receive");
            return -1;
        }

        while (1) {
            memset(buff, 0, sizeof(buff));
            int len = read(receive, buff, sizeof(buff)); // 读取管道数据
            write(1, buff, len);
            if (strcmp(buff, "quit\n") == 0) {
                kill(getppid(), SIGTERM); // 通知父进程退出
                break;
            }
        }
        close(receive);
        exit(EXIT_SUCCESS);
    } else {
        perror("fork");
        return -1;
    }

    return 0;
}

代码解释

进程A

  1. 信号处理函数

    void handle_signal(int sig) {
        exit(EXIT_SUCCESS);
    }
    
    • 当接收到 SIGTERM 信号时,进程会调用 handle_signal 函数并退出。
  2. 注册信号处理函数

    signal(SIGTERM, handle_signal);
    
    • 注册 SIGTERM 信号的处理函数。
  3. 创建子进程

    pid_t pid;
    pid = fork();
    
  4. 父进程(发送信息)

    if (pid > 0) {
        // 打开管道
        int send = open("./path1", O_WRONLY);
        // 读取标准输入并写入管道
        while (1) {
            int len = read(0, buff, sizeof(buff));
            write(send, buff, len);
            if (strcmp(buff, "quit\n") == 0) {
                kill(getpid(), SIGTERM); // 通知子进程退出
                break;
            }
        }
        close(send);
        exit(EXIT_SUCCESS);
    }
    
  5. 子进程(读取信息)

    else if (pid == 0) {
        // 打开管道
        int receive = open("./path2", O_RDONLY);
        // 从管道读取数据并显示到终端
        while (1) {
            int len = read(receive, buff, sizeof(buff));
            write(1, buff, len);
            if (strcmp(buff, "quit\n") == 0) {
                kill(getppid(), SIGTERM); // 通知父进程退出
                break;
            }
        }
        close(receive);
        exit(EXIT_SUCCESS);
    }
    

进程B

  1. 信号处理函数

    void handle_signal(int sig) {
        exit(EXIT_SUCCESS);
    }
    
    • 当接收到 SIGTERM 信号时,进程会调用 handle_signal 函数并退出。
  2. 注册信号处理函数

    signal(SIGTERM, handle_signal);
    
    • 注册 SIGTERM 信号的处理函数。
  3. 创建子进程

    pid_t pid;
    pid = fork();
    
  4. 父进程(发送信息)

    if (pid > 0) {
        // 打开管道
        int send = open("./path2", O_WRONLY);
        // 读取标准输入并写入管道
        while (1) {
            int len = read(0, buff, sizeof(buff));
            write(send, buff, len);
            if (strcmp(buff, "quit\n") == 0) {
                kill(getpid(), SIGTERM); // 通知子进程退出
                break;
            }
        }
        close(send);
        exit(EXIT_SUCCESS);
    }
    
  5. 子进程(读取信息)

    else if (pid == 0) {
        // 打开管道
        int receive = open("./path1", O_RDONLY);
        // 从管道读取数据并显示到终端
        while (1) {
            int len = read(receive, buff, sizeof(buff));
            write(1, buff, len);
            if (strcmp(buff, "quit\n") == 0) {
                kill(getppid(), SIGTERM); // 通知父进程退出
                break;
            }
        }
        close(receive);
        exit(EXIT_SUCCESS);
    }
    

运行步骤

  1. 创建命名管道:

    #include<myhead.h>
    int main (int argc, const char *argv[])
    {
    	if(mkfifo("./path1",0644)==-1){
    		perror("path1");
    		return -1;
    	}
    	if(mkfifo("./path2",0644)==-1){
    		perror("path2");
    		return -1;
    	}
    
    	return 0;
    }
    
  2. 编译两个程序:

    gcc usr2.c -o u1
    gcc usr2.c -o u2
    
  3. 分别在两个终端窗口中运行两个程序:

    ./u1
    

    ./u2
    

总结

这篇博客展示了如何使用两个有名管道实现双向通信。父进程负责写入信息到管道,子进程负责从管道读取信息并显示到终端上。还使用了信号处理来确保进程在接收到 “quit” 命令时正确退出。

  • 18
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值