select函数的简单使用

来源:互联网 发布:收看电视直播的软件 编辑:程序博客网 时间:2024/06/17 00:28

学习于 Linux man手册

功能描述:使用select 检测键盘在5秒的时间内是否有输入,以回车结尾作为一次输入


 #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> int main(void) {   fd_set rfds;   struct timeval tv;   int retval;   /* Watch stdin (fd 0) to see when it has input. */   FD_ZERO(&rfds);   FD_SET(0, &rfds);   /* Wait up to five seconds. */   tv.tv_sec = 5;   tv.tv_usec = 0;   retval = select(1, &rfds, NULL, NULL, &tv);   /* Don't rely on the value of tv now! */   if (retval == -1)   perror("select()");   else if (retval)   printf("Data is available now.\n");   /* FD_ISSET(0, &rfds) will be true. */   else   printf("No data within five seconds.\n");   exit(EXIT_SUCCESS); }


原创粉丝点击