3

The documentation of poll() did not explain this in detail. While polling on an fd, when should one POLLIN and when should one use POLLPRI? Any insights will be useful.

Punit Soni
  • 1,229
  • 3
  • 17
  • 26

3 Answers3

10

There are some description on poll() document.

POLLIN There is data to read. POLLPRI There is urgent data to read.

If you use POLLIN only, poll() will return if there is data or urgent data to read. If you use POLLPRI only, poll() will return only if there is urgent data to read, but ignore normal data.

What's urgent data? Like tcp's out-of-band data. In TCP frame header, there is a flag named urg_data. Urg_data means this frame has higher priority to delivery. Once kernel received a urg_data maked frame, it set a POLLPRI flag! Look at the following code:

...
if (tp->urg_data & TCP_URG_VALID)
   mask |= POLLPRI;
....
return mask;
Bennett Ma
  • 309
  • 1
  • 2
  • 7
  • As @paxdiablo said too: the POSIX standard says this about `POLLIN`: “Data *other than high-priority data* may be read without blocking.” – Gandaro Jan 30 '15 at 20:40
2

According to UNIX Network Programming, 3rd edition,POLLPRI is exclusive to STREAMS, and will never be triggered when working with TCP or UDP on POSIX compliant systems.

https://books.google.dk/books?id=ptSC4LpwGA0C&pg=PA183&lpg=PA183&dq=POLLRDNORM+POLLRDBAND+POLLPRI&source=bl&ots=Ks7CSockOv&sig=nqzhameGpvzi_TSq1-2qi9gqdaY&hl=en&sa=X&ved=0ahUKEwjjqaOh9cbPAhWGjCwKHWjxCdw4ChDoAQg_MAc#v=onepage&q=POLLRDNORM%20POLLRDBAND%20POLLPRI&f=false

Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
1

I always use both of them, that's possible since they're bitmasks (so you can use POLLIN | POLLPRI).

The priority entries are for information that's considered more important than regular information. Ideally, you'd ask for both types and then check/process the priority ones first.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • from Bennett's answer, it implies if we use POLLIN, we get both normal and urgent data. So, POLLPRI can be used to prioritize this data. – Punit Soni Mar 15 '13 at 05:47
  • 1
    @Punit, the POSIX specs state (for POLLIN) "Data **other than** high-priority data may be read without blocking" so, in order to be safe, it's not wise to assume asking for POLLIN will tell you if _only_ priority traffic has arrived. Better to use both, that way you're certain. – paxdiablo Mar 15 '13 at 05:56
  • 2
    I can confirm waiting for POLLIN can be insufficient in actual practice, at least with epoll and /dev/dahdi/timer. – Left For Archive Apr 10 '14 at 20:42
  • I'm not sure this is strictly accurate since for at least TCP you will only read data that alerts you via `POLLPRI` if you use `recv(...., MSG_OOB)` --- [example](http://beej.us/guide/bgnet/output/html/multipage/pollman.html) – Patrick Collins Feb 25 '16 at 07:15