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

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

实现代码;

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

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

相关推荐
非 白1 小时前
数据结构——树
数据结构·笔记·考研
我不是程序猿儿3 小时前
【C】识别一份嵌入式工程文件
c语言·开发语言
Christal_pyy3 小时前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
Dizzy.5173 小时前
数据结构(查找)
数据结构·学习·算法
csbDD4 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
E___V___E4 小时前
MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 高级篇 part 2
数据库·笔记·mysql
小金的学习笔记5 小时前
如何在本地和服务器新建mysql用户和密码
运维·服务器·mysql
EasyNVR5 小时前
EasyRTC智能硬件:实时畅联、沉浸互动、消音护航
运维·服务器·网络·安全·音视频·webrtc·p2p
分别努力读书6 小时前
acm培训 part 7
算法·图论
武乐乐~6 小时前
欢乐力扣:赎金信
算法·leetcode·职场和发展