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

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

实现代码;

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

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

相关推荐
伤不起bb3 分钟前
Kafka 消息队列
linux·运维·分布式·kafka
Hello.Reader13 分钟前
Git 安装全攻略Linux、macOS、Windows 与源码编译
linux·git·macos
龙仔72516 分钟前
华为云CentOS配置在线yum源,连接公网后,逐步复制粘贴,看好自己对应的版本即可,【新手必看】
linux·centos·华为云
tiging17 分钟前
centos实现SSH远程登录
linux·centos·ssh
suuijbd26 分钟前
个人总结八股文之-基础篇(持续更新)
算法
2401_8812444028 分钟前
斐波那契数列------矩阵幂法
线性代数·算法·矩阵
Wooden-Flute36 分钟前
五、查询处理和查询优化
服务器·数据库·oracle
机器学习与统计学1 小时前
阿里牛逼,又开源两个遥遥领先的模型(向量化、重排),知识库要翻天地覆了
算法
明月清了个风1 小时前
数据结构与算法学习笔记(Acwing 提高课)----动态规划·树形DP
笔记·学习·动态规划·树形dp
小河豚oO1 小时前
LeetCode刷题---贪心算法---944
算法·leetcode·贪心算法