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("\n you hit %c. \n",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.

 

转载于:https://www.cnblogs.com/Lwd-linux/p/6220891.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值