非阻塞轮询读终端和等待超时
来源:互联网 发布:拉菲软件下载 编辑:程序博客网 时间:2024/06/05 01:08
1、非阻塞轮询读终端
#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <string.h>#include <stdlib.h>#define MSG_TRY "try again\n"int main(void){char buf[10];int fd, n;fd = open("/dev/tty", O_RDONLY|O_NONBLOCK);if(fd<0) {perror("open /dev/tty");exit(1);}tryagain:n = read(fd, buf, 10);if (n < 0) {if (errno == EAGAIN) {sleep(1);write(STDOUT_FILENO, MSG_TRY, strlen(MSG_TRY));goto tryagain;}perror("read /dev/tty");exit(1);}write(STDOUT_FILENO, buf, n);close(fd);return 0;}
2、非阻塞轮询读终端和等待超时
#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <string.h>#include <stdlib.h>#define MSG_TRY "try again\n"#define MSG_TIMEOUT "timeout\n"int main(void){char buf[10];int fd, n, i;fd = open("/dev/tty", O_RDONLY|O_NONBLOCK);if(fd<0) {perror("open /dev/tty");exit(1);}for(i=0; i<5; i++) {n = read(fd, buf, 10);if(n>=0)break;if(errno!=EAGAIN) {perror("read /dev/tty");exit(1);}sleep(1);write(STDOUT_FILENO, MSG_TRY, strlen(MSG_TRY));}if(i==5)write(STDOUT_FILENO, MSG_TIMEOUT, strlen(MSG_TIMEOUT));elsewrite(STDOUT_FILENO, buf, n);close(fd);return 0;}
0 0
- 非阻塞轮询读终端和等待超时
- 非阻塞读终端与等待超时
- 阻塞和非阻塞等待
- Linux阻塞和非阻塞读终端
- 阻塞等待和忙等待,以及超时问题
- IO 的阻塞和非阻塞一:等待队列
- connect超时设置.阻塞和非阻塞 ioctlsocket函数
- 等待队列 阻塞非阻塞
- 非阻塞超时轮询
- 阻塞模式下的超时等待
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- java.lang.NoSuchMethodError
- smartConfig---通过数据包长度位作为编码位,进行短数据通讯
- struts2-Action-2-访问实现过程
- 机器学习算法笔记之6:数据预处理
- hdu1036 Average is not Fast Enough!(水题一枚)
- 非阻塞轮询读终端和等待超时
- linux网络编程(4)——网络TCP_IP协议族
- AndroidStudio使用SVN进行代码的检出,更新和提交
- java- 数组练习题1
- SuperMap iServer 如何配置单机集群
- Can't find bundle for base name DBInfo, locale zh_CN解决方法
- 实例
- SPI驱动之主控制器驱动程序
- Discuz!论坛运营之修改帖子10000字节长度限制最大字数的方法