Skip to content
Advertisement

file descriptor unable to read 1 byte

I’m a beginner in Unix/Linux, just a question on file descriptor, my textbooks asks a question:

In most Unix systems, typing ctrl-d indicates EOF on standard input. What happens if you type ctrl-d to the program in Figure 12.6(just a program that use select()) to while it is blocked in the call to select?

enter image description here

and the answer is :

a descriptor is ready for reading if a request to read 1 byte from that descriptor would not block. If EOF becomes true on a descriptor, then the descriptor is ready for reading because the read operation will return immediately with a zero return code indicating EOF. Thus, typing ctrl-d causes the select function to return with descriptor 0 in the ready set.

so my question:

why the file descriptor would not be able to read 1 byte before we press ctrl-d?

Advertisement

Answer

select blocks until there is something to read from at least one of the file descriptors in its fd_set. Prior to pressing CTRL-d there are no bytes to read. After pressing CTRL-d, stdin is closed, and when this happens the EOF byte is then available to read from stdin’s buffer.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement