Select
and Poll Function:
The select() and poll() methods
can be a powerful tool when you’re multiplexing network sockets. Specifically,
these methods will indicate when a procedure will be safe to execute on an open
file descriptor without any delays. For instance, a programmer can use these
calls to know when there is data to be read on a socket. By delegating
responsibility to select() and poll(),
you don’t have to constantly check whether there is data to be read. Instead, select() and poll() can
be placed in the background by the operating system and woken up when the event
is satisfied or a specified timeout has elapsed. This process can significantly
increase execution efficiency of a program
As you will see, select() and poll() are very similar in functionality. Often, implementations ofselect() and poll() are mapped onto each other.
As you will see, select() and poll() are very similar in functionality. Often, implementations ofselect() and poll() are mapped onto each other.
select() description
The select function indicates which of the specified file descriptors is ready for reading, ready for writing, or has an error condition pending. When an application calls recv or recvfrom, it is blocked until data arrives for that socket. An application could be doing other useful processing while the incoming data stream is empty. Another situation is when an application receives data from multiple sockets.
Calling recv or recvfrom on a socket that has no data in its input queue
prevents immediate reception of data from other sockets. The select function
call solves this problem by allowing the program to poll all the socket handles
to see if they are available for non-blocking reading and writing operations.
Prototype :
int select(int nfds,
fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
It takes these parameters:
int nfds - The highest file descriptor in all given sets plus one.
It takes these parameters:
int nfds - The highest file descriptor in all given sets plus one.
fd_set
*readfds - File descriptors that will trigger a return when data is ready
to be read.
fd_set
*writefds - File descriptors that will trigger a return when data is ready
to be written to.
fd_set
*errorfds - File descriptors that will trigger a return when an exception
occurs.
struct
timeval *timeout - The maximum period select() should wait for an
event
The return value indicates the number of file descriptors (fds) whose request event has been satisfied.
You can’t modify the fd_set structure by changing its value directly. The only portable way to either set or retrieve the value is by using the provided FD_* macros:
FD_ZERO(fd_set *) - Initializes an fd_set to be empty
FD_CLR(int
fd, fd_set *) - Removes the associated fd from the fd_set
FD_SET(int
fd, fd_set *) - Adds the associated fd to the fd_set
FD_ISSET(int fd, fd_set *) - Returns a nonzero
value if the fd is in fd_set
Upon return from select(), FD_ISSET() can be called for each fd in a given set to identify whether its condition has been met.
With the timeout value, you can specify how long select() will wait for an event. If timeout isNULL, select() will wait indefinitely for an event. If timeout's timeval structures are set to 0,select() will return immediately rather than wait for any event to occur. Otherwise, timeoutdefines how long select() will wait.
Poll()
The poll() method
attempts to consolidate the arguments of select() and
provides notification of a wider range of events.
int poll(struct pollfd fds[ ], nfds_t nfds, int timeout);
It takes these parameters:
struct pollfd fds[ ] - An array of pollfd structures
It takes these parameters:
struct pollfd fds[ ] - An array of pollfd structures
nfds_t
nfds - The number of file descriptors set in fds[ ]
int
timeout - How long poll() should wait for an
event to occur (in milliseconds)
The return value indicates how many fds had an event occur.
A pollfd struct typically includes the following members:
int fd - Indicates which fd to monitor for an event
short
events - A bitwise field that represents which events will be monitored
short
revents - A bitwise field that represents which events were detected in
a call topoll()
Alternatives to select() and poll()
Even with the advantage of having multiplexed event notification via select() or poll(), other implementations exist that provide better performance. However, these implementations are not standardized across platforms. You must weigh the potential performance benefits of using one of these specialized implementations against the loss of portability. We will examine two of these alternatives: Solaris' /dev/poll and FreeBSD's kqueue.
As you will see, both implementations gain their key performance benefits by leveraging the fact that, in the real world, developers continually call select() or poll() with the same fds. To eliminate overhead when passing the same arguments each time, fds that are examined can be cached. This approach also works well when a large number of fds are monitored, because some select() and poll() implementations have scalability issues.
Even with the advantage of having multiplexed event notification via select() or poll(), other implementations exist that provide better performance. However, these implementations are not standardized across platforms. You must weigh the potential performance benefits of using one of these specialized implementations against the loss of portability. We will examine two of these alternatives: Solaris' /dev/poll and FreeBSD's kqueue.
As you will see, both implementations gain their key performance benefits by leveraging the fact that, in the real world, developers continually call select() or poll() with the same fds. To eliminate overhead when passing the same arguments each time, fds that are examined can be cached. This approach also works well when a large number of fds are monitored, because some select() and poll() implementations have scalability issues.
No comments:
Post a Comment