随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

C/C++使用FIFO实现非父子进程之间的通讯

一、概述

  案例:编写两个进程,要求实现两个进程之间的通讯,A进程循环写数据,B进程循读取管道中的数据。

  fifo简单介绍:使用fifo这个系统提供的Api函数可实现两个进程之间的相互通讯。其通讯原理是通过mkfifo函数创建一个fifo文件,进程A和进程B通过操作者fifo文件来完成双方的通信。由于进程之间通讯必须要经过操作系统内核,所以进程A和进程B操作fifo文件就相当于间接操作了内核。其中进程A操作fifo文件写入数据,进程B通过操作fifo文件将管道中的数据读取出来。

  操作步骤

    进程A:

    1.先检测myfifo文件是否存在,如果不存在就创建一个

    2.打开myfifo文件

    3.写入myfifo文件

    4.关闭myfifo文件

    进程B:

    1.检测myfifo文件是否存在,如果存在就读取,不存在就创建

    2.打开myfifo文件

    3.读取myfifo文件

    4.关闭myfifo文件

    

  

二、代码实例

  1.写端:

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//fifo完成两个非父子进程之间的通讯
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
 
int main()
{
        //1.创建fifo文件
        //检测fifo文件是否存在
        int ret = access("./myfifo",F_OK);
        if(ret!=0){
                ret = mkfifo("./myfifo",0777);
                if(ret<0){
                        perror("mkfifo error");
                        return -1;
                }
        }
        //打开文件
        int fd = open("./myfifo",O_RDWR);
        if(fd<0){
                perror("open error");
                return -1;
        }
        //写fifo文件
        int i=0;
        char buf[64];
        while(1){
                memset(buf,0x00,sizeof(buf));
                sprintf(buf,"%d:%s",i,"hello world");
                write(fd,buf,strlen(buf));
                sleep(1);
                i++;
        }
        //关闭文件
        close(fd);
        return 0;
}

  

  2.读端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
 
int main(){
        //判断myfifo文件是否存在,如果不存在就创建
        int ret = access("./myfifo",F_OK);
        if(ret!=0){
                ret = mkfifo("./myfifo",0777);
                if(ret<0){
                        perror("mkfifo error");
                        return -1;
                }
 
        }
        //打开文件
        int fd = open("./myfifo",O_RDWR);
        if(fd<0){
                perror("open error");
                return -1;
        }
        //读fifo文件
        int n =0;
        char buf[64];
        while(1){
                memset(buf,0x00,sizeof(buf));
                n = read(fd,buf,sizeof(buf));
                printf("n==[%d],buf==[%s]\n",n,buf);
        }
        //关闭文件
        close(fd);
 
        return 0;
}

  

三、测试图

  

 

posted on   飘杨......  阅读(891)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示