我在使用函数
我调查了函数
我的电脑是安装了gcc 4.6.3的x86_64 Ubuntu 12.04。
这是我为这个问题编写的代码。
fcntl
处理stdin
时遇到了问题,当我将stdin
的FD状态标志设置为O_NONBLOCK
时,它可以正常工作,但是带有副作用,即stdout和stderr的状态标志也已更改为O_NONBLOCK
。我调查了函数
fcntl
、SYSCALL_DEFINE3
和do_fcntl
的源代码,但没有任何有用的信息。我已经在Stackoverflow和Google上搜索过,但也没有找到答案。我认为这可能与内核或glibc实现有关。我的电脑是安装了gcc 4.6.3的x86_64 Ubuntu 12.04。
int flag = 0;
int value = O_NONBLOCK;
int fd = open("./tmp", O_RDONLY);
if(-1 == (flag = fcntl(fd, F_GETFL)))
fprintf(stdout, "%d:%s\n", errno, strerror(errno));
flag = fcntl(stdin->_fileno, F_GETFL);
flag = fcntl(stderr->_fileno, F_GETFL);
if(-1 == (flag = fcntl(stdout->_fileno, F_GETFL)))
fprintf(stdout, "%d:%s\n", errno, strerror(errno));
flag = fcntl(stdout->_fileno, F_SETFL, flag | O_NONBLOCK);
flag = fcntl(fd, F_GETFL);
flag = fcntl(stdin->_fileno, F_GETFL);
flag = fcntl(stdout->_fileno, F_GETFL);
flag = fcntl(stderr->_fileno, F_GETFL);
flag = fcntl(stdin->_fileno, F_SETFL, flag | O_APPEND);
flag = fcntl(fd, F_GETFL);
flag = fcntl(stdin->_fileno, F_GETFL);
flag = fcntl(stdout->_fileno, F_GETFL);
flag = fcntl(stderr->_fileno, F_GETFL);
close(fd);
这是我为这个问题编写的代码。
echo "" | ./your_program
) - SheetJSgdb a.out
命令,并显示标志,当将 O_NONBLOCK 设置为标准输出时,我发现标准输入和标准错误流也都变成了 O_NONBLOCK。这有点让人困惑。~ - Incarnation P. Lee