非阻塞轮询读终端和等待超时
2017-04-17 17:20
225 查看
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;}
相关文章推荐
- 非阻塞读终端与等待超时
- 阻塞模式下的超时等待
- 非阻塞超时轮询
- Linux定时器处理之实时信号使用,消息队列阻塞模型,避免超时等待
- 【转】阻塞模式下的超时等待
- [Linux]阻塞与非阻塞(等待队列、轮询)机制
- Linux驱动中的阻塞与非阻塞I/O,等待队列,轮询操作
- 阻塞等待和忙等待,以及超时问题
- 阻塞模式下的超时等待
- 【腾讯Bugly干货分享】微信终端跨平台组件 mars 系列(二) - 信令传输超时设计
- 【ORA-02049】超时分布式事务处理等待锁 解决方法
- mysql事物锁等待超时,报错 Lock wait timeout exceeded; try restarting transaction
- 超时分布式事务处理等待锁 解决办法
- MAC OSX终端登录ssh保持激活状态防止超时
- / 子线程阻塞,等待信号,然后输出字符串 // 主线程从键盘录入字符,给子线程发信号。
- JAVA中的多线程操作 线程等待、阻塞、让步等
- java多线程中等待线程与被阻塞线程的区别
- socket 设置阻塞和超时
- Flash Builder 4 无法调试,正在等待 adobe flash player 连接调试器,连接失败;会话已超时
- windows下如何解决PHP调用的外部程序超时阻塞问题