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

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

实现代码;

1.创建管道文件

#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.发送端

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

接收端:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值