The Linux Programming Interface

(nextflipdebug5) #1

1342 Chapter 63


Regular files
File descriptors that refer to regular files are always marked as readable and writ-
able by select(), and returned with POLLIN and POLLOUT set in revents for poll(), for the
following reasons:

z A read() will always immediately return data, end-of-file, or an error (e.g., the
file was not opened for reading).
z A write() will always immediately transfer data or fail with some error.

SUSv3 says that select() should also mark a descriptor for a regular file as having
an exceptional condition (though this has no obvious meaning for regular
files). Only some implementations do this; Linux is one of those that do not.

Terminals and pseudoterminals
Table 63-3 summarizes the behavior of select() and poll() for terminals and
pseudoterminals (Chapter 64).
When one half of a pseudoterminal pair is closed, the revents setting returned
by poll() for the other half of the pair depends on the implementation. On Linux, at
least the POLLHUP flag is set. However, other implementations return various flags to
indicate this event—for example, POLLHUP, POLLERR, or POLLIN. Furthermore, on some
implementations, the flags that are set depend on whether it is the master or the
slave device that is being monitored.

Pipes and FIFOs
Table 63-4 summarizes the details for the read end of a pipe or FIFO. The Data in
pipe? column indicates whether the pipe has at least 1 byte of data available for read-
ing. In this table, we assume that POLLIN was specified in the events field for poll().
On some other UNIX implementations, if the write end of a pipe is closed,
then, instead of returning with POLLHUP set, poll() returns with the POLLIN bit set
(since a read() will return immediately with end-of-file). Portable applications
should check to see if either bit is set in order to know if a read() will block.
Table 63-5 summarizes the details for the write end of a pipe. In this table, we
assume that POLLOUT was specified in the events field for poll(). The Space for PIPE_BUF
bytes? column indicates whether the pipe has room to atomically write PIPE_BUF bytes
without blocking. This is the criterion on which Linux considers a pipe ready for
writing. Some other UNIX implementations use the same criterion; others con-
sider a pipe writable if even a single byte can be written. (In Linux 2.6.10 and ear-
lier, the capacity of a pipe is the same as PIPE_BUF. This means that a pipe is
considered unwritable if it contains even a single byte of data.)

Table 63-3: select() and poll() indications for terminals and pseudoterminals

Condition or event select() poll()
Input available r POLLIN
Output possible w POLLOUT
After close() by pseudoterminal peer rw See text
Pseudoterminal master in packet mode detects slave state change x POLLPRI
Free download pdf