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

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

实现代码;

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;
}

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

相关推荐
酷酷的崽7981 小时前
【数据结构】——原来排序算法搞懂这些就行,轻松拿捏
数据结构·算法·排序算法
zealous_zzx1 小时前
深度解析Linux系统和Unix系统的基本概念及优缺点和原理
linux·unix
丢爸1 小时前
网络学习-eNSP配置NAT
linux·网络·学习
沐风ya1 小时前
NAT技术介绍+缺陷(内网穿透+工具),NAPT(介绍,替换过程,原理,NAT转换表)
linux·服务器·网络
八月的雨季 最後的冰吻2 小时前
C--字符串函数处理总结
c语言·前端·算法
YHPsophie2 小时前
AT3340-6T杭州中科微BDS定位授时板卡性能指标
经验分享·笔记·学习·车载系统·信息与通信
天启代理ip2 小时前
HTTP隧道代理:互联网冲浪的隐形翅膀
服务器·网络·爬虫·网络协议·tcp/ip
Pandaconda3 小时前
【C++ 面试 - 新特性】每日 3 题(六)
开发语言·c++·经验分享·笔记·后端·面试·职场和发展
别挡3 小时前
CentOS Stream 8中安装和使用 Docker
linux·docker·centos
手打猪大屁3 小时前
STM32——串口通信(发送/接收数据与中断函数应用)
经验分享·笔记·stm32·单片机·嵌入式硬件