2

The reference about poll() just says “The specified fd value is invalid. This flag is only valid in the revents member; it shall ignored in the events member.”.

That's not clear (at least to me): if the file descriptor is invalid, shouldn't poll return an error instead (negative return value)?

I had the same question about POLLERR and learned somewhere it may refers to, as an example, error in a device or network. This one, POLLNVAL, remains mysterious.

Do someone know example cases?

2

POLLNVAL is set if the file descriptor number does not correspond to a file descriptor.

I don't know for sure why this behavior was chosen rather than having poll return an error. The reason may have been ease of implementation: it allows the implementation to loop over the array of struct pollfd a single time, without having to cope with an early return where some elements of the array would have been modified and others not.

It's also possible that the reason was programmer convenience. Since calling poll on an invalid file descriptor is not an error, it's possible to close a file descriptor and still include it in the array. This convenience is of limited interest: you can only do this as long as you don't open any other file (because it might reuse the file descriptor), and you still pay the (tiny) performance penalty for the array element. It can be useful in a multithreaded program, where one thread may call close while another thread is engaged in a poll call or about to do so: this is not an error, the polling thread only needs to be notified before an open.

| improve this answer | |
  • To me, the last reason, the one involving threads, is more convincing than the former convenience. I will keep the latter reason in mind. – Hibou57 Mar 1 '15 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.