• 多进程间通信学习之有名管道


    • 有名管道:
    • 区别于无名管道,其可以用于任意进程间的通信
    • 同无名管道一样,也是半双工的通信方式
    • 有名管道的大小也是64KB
    • 也是不能使用lseek函数
    • 其本质上,是在内存上,在文件系统上只是一个标识
    • 有名管道会创建一个管道文件,只需要打开这个文件,进行相应的读写操作即可;
    • 读写特点:
    • 若读端存在写管道,那么有多少数据,就写多少数据,直到有名管道写满为止,此时会出现写阻塞
    • 若读端不存在写管道,会出现两种情况
    • 第一种:读端没有打开,写端open函数的位置阻塞;
    • 第二种:读端打开后关闭,会出现管道破裂的现象;
    • 若写端存在读管道,那么有多少数据,就读多少数据,没有数据的时候,会出现阻塞等待
    • 若写端不存在读管道,也会出现两种情况
    • 第一种:写端没有打开,读端open函数的位置阻塞;
    • 第二种:写端打开后关闭,有多少数据,就读多少,没有数据的时候,就会立即返回,即非阻塞的状态
    • 创建有名管道(mkfifo函数):
    	#include 
    	#include 
    	
    	int mkfifo(const char *pathname, mode_t mode);
    	/*
    	功能:
    	
    			创建管道文件
    	
    	参数:
    	
    	    	pathname:管道路径和名字
    	
    	    	mode:管道文件的权限
    	
    	返回值:
    	
    	    	成功 0
    	
    	    	失败 -1 重置错误码
    	*/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 示例代码:
    • 写端:
    	#include 
    	#include 
    	#include 
    	
    	#include 
    	#include 
    	#include 
    	#include 
    	
    	#include 
    	#include 
    	
    	int main(int argc, char const *argv[])
    	{
    	    int fd = open("./fifo_k",O_WRONLY);
    	    if(-1 == fd)
    	    {
    	        perror("open error");
    	        exit(-1);
    	    }
    	    char buf[128] = {0};
    	    while(true)
    	    {
    	        memset(buf,0,sizeof(buf));
    	        fgets(buf,sizeof(buf),stdin);
    	        buf[strlen(buf) - 1] = '\0';
    	        write(fd,buf,sizeof(buf));
    	        if(!strncmp(buf,"quit",4))
    	        {
    	            exit(-1);
    	
    	        }
    	    }
    	    close(fd);
    	    
    	    return 0;
    	}
    
    
    
    • 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
    • 读端:
    	#include 
    	#include 
    	#include 
    	
    	#include 
    	#include 
    	#include 
    	#include 
    	
    	#include 
    	#include 
    	
    	int main(int argc, char const *argv[])
    	{
    	    int fd = open("./fifo_k",O_RDONLY);
    	    if(-1 == fd)
    	    {
    	        perror("open error");
    	        exit(-1);
    	    }
    	    char buf[128] = {0};
    	    while(true)
    	    {
    	        memset(buf,0,sizeof(buf));
    	        read(fd,buf,sizeof(buf));
    	        if(!strncmp(buf,"quit",4))
    	        {
    	            exit(-1);
    	
    	        }
    	        printf("写端发来的数据[%s]\n",buf);
    	    }
    	    close(fd);
    	    
    	    return 0;
    	}
    	
    
    
    • 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
    • 运行结果:
    • 写端:
    	hello
    	china
    	quit
    
    • 1
    • 2
    • 3
    • 读端:
    	写端发来的数据[hello]
    	写端发来的数据[china]
    
    • 1
    • 2
  • 相关阅读:
    超级浏览器对跨境亚马逊防关联有用吗?
    uni-app--》基于小程序开发的电商平台项目实战(五)
    (最详细)关于List和Set的区别与应用
    【数据结构】二叉树的实现
    自适应 t 分布与动态边界策略改进的算术优化算法-附代码
    Android 巧用ImageView属性实现选中和未选中效果
    提分必练!中创教育PMP全真模拟题分享来喽
    作为大厂面试官,原来这种学生最吃香!
    Cemotion情感分析
    安防监控视频平台EasyCVR视频汇聚平台定制项目增加AI智能算法详细介绍
  • 原文地址:https://blog.csdn.net/qq_41878292/article/details/134096040