Sponsors
Submitted by xman on Wed, 25/04/2007 - 6:34pm
Posted in
Highlight the code from http://www.vyvy.org/main/node/129 using Texy!
#include <sys/select.h>
#include <sys/time.h>
...
fd_set rset_backup, rset, wset_backup, wset; // Declare the descriptor sets
int maxfdp = 0;
struct timeval timeout_backup, timeout;
int retval;
/* The descriptor sets */
FD_ZERO(&rset_backup); // Clear all bits
FD_SET(fd_read1, &rset_backup); // Set the bit
FD_SET(fd_read2, &rset_backup);
FD_ZERO(&wset);
FD_SET(fd_write1, &wset_backup);
// FD_CLR(fd, &wset_backup); // Clear the bit for fd
maxfdp = (fd_read1 > fd_read2) ? fd_read1 : fd_read2;
if (fd_write1 > maxfdp)
maxfdp = fd_write1;
/* Timeout: 2.3 seconds */
timeout_backup.tv_sec = 2;
timeout_backup.tv_usec = 300;
for (;;) {
rset = rset_backup;
wset = wset_backup;
timeout = timeout_backup; // Linux's select() can modify timeout
retval = select(maxfdp + 1,
&rset, // Ready for reading? Can be NULL.
&wset, // Ready for writing? Can be NULL.
NULL, // Have an exception condition pending? Can be NULL.
&timeout // Can be NULL.
);
if (retval > 0) { // Positive counts of ready descriptors
if (FD_ISSET(fd_read1, &rset)) {
// fd_read1 is readable
...
}
if (FD_ISSET(fd_read2, &rset)) {
// fd_read2 is readable
...
}
if (FD_ISSET(fd_write1, &wset)) {
// fd_write1 is writable
...
}
}
else if (retval == 0) { // Timeout
// Linux (but not on most other systems) reflects the amount of time not slept in 'timeout'.
...
}
else { // retval < 0
// Error
...
}
}
...

Post new comment