06.08.2013 Views

pSOSystem System Calls - Read

pSOSystem System Calls - Read

pSOSystem System Calls - Read

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

pNA+ <strong>System</strong> <strong>Calls</strong> select<br />

Usage<br />

timeout Specifies a timeout option. If the fields in timeout are set to 0,<br />

select() returns immediately. If the timeout is a null pointer,<br />

select() blocks until a descriptor is selectable. The structure<br />

timeval is defined in the file and has the following format:<br />

Macros<br />

The status of a socket descriptor in a select mask can be tested with the<br />

FD_ISSET(s, &mask) macro, which returns a non-zero value if s is a member of the<br />

set mask, and 0 if it is not.<br />

In addition, the macros FD_SET(s, &mask) and FD_CLEAR(s, &mask) are provided for<br />

adding and removing socket descriptors to and from a set. s is the socket descriptor,<br />

and mask points to a bit mask data structure. The macro FD_ZERO(&mask) is<br />

provided to clear the set and should be used before the set is used.<br />

These macros are defined in the file .<br />

Example<br />

struct timeval {<br />

long tv_sec; /* number of seconds */<br />

long tv_usec; /* number of microseconds */<br />

The following example shows how to use select() to determine if two sockets have<br />

available data:<br />

fd_set read_mask;<br />

struct timeval wait;<br />

for (;;)<br />

{<br />

wait.tv_sec = 1; /* wait for 1 second */<br />

wait.tv_usec = 0;<br />

FD_ZERO (&read_mask);<br />

FD_SET (s1, &read_mask);<br />

FD_SET (s2, &read_mask);<br />

nb = select (FD_SETSIZE, &read_mask, (fd_set *) 0,<br />

(fd_set *) 0, &wait);<br />

if (nb

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!