• Linux--数据通信编程实现(FIFO)


    当open一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:
    ①若没有指定O_NONBLOCK(默认),只读open要阻塞到某个其他进程为写而打开此FIFO。类似的,只写open要阻塞到某个其他进程为读而打开它。
    ②若指定了O_NONBLOCK,则只读open立即返回,而只写open将出错返回-1如果没有进程已经为读而打开该FIFO,其errno置ENXIO。
    例:
    读的方式打开

    #include 
    #include 
    #include 
    #include 
    #include 
     
    //	int mkfifo(const char *pathname, mode_t mode);
     
    int main()
    {
    	if((mkfifo("./file",0600)==-1)&&errno!=EEXIST){
    		printf("mkfifo failuer \n");
    		perror("why?\n");
    	}
     	
     	int fd = open("./file",O_RDONLY);
     	printf("open success\n");
     	//用写的方式打开FIFO,读才不会阻塞
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    读的方式打开

    #include 
    #include 
    #include 
    #include 
    #include 
     
    //	int mkfifo(const char *pathname, mode_t mode);
     
    int main()
    {
     	int fd = open("./file",O_WRONLY);
     	printf("write open success\n");
     	//用写的方式打开FIFO,读才不会阻塞
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行read会发生阻塞,调用write之后程序继续进行。

    例:读取FIFO内容

    //读的方式打开
    
    #include 
    #include 
    #include 
    #include 
    #include 
     
    //	int mkfifo(const char *pathname, mode_t mode);
     
    int main()
    {
    	int buf[30] = {0};
    	
    	if((mkfifo("./file",0600) == -1) && errno != EEXIST){
    		printf("mkfifo failuer \n");
    		perror("why?\n");
    	}
     	
     	int fd = open("./file",O_RDONLY);
     	printf("open success\n");
     	//用写的方式打开FIFO,读才不会阻塞
    	
    	int nread = read(fd,buf,20);
    	printf("read %d byte from fifo , context : %s\n",nread,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
    //写的方式打开
    
    #include 
    #include 
    #include 
    #include 
    #include 
     
    //	int mkfifo(const char *pathname, mode_t mode);
     
    int main()
    {
    	char *str = "message from FIFO !";
    	
     	int fd = open("./file",O_WRONLY);
     	printf("write open success\n");
     	//用写的方式打开FIFO,读才不会阻塞
    	
    	write(fd,str,strlen(str));
    	
    	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
  • 相关阅读:
    【MATLAB源码-第52期】基于matlab的4用户DS-CDMA误码率仿真,对比不同信道以及不同扩频码。
    【SpringMVC】JSR 303与拦截器注解使用
    ROS 消息订阅 状态同步 action 行为通讯方式
    微服务是个坏主意吗?
    人工智能、深度学习、机器学习常见面试题301~320
    【Java】初识IO流【附导航】
    从零搭建react+ts组件库(封装antd)
    C语言编程陷阱(五)
    UTONMOS:数字藏品市场将迎来科技大变局
    vue针对低版本浏览器不兼容es6特性解决方案,
  • 原文地址:https://blog.csdn.net/weixin_48208102/article/details/132803123