进程间的通信:管道文件的使用

使用有名管道实现两个进程间相互通信,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份

实现代码;

1.创建管道文件

cpp 复制代码
#include <myhead.h>
int main(int argc, char const *argv[])
{
    int pipefd[2];
    //创建管道文件
    if(mkfifo("./pipe",0664 )==-1)
    {
        perror ("mkfifo error");
        return -1;
    }
     if(mkfifo("./pipe1",0664 )==-1)
    {
        perror ("mkfifo error");
        return -1;
    }
    printf("管道创建成功\n");
    return 0;
}

2.发送端

cpp 复制代码
#include <myhead.h>
int main(int argc, char const *argv[])
{
    //打开管道1(写)
    int wfd1 = open("./pipe", O_WRONLY);
    if (wfd1 == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件1写端打开\n");

    //打开管道2(读)
    int rfd2 = open("./pipe1", O_RDONLY);
    if (rfd2 == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件2读端打开\n");

    pid_t pid = fork();
    //父进程
    if (pid > 0)
    {
        //发送信息
        char wbuf[128] = "";
        while (1)
        {
            printf("请输入:\n");
            fgets(wbuf, sizeof(wbuf), stdin);
            wbuf[strlen(wbuf) - 1] = 0;
            //发送数据
            write(wfd1, wbuf, strlen(wbuf));
            if (strcmp(wbuf, "quit") == 0)
                break;
        }
        close(wfd1);
    }
    //子进程
    else if (pid == 0)
    {
        //定义接受容器
        char rbuf[128] = "";
        while (1)
        {
            //usleep(100);
            bzero(rbuf, sizeof(rbuf));
            //读取数据
            read(rfd2, rbuf, sizeof(rbuf));
            if (strcmp(rbuf, "quit") == 0)
            {
                printf("子进程pid是%d\n", getpid());
                break;
            }
            printf("收到的消息为:%s\n", rbuf);
            printf("请输入:\n");
        }
        close(rfd2);
    }
    // 父进程等待子进程结束
    if (pid > 0)
    {
        wait(NULL);
    }
    return 0;
}

接收端:

cpp 复制代码
#include <myhead.h>
int main(int argc, char const *argv[])
{
    //打开管道1(读)
    int rfd1 = open("./pipe", O_RDONLY);
    if (rfd1 == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件1写端打开\n");

    //打开管道2(写)
    int wfd2 = open("./pipe1", O_WRONLY);
    if (wfd2 == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件2读端打开\n");

    //打开文件
    int fd = -1;
    if((fd = open("./file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
    {
        perror("open error");
        return -1;
    }

    pid_t pid = fork();
    //父进程
    if (pid > 0)
    {
        //发送信息
        char wbuf[128] = "";
        while (1)
        {
            printf("请输入:\n");
            fgets(wbuf, sizeof(wbuf), stdin);
            wbuf[strlen(wbuf) - 1] = 0;
            //发送数据
            write(wfd2, wbuf, strlen(wbuf));
            //写入文件
            write(fd ,wbuf ,strlen (wbuf));
            if (strcmp(wbuf, "quit") == 0)
                break;
        }
        close(wfd2);
    }
    //子进程
    else if (pid == 0)
    {
        //定义接受容器
        char rbuf[128] = "";
        while (1)
        {
            //usleep(100);
            bzero(rbuf, sizeof(rbuf));
            //读取数据
            read(rfd1, rbuf, sizeof(rbuf));
            //写入文件
            write (fd,rbuf ,strlen(rbuf));
            if (strcmp(rbuf, "quit") == 0)
            {
                printf("子进程pid是%d\n", getpid());
                break;
            }
            printf("收到的消息为:%s\n", rbuf);
            printf("请输入:\n");
        }
        close(rfd1);
    }
    close(fd);
    // 父进程等待子进程结束
    if (pid > 0)
    {
        wait(NULL);
    }
    return 0;
}

*要求:先运行创建管道函数后运行收发函数

相关推荐
寻丶幽风13 分钟前
论文阅读笔记——VGGT: Visual Geometry Grounded Transformer
论文阅读·笔记·transformer·三维重建·3dgs·vggt
爱思德学术36 分钟前
中国计算机学会(CCF)推荐学术会议-B(交叉/综合/新兴):BIBM 2025
算法
冰糖猕猴桃1 小时前
【Python】进阶 - 数据结构与算法
开发语言·数据结构·python·算法·时间复杂度、空间复杂度·树、二叉树·堆、图
天水幼麟1 小时前
python学习笔记(深度学习)
笔记·python·学习
lifallen1 小时前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
m0_694845571 小时前
服务器如何配置防火墙规则开放/关闭端口?
linux·服务器·安全·云计算
you45802 小时前
小程序学习笔记:使用 MobX 实现全局数据共享,实例创建、计算属性与 Actions 方法
笔记·学习·小程序
笑衬人心。2 小时前
初学Spring AI 笔记
人工智能·笔记·spring
liujing102329292 小时前
Day04_刷题niuke20250703
java·开发语言·算法
阿巴~阿巴~2 小时前
Linux基本命令篇 —— alias命令
linux·服务器·bash