Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

select() - multiplex input/output

&pagelevel(3)&pagelevel



#include <sys.time.h> 
#include <sys.socket.h>


Kernighan-Ritchie-C:
int select(nfds, readfds, writefds, exceptfds, timeout);

int nfds;
fd_set *readfds, *writefds, *exceptfds;
struct timeval *timeout;


FD_SET(fd, &fdset);

FD_CLR(fd, &fdset);

FD_ISSET(fd, &fdset);

FD_ZERO(&fdset);

int fd;
fd_set fdset;

ANSI-C:
int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);

FD_SET(fd, &fdset);
FD_CLR(fd, &fdset);
FD_ISSET(fd, &fdset);
FD_ZERO(&fdset);

int fd;
fd_set fdset;


Description

The select() function tests three different sets of socket descriptors passed with the readfds, writefds and exceptfds parameters.
select() determines

  • which descriptors in the set passed with readfds are ready for reading,

  • which descriptors in the set passed with writefds are ready for writing,

  • for which descriptors in the set passed with exceptfds a pending exception exists.

The exceptfds parameter is not evaluated by SOCKETS(BS2000) at present.

The bit masks for the individual descriptor sets are stored as bit fields in integer strings. The maximum size of the bit fields should be determined by using the getdtablesize() function (see section "getdtablesize() - get size of descriptor table"). The required memory should be requested from the system dynamically.

The nfds parameter specifies how many bits are to be tested in each bit mask. select() tests bits 0 to nfds-1 in the individual bit masks.
select() replaces the descriptor sets passed in the call with the appropriate subsets. These subsets include all the respective descriptors that are ready for the operation involved.

You can use the following macros to manipulate bit masks or descriptor sets:

FD_SET(fd, &fdset)

Extends the descriptor set fdset by descriptor fd.

FD_CLR(fd, &fdset)

Removes descriptor fd from descriptor set fdset.

FD_ISSET(fd, &fdset)

Tests whether descriptor fd is a member of descriptor set fdset:

  • Return value != 0: fd is a member of fdset.

  • Return value == 0: fd is not a member of fdset.

FD_ZERO(&fdset)

Removes all file descriptors from descriptor set fdset.

The behavior of these macros is undefined if the descriptor value is <0 or greater than the maximum size for bit fields that was determined with the getdtablesize() function.

The timeout parameter defines the maximum time that the select() function has to complete the selection of the ready descriptors. If timeout is the null pointer, select() blocks for an undefined time. The maximum time value that will be accepted is ~ 24.85 days. Values bigger than that will lead to waiting until an event occurs.
You can enable polling by passing a pointer for timeout to a timeval object whose components all have the value 0.

If the descriptors are of no interest, the null pointer can be passed as the current parameter for readfds, writefds and exceptfds.

If select() determines the “read” readiness of a socket descriptor after calling listen(), this indicates that a subsequent accept() call for this descriptor will not block.

Return value

>0:

The positive number indicates the number of ready descriptors in the descriptor set.

0:

Indicates that the timeout limit has been exceeded. The descriptor sets are then undefined.

-1:

If errors occur. errno is set to indicate the error. The descriptor sets are then undefined.

7F000000:

Trace event for user sockets trace. 

Errors indicated by errno

EBADF

One of the descriptor sets specified an invalid descriptor.

EINTR

The select() call was interrupted by soc_wake().

ENETDOWN

The connection to the network is down.

When virtual hosts are used, ENETDOWN does not necessarily mean that the entire network is down. It can also mean that only the network of a virtual host has failed.

Note

In rare circumstances, select() may indicate that a descriptor is ready for writing, although a write attempt would actually block. This can occur if the system resources required for writing are exhausted or not present. If it is critical for your application that writes to a file descriptor do not block, you should set the descriptor to non-blocking input/output with a soc_ioctl() call.

See also

accept(), connect(), listen(), recv(), send(), soc_ioctl(), soc_write(), soc_writev()