问题描述
select函数阻塞调用过程,直到有在任何指定的组文件描述符的活性[...]文件描述符被认为是读就绪如果读取调用不会阻塞。 (参见:)
所以我预期的那样,请在下面的程序会立即在第二回,...迭代,如果你在第一次迭代输入字符串> 4个字符。但是事实并非如此。刚刚经过第一输出后pressing任何其他键它继续处理所有剩余的输入。为什么呢?
So I expected, that select in the following program would immediately return in the 2nd, ... iteration, if you enter a string > 4 characters in the first iteration. However it does not. Just after pressing any other key after the first output it continues to handle all the remaining input. Why?
示例输出:
./selectTest
12345678900
Keyboard input received: 1234
A
Keyboard input received: 5678
Keyboard input received: 900
Keyboard input received: A
code
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
fd_set rfds;
char buf[100];
while(1)
{
FD_ZERO(&rfds);
FD_SET(fileno(stdin), &rfds);
if(-1 == select(FD_SETSIZE, &rfds, NULL, NULL, NULL))
{
perror("select() failed\n");
}
if(FD_ISSET(fileno(stdin), &rfds))
{
printf("Keyboard input received: ");
fgets(buf, 5, stdin);
printf("%s\n", buf);
}
}
return 0;
}
(我知道,我不应该使用select()了,但我学习考试,我们不得不...)
(I know, that I should not use select() anymore, but I am learning for an exam and we have to ...)
推荐答案
您是从的(在通常情况下,当的标准输入的是你的终端)。这些都是技巧性的东西,阅读。
You are reading from a tty(4) (in the usual case when stdin is your terminal). These are tricky things, read the tty demystified.
请注意,你的终端和TTY有一些。因此,一些数据在内核缓冲(以及在标准库)。
Be aware that your terminal and its tty has some line discipline. So, some data is buffered in the kernel (and also in the standard library).
您可能想要把你的tty原始模式。请参见&安培;
You might want to put your tty in raw mode. See termios(3) & stty(1)
但不要失去你的时间,而不是使用像一些库或 readline的
But don't lose your time, instead use some library like ncurses or readline
要与播放选择
,你可能会使用一些的,或许与 mkfifo子的/ tmp / myfifo
然后 yourprogram&LT;的/ tmp / myfifo
,并在另一端,回声你好&GT;的/ tmp / myfifo
To play with select
, you might use some fifo(7), perhaps with mkfifo /tmp/myfifo
then yourprogram < /tmp/myfifo
and, in another terminal, echo hello > /tmp/myfifo
这篇关于Ç - select()的标准输入时已存在的标准输入的缓冲区中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!