stdin 上的 read() 返回 EOF 而不是等待输入
有谁知道为什么运行以下代码可能导致对该 fd(即标准输入)的所有未来 read() 调用立即返回 0 而不是阻塞输入?
termios newTerminalSettings;
tcgetattr(inFd, &newTerminalSettings);
newTerminalSettings.c_lflag &= ~ICANON;
tcsetattr(inFd, TCSANOW, &newTerminalSettings);
删除 tcsetattr 行使 read() 按预期工作。
也尝试过:
fcntl(inFd, F_SETFL, 0);
没有运气。
请注意,我目前有 2 个不同的终端。在其中之一中运行应用程序会导致 read 立即返回。在其他中运行它会导致读取阻塞以获取输入。可能是什么?
预先感谢:-)
复制来源:
#include <iostream>
#include <termios.h>
using namespace std;
int main(void) {
termios newTerminalSettings;
tcgetattr(0, &newTerminalSettings);
newTerminalSettings.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &newTerminalSettings);
char readBuf[5000];
cout << "read returned: " << read(0, readBuf, sizeof(readBuf));
return 0;
}
Does anyone know why running the following code may cause all future read() calls on that fd (which is stdin) to immediately return 0 instead of blocking for input?
termios newTerminalSettings;
tcgetattr(inFd, &newTerminalSettings);
newTerminalSettings.c_lflag &= ~ICANON;
tcsetattr(inFd, TCSANOW, &newTerminalSettings);
Removing the tcsetattr line makes read() work as expected.
Also tried:
fcntl(inFd, F_SETFL, 0);
with no luck.
Note that I currently have 2 different terminals. Running the app in one of them causes read to return instantly. Running it in the other causes read to block for input. What could it be?
Thanks in advance :-)
Repro source:
#include <iostream>
#include <termios.h>
using namespace std;
int main(void) {
termios newTerminalSettings;
tcgetattr(0, &newTerminalSettings);
newTerminalSettings.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &newTerminalSettings);
char readBuf[5000];
cout << "read returned: " << read(0, readBuf, sizeof(readBuf));
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的问题是屏蔽了 ICANON,这又会关闭规范模式(启用非规范模式)。根据 termios(3) 的联机帮助页:
“在非规范模式下,输入可以立即使用(用户无需键入行分隔符),并且行编辑被禁用。”
为了避免这篇文章混乱,请参阅手册页,因为它详细解释了此行为。当 read 没有任何内容可返回时(如在异步模式下),会发生以下行为。
来自toptal Engineering的Gergely
I think your problem is masking out ICANON, which in turn turns off canonical mode (enables noncanonical mode). According to the manpage of termios(3):
"In noncanonical mode input is available immediately (without the user having to type a line-delimiter character), and line editing is disabled."
To avoid cluttering this post, please see the man page, as it explains this behavior in detail. The following behavior happens when read has nothing to return (as in asynchronous mode).
Gergely from toptal Engineering
请参阅此处
Refer here