IO学习系列之非阻塞IO

  • 非阻塞IO:
  • 若资源没有准备就绪,立即返回错误信息
  • 若资源准备就绪,会获取相关资源
  • 特点:
  • 在所有的IO模型中,进程不会阻塞轮询访问,CPU消耗较大;
  • 设置非阻塞(fcntl函数):
  • fcntl函数功能:控制文件描述符状态;
  • fcntl函数:
	#include <unistd.h>
	#include <fcntl.h>
	int fcntl(int fd, int cmd, ... /* arg */ );
	/*
	参数:
		fd	文件描述符
	
		cmd	要控制的方式
	
			F_GETFL	获取文件描述符的状态 arg被忽略
	
			F_SETFL 设置文件描述符的状态 arg为int类型
	
				文件描述符的状态 O_NONBLOCK 表示非阻塞
	
		...	可变参 有没有 以及什么类型 都取决于 cmd
	
	返回值:
	
		cmd是 F_GETFL 	成功 	返回文件状态标志位 失败返回-1 重置错误码 
	
		cmd是 F_SETFL 	成功 	返回 0 失败返回-1 重置错误码
	*/
	int flag = fcntl(fd, F_GETFL);	//先获取原来的状态信息

	flag |= O_NONBLOCK; 			//添加非阻塞的标志位

	fcntl(fd, F_SETFL, flag);		//再设置回去
  • 三个写端:
	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	int main(int argc, char const *argv[])
	{
	    int fd = open("myfifo1",O_WRONLY);
	    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));
	    }
	    return 0;
	}

	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	int main(int argc, char const *argv[])
	{
	    int fd = open("myfifo2",O_WRONLY);
	    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));
	    }
	    return 0;
	}

	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	int main(int argc, char const *argv[])
	{
	    int fd = open("myfifo3",O_WRONLY);
	    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));
	    }
	    return 0;
	}

  • 一个读端:
	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	
	int main(int argc, char const *argv[])
	{
	    int fd1 = open("myfifo1",O_RDONLY);
	    int fd2 = open("myfifo2",O_RDONLY);
	    int fd3 = open("myfifo3",O_RDONLY);
	
	
	    //设置为非阻塞
	
	    int flag = 0;
	    flag = fcntl(fd1,F_GETFL);
	    flag |= O_NONBLOCK;
	    fcntl(fd1,F_SETFL,flag);
	
	   
	    flag = fcntl(fd2,F_GETFL);
	    flag |= O_NONBLOCK;
	    fcntl(fd2,F_SETFL,flag);
	
	    
	    flag = fcntl(fd3,F_GETFL);
	    flag |= O_NONBLOCK;
	    fcntl(fd3,F_SETFL,flag);
	
	    char buf[128] = {0};
	
	    while(true)
	    {
	        memset(buf,0,sizeof(buf));
	        read(fd1,buf,sizeof(buf));
	        printf("myfifo1:%s\n",buf);
	
	        memset(buf,0,sizeof(buf));
	        read(fd2,buf,sizeof(buf));
	        printf("myfifo2:%s\n",buf);
	
	        memset(buf,0,sizeof(buf));
	        read(fd3,buf,sizeof(buf));
	        printf("myfifo3:%s\n",buf);  
	
	        sleep(2); //防止刷屏     
	
	    }
	
	    close(fd1);
	    close(fd2);
	    close(fd3);
	    return 0;
	}

  • 运行结果:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:hi
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:hello
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:
	myfifo1:
	myfifo2:
	myfifo3:china
	myfifo1:
	myfifo2:
	myfifo3:
  • 仅供参考
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值