I have the following code:
void
set_fl(int fd, int flags) /* flags are file status flags to turn on */
{
int val;
if ((val = fcntl(fd, F_GETFL, 0)) < 0)
err_sys("fcntl F_GETFL error");
val |= flags; /* turn on flags */
if (fcntl(fd, F_SETFL, val) < 0)
err_sys("fcntl F_SETFL error");
}
int
main(void)
{
char buf[BUFSIZ];
set_fl(STDOUT_FILENO, O_NONBLOCK); //set STDOUT_FILENO to nonblock
if(read(STDIN_FILENO, buf, BUFSIZ)==-1) { //read from STDIN_FILENO
printf("something went wrong with read()! %s\n", strerror(errno));
}
}
As you can see, I set STDOUT_FILENO to non-blocking mode but it seems the read operation on STDIN_FILENO finished immediately. Why?
$ ./testprog
something went wrong with read()! Resource temporarily unavailable
Thanks
That’s exactly right: doing a print of
errnoand aperrorcall immediately after the read results in a "resource busy" and an error number of 11, orEAGAIN/EWOULDBLOCK, as shown in this code:which generates:
The reason is that file descriptors have two different types of flags (see here in the section detailing duplicating file descriptors):
The first is file descriptor flags and these are indeed unique per file descriptor. According to the documentation,
FD_CLOEXEC(close onexec) is the only one currently in this camp.All other flags are file status flags, and are shared amongst file descriptors that have been duplicated. These include the I/O operating modes such as
O_NONBLOCK.So, what’s happening here is that the standard output file descriptor was duplicated from the standard input one (the order isn’t relevant, just the fact that one was duplicated from the other) so that setting non-blocking mode on one affects all duplicates (and that would probably include the standard error file descriptor as well, though I haven’t confirmed it).
It’s not usually a good idea to muck about with blocking mode on file descriptors that are duplicated, nor with file descriptors that will likely be inherited by sub-processes – those sub-processes don’t always take kindly to having their standard files misbehaving (from their point of view).
If you want more fine-grained control over individual file descriptors, consider using
selectto check descriptors before attempting a read.