蒋博1026 发表于 2019-8-18 09:31

Linux-IO多路复用select函数实践

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
int main(void)
{
    int ret = -1;
    int fd = -1;
    char buf;
    fd_set myset;
    struct timeval tm;
    fd = open("/dev/input/mouse0",O_RDONLY);
    if(fd < 0)
    {
      perror("open:");
      return -1;
    }
    FD_ZERO(&myset);
    FD_SET(fd, &myset);
    FD_SET(0, &myset);
    tm.tv_sec = 10;
    tm.tv_usec = 0;
    ret = select(fd + 1, &myset, NULL, NULL, &tm);
    if(ret < 0)
    {
      perror("select:");
      return -1;
    }
    else if(ret == 0)
    {
      printf("超时了\n");
    }
    else
    {
      if(FD_ISSET(fd, &myset))
      {
            //处理鼠标
            memset(buf, 0, sizeof(buf));
            read(fd, buf, 2);
            printf("读出的鼠标:[%s]\n",buf);
      }
      if(FD_ISSET(0, &myset))
      {
            //处理键盘
            memset(buf, 0, sizeof(buf));
            read(0, buf, 2);
            printf("读出的键盘:[%s]\n",buf);
      }
    }
    return 0;
}

页: [1]
查看完整版本: Linux-IO多路复用select函数实践