IO学习系列之非阻塞IO

编程入门 行业动态 更新时间:2024-10-26 10:38:27

IO学习<a href=https://www.elefans.com/category/jswz/34/1768176.html style=系列之非阻塞IO"/>

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:himyfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:hellomyfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:myfifo1:myfifo2:myfifo3:chinamyfifo1:myfifo2:myfifo3:
  • 仅供参考

更多推荐

IO学习系列之非阻塞IO

本文发布于:2023-11-16 13:10:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:系列之   IO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!