码迷,mamicode.com
首页 > 其他好文 > 详细

IO复用之 select

时间:2020-06-30 14:42:04      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:timeval   sys   fd_set   sse   set   dev   lse   style   eva   

 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
int main ()
{
        int keyboard;
        int ret,i;
        char c;
        fd_set readfd;
        struct timeval timeout;
        keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK);
        assert(keyboard>0);
        while(1)
        {
                timeout.tv_sec=5;
                timeout.tv_usec=0;
                FD_ZERO(&readfd);
                FD_SET(keyboard,&readfd);
                ret=select(keyboard+1,&readfd,NULL,NULL,&timeout);
                //select error when ret = -1  
                if (ret == -1)
                        perror("select error");
                //data coming when ret>0  
                else if (ret)
                {
                        if(FD_ISSET(keyboard,&readfd))
                        {
                                i=read(keyboard,&c,1);
                                if(\n==c)
                                        continue;
                                printf("the input is %c\n",c);
                                if (q==c)
                                        break;
                        }
                }
                //time out when ret = 0  
                else if (ret == 0)
                        printf("time out\n");
        }
}

 

IO复用之 select

标签:timeval   sys   fd_set   sse   set   dev   lse   style   eva   

原文地址:https://www.cnblogs.com/nanqiang/p/13213446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!