Previously when I needed to herd a bunch of files / devices in real time
from a single process, I have used "select". It's in most of the
textbooks.

Last time I followed things around in the debugger I discovered that
lo and behold, select is implemented in terms of "poll" these days.

So the other day when I needed select again, I thought I'd try "poll"
instead.

Easier, tidier.

So that's the tip'o'the day... if you want to use select.

Don't.

Use "poll"

See "man poll" for more details.

NAME
       poll, ppoll - wait for some event on a file descriptor

SYNOPSIS
       #include <poll.h>

       int poll(struct pollfd *fds, nfds_t nfds, int timeout);

       #define _GNU_SOURCE
       #include <poll.h>

       int ppoll(struct pollfd *fds, nfds_t nfds,
               const struct timespec *timeout, const sigset_t *sigmask);

DESCRIPTION
       poll()  performs a similar task to select(2): it waits for one of a set
       of file descriptors to become ready to perform I/O.

       The set of file descriptors to be monitored is  specified  in  the  fds
       argument, which is an array of nfds structures of the following form:

           struct pollfd {
               int   fd;         /* file descriptor */
               short events;     /* requested events */
               short revents;    /* returned events */
           };

       The field fd contains a file descriptor for an open file.

       The  field  events  is  an  input  parameter, a bit mask specifying the
       events the application is interested in.

       The field revents is an output parameter, filled by the kernel with the
       events  that  actually  occurred.   The  bits  returned  in revents can
       include any of those specified in events, or one of the values POLLERR,
       POLLHUP,  or POLLNVAL.  (These three bits are meaningless in the events
       field, and will be set in the revents field whenever the  corresponding
       condition is true.)

       If  none of the events requested (and no error) has occurred for any of
       the file descriptors, then  poll()  blocks  until  one  of  the  events
       occurs.

       The  timeout  argument  specifies  an upper limit on the time for which
       poll() will block, in milliseconds.  Specifying  a  negative  value  in
       timeout means an infinite timeout.

       The  bits that may be set/returned in events and revents are defined in
       <poll.h>:

              POLLIN There is data to read.

              POLLPRI
                     There is urgent data to read (e.g., out-of-band  data  on
                     TCP  socket;  pseudo-terminal  master  in packet mode has
                     seen state change in slave).

              POLLOUT
                     Writing now will not block.

              POLLRDHUP (since Linux 2.6.17)
                     Stream socket peer closed connection, or shut down  writ‐
                     ing  half  of  connection.   The _GNU_SOURCE feature test
                     macro must be defined in order to obtain this definition.

              POLLERR
                     Error condition (output only).

              POLLHUP
                     Hang up (output only).

              POLLNVAL
                     Invalid request: fd not open (output only).

  >snip<

RETURN VALUE
       On success, a positive number is returned; this is the number of struc‐
       tures  which  have  non-zero  revents  fields  (in  other  words, those
       descriptors with events or errors reported).  A value  of  0  indicates
       that  the call timed out and no file descriptors were ready.  On error,
       -1 is returned, and errno is set appropriately.




John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : [email protected]
New Zealand

Reply via email to