I have met a problem when acting function fcntl
on stdin
, when I set the stdin
FD status flag to O_NONBLOCK
, It works well but within a side effect. The status flag of stdout and stderr has also changed to O_NONBLOCK
.
I investigated the source code of function fcntl
, SYSCALL_DEFINE3
and do_fcntl
, but got nothing helps. Also stackoverflow or google. I consider that it may related to kernel or glibc implementation.
My computer is Ubuntu 12.04 on x86_64, within gcc 4.6.3 installed.
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);
This is my code for this problem.