C++ select()系统调用和bash转义
当我使用命令C++ select()系统调用和bash转义,c++,linux,bash,c++11,select,C++,Linux,Bash,C++11,Select,当我使用命令g++-std=c++11编译并运行以下代码,并使用/a.out运行并输入一些文本以查看select调用将返回什么(它应该返回1,因为当我输入文本时,文本就可以被读入)。不知何故,我输入的文本作为bash命令本身转义。有人能解释一下为什么会这样吗 #include <errno.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sy
g++-std=c++11
编译并运行以下代码,并使用/a.out
运行并输入一些文本以查看select调用将返回什么(它应该返回1,因为当我输入文本时,文本就可以被读入)。不知何故,我输入的文本作为bash命令本身转义。有人能解释一下为什么会这样吗
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
int input_timeout (int filedes, unsigned int seconds) {
fd_set set;
struct timeval timeout;
// Initialize the file descriptor set.
FD_ZERO (&set);
FD_SET (filedes, &set);
// Initialize the timeout data structure.
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
// select returns 0 if timeout, 1 if input available, -1 if error.
return select (FD_SETSIZE,
&set, NULL, NULL,
&timeout);
}
int main () {
fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 1));
return 0;
}
然后无限期地等待输入到
what
命令您只检查是否有可从标准输入读取的输入,但实际上您没有读取它,因此它被留在终端缓冲区中。可能与问题无关,但是为什么你要使用所有的c库而不是c++对不起,我完全是从GNU网站上的一个示例复制粘贴了这段代码。。。
bash-3.2 $ ./a.out
what
select returned 1.
bash-3.2$ what