Ç - select()的标准输入时已存在的标准输入的缓冲区中的数据 [英] C - select() on stdin when there is already data in stdin's buffer

查看:175
本文介绍了Ç - select()的标准输入时已存在的标准输入的缓冲区中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

select函数阻塞调用过程,直到有在任何指定的组文件描述符的活性[...]文件描述符被认为是读就绪如果读取调用不会阻塞。 (参见: https://www.gnu.org/软件/的libc /手动/ html_node /等待换I_002fO.html

The select function blocks the calling process until there is activity on any of the specified sets of file descriptors [...] A file descriptor is considered ready for reading if a read call will not block. (See: https://www.gnu.org/software/libc/manual/html_node/Waiting-for-I_002fO.html)

所以我预期的那样,请在下面的程序会立即在第二回,...迭代,如果你在第一次迭代输入字符串> 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 ...)

推荐答案

您是从的 TTY(4)(在通常情况下,当的标准输入的是你的终端)。这些都是技巧性的东西,阅读 TTY揭秘

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原始模式。请参见的termios(3)&安培; 的stty(1)

You might want to put your tty in raw mode. See termios(3) & stty(1)

但不要失去你的时间,而不是使用像一些库 ncurses的或<一个HREF =htt​​p://www.gnu.org/s/readline相对=nofollow> readline的

But don't lose your time, instead use some library like ncurses or readline

要与播放选择,你可能会使用一些的 FIFO(7),或许与 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()的标准输入时已存在的标准输入的缓冲区中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆