QenBau
QenBau

Reputation: 395

can select() prevent read() from getting interrupted?

According to POSIX, if we use a select() to decide when we can use a read without blocking, is it possible that the read() is interrupted by a signal and returns EINTR?

Upvotes: 0

Views: 103

Answers (2)

John Bollinger
John Bollinger

Reputation: 180038

If we use a select() to decide when we can use a read without blocking, is it possible that the read() is interrupted by a signal and returns EINTR?

Yes.

I infer that the question is at least in part motivated by the recognition that read() indicates EINTR only if it is interrupted before transferring any data. But "before transferring any data" is not the same thing as "while blocked on data becoming available". As far as the specifications are concerned, the question of whether there is any data available to read, such that a read() call would not block, is orthogonal to the question of whether a read() call might be interrupted by a signal before transferring any data.

If you are looking for more than that then consider also that read() is a cancellation point, so it checks on entry whether there is a pending cancellation request. Since it does non-trivial work before performing any data transfer, there is necessarily at least a narrow window in which read() could receive a signal before it starts transferring data, even when data are already available.

Upvotes: 2

SergeyA
SergeyA

Reputation: 62553

Your reads certainly can be interrupted by signals. All that select guarantees is that read will not be blocked. It has absolutely nothing to say about possible interruption by signals.

From man page:

select() allows a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform a corresponding I/O operation (e.g., read(2), or a sufficiently small write(2)) without blocking.

Moreover, how can select predict the future? It knows that right now socket has some data which can be read. How can it tell you if signal will be raised when you try to read this data?

Upvotes: 3

Related Questions