select函数(二)
来源:互联网 发布:淘宝drjart天猫旗舰店 编辑:程序博客网 时间:2024/05/21 18:46
//输入a转变为大写A#include <stdio.h>#include <ctype.h>#include <ctype.h>#include <unistd.h>#include <stdlib.h>#include <sys/select.h>#define N 1024int main(void){fd_set rfds, tmp;int retval;int maxfd;struct timeval tv;//char buf[N];char ch, ch_ret;int n;FD_ZERO(&rfds);FD_SET(STDIN_FILENO, &rfds);maxfd = STDIN_FILENO;while(1){tv.tv_sec = 3;tv.tv_usec = 0;tmp = rfds;retval = select(maxfd + 1, &tmp, NULL, NULL, &tv);//retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);if(retval == -1){perror("select fail\n");exit(1);}else if(retval > 0){if(FD_ISSET(maxfd, &rfds)){n = read(STDIN_FILENO, &ch, 1);if(n == -1){perror("read fail\n");exit(1);}if( ch == 'q' ){//exit(1);break;}if( ch == '\n'){continue;} ch_ret= toupper(ch);write(STDOUT_FILENO, &ch_ret, 1);putchar('\n');continue;}}else if(retval == 0){printf("time out\n");printf("%ld\n", tv.tv_sec);//exit(1);}}return 0;}/*akaedu@akaedu-G41MT-D3:~/lin/20140820_dop2_select$ ./a.out aAxXvVmMtime out0time out0gGtime out0^Cakaedu@akaedu-G41MT-D3:~/lin/20140820_dop2_select$ */<pre name="code" class="cpp">/* * reads input from pipes p1, p2 * using select() for multiplexing * * tty1:cat > ./p1 * tty2:cat > ./p2 * tty3: ./select */ #include <fcntl.h>#include <stdio.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>int main(void) {int fds[2]; char buf[4096]; int i, rc, maxfd;fd_set watchset; /* 欲监听read操作的集合 */fd_set inset; /* select()调用后,被修改的集合 */if((fds[0]=open("p1", O_RDONLY|O_NONBLOCK)) < 0){ perror("open p1"); return 1; }if((fds[1]=open("p2", O_RDONLY|O_NONBLOCK)) < 0){ perror("open p2"); return 1; }/* 清零集合,将监听描述符加入集合内 */FD_ZERO(&watchset);FD_SET(fds[0], &watchset);FD_SET(fds[1], &watchset);/* 找出最大文件描述符, 用以select第一个参数 */maxfd = fds[0] > fds[1] ? fds[0] : fds[1];/* 循环监视两个文件描述符是否有read条件*/while(FD_ISSET(fds[0], &watchset) || FD_ISSET(fds[1], &watchset)){inset = watchset;// backup, 因为select()调用结束会修改参二(传出参数) if(select(maxfd+1, &inset, NULL,NULL,NULL) < 0){perror("select"); return 1; } //从监听的两个文件描述符中去判断,哪个还在集合中 for(i = 0; i < 2; i++){if(FD_ISSET(fds[i], &inset)){//具备read条件rc = read(fds[i], buf, sizeof(buf) - 1);//read之if(rc < 0){perror("read"); return 1;}else if(!rc){/* 管道已经被关闭,无需再监听了 */close(fds[i]);FD_CLR(fds[i], &watchset);//清除出监听集合中}else{buf[rc] = '\0';printf("read: %s", buf);} } } }return 0;}
0 0
- select函数(二)
- select函数(二)
- Linux select函数(二)
- Linux select函数(二)
- select函数(一)
- 学习select(函数)
- 学习select(函数)
- select()函数、poll()函数
- select()函数、poll()函数
- platform按键驱动(二)——编程测试实例以及select()函数详解
- select()函数的应用
- select()函数分析(linux)
- 细谈select()函数(转)
- select()函数和fd_set
- Select()函数的介绍
- socket select()函数详解
- select函数(C语言)
- MySQL必知必会笔记(二)SELECT语句 计算字段 数据处理函数 汇总函数 分组数据 子查询
- HDU 1853 Cyclic Tour(二分图最优匹配:有向环覆盖)
- bzoj1237 wikioi2430 [SCOI2008]配对 dp
- A*算法入门
- UVA10391
- python 内置函数
- select函数(二)
- 当开发者产生一个伟大的想法之后应该做的10件事
- Codeforces Round #262 (Div. 2) B. Little Dima and Equation
- [WARALD美国职业谈]为PHD正名-说说PHD博士就业的一些问题 (全文)
- 循环数组的最大子段和
- sgu 199 Beautiful People (DP-LIS O(nlogn)算法 输出所选元素)
- 看WARALD博客 关于PHD和MASTER比较的摘要
- 欢乐暑假线上编程比赛第三题:轮换数
- 某邻居的WIFI密码