• 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
  • 相关阅读:
    Mac 手动安装 sshpass
    3.3 DLL注入:突破会话0强力注入
    论文投稿指南——收藏|SCI写作投稿发表全流程
    【分享】许多 有意思的 颜文字表情包
    我的十年编程路 2017年篇
    Nacos使用实践
    JDK21更新内容:临时变量
    vue的axios方法
    Elastic Stack容器化部署拓展(Https、AD域集成)并收集Cisco设备的日志信息
    编译linux的设备树
  • 原文地址:https://blog.csdn.net/weixin_48208102/article/details/132803123