zoukankan      html  css  js  c++  java
  • 1.使用非阻塞方式获取标准输入

    //首先,我们先来看总的main函数框架
    1
    int main() 2 { 3 char c; 4 int i=0; 5 6 nonblock(NB_ENABLE); 7 while(!i) 8 { 9 usleep(1); 10 i=kbhit(); 11 if (i!=0) 12 { 13 c=fgetc(stdin); 14 if (c=='q') 15 i=1; 16 else 17 i=0; 18 } 19 20 fprintf(stderr,"%d ",i); 21 } 22 printf(" you hit %c. ",c); 23 nonblock(NB_DISABLE); 24 25 return 0; 26 }

    nonblock函数的原型如下:

     1 void nonblock(int state)
     2 {
     3     struct termios ttystate;
     4  
     5     //get the terminal state
     6     tcgetattr(STDIN_FILENO, &ttystate);
     7  
     8     if (state==NB_ENABLE)
     9     {
    10         //turn off canonical mode
    11         ttystate.c_lflag &= ~ICANON;
    12         //minimum of number input read.
    13         ttystate.c_cc[VMIN] = 1;//有一个数据时就立刻返回
    14     }
    15     else if (state==NB_DISABLE)
    16     {
    17         //turn on canonical mode
    18         ttystate.c_lflag |= ICANON;
    19     }
    20     //set the terminal attributes.
    21     tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
    22  
    23 }

    nonblock函数主要完成以下工作:

    a.从标准输入获取状态,存放在ttystate中

    b.关闭the canonical mode,通过置0的方式实现。

    c.通过ttystate设置标准输入的状态。

    kbhit函数的原型如下:

     1 int kbhit()
     2 {
     3     struct timeval tv;
     4     fd_set fds;
     5     tv.tv_sec = 0;
     6     tv.tv_usec = 0;
     7     FD_ZERO(&fds);
     8     FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
     9     select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
    10     return FD_ISSET(STDIN_FILENO, &fds);
    11 }

    该函数主要通过timeval和select函数来实现的。select函数主要用于一些某些需要加工的多功能的I/O口中。如果不会使用select函数,可以查看《Unix高级编程》或者参考select函数的用法

    kbhit函数主要完成的工作如下:

    a.设置tv.tv_sec和tv.tv_usec为0,不等待,一直查询端口。

    b.FD的设置

    c.调用select函数,Seems we are only interested in input, so we place out fd set at second parameter of select(), the 3rd is for output and 4th is for exception.

    注意:如果user input被触发,FD_ISSET将返回非0值,否则返回0;所以应该加如下的返回值判断。

    1 while(!kbhit())
    2 {
    3       //do certain operation..
    4 }
    5 //user hits enter.

    Due to the canonical mode of your terminal, you need to hit enter to confirm your user input. Canonical mode means it always wait for enter to confirms the user input.

  • 相关阅读:
    周末给女友讲了遍加密算法,没想到...
    gradle执行打包并导出Apk到指定文件夹
    功能算法
    位运算之异或运算
    禁止ViewPager滑动
    macOS 去掉系统软件更新红点提示
    【转】Kotlin的inline内联函数
    Android Gradle Plugin v3.6.0/3.6.1 构建Bug
    IntelliJ IDEA UML插件
    【LeetCode 1. Two Sum】
  • 原文地址:https://www.cnblogs.com/Lwd-linux/p/6220891.html
Copyright © 2011-2022 走看看