public
Created

Testing select on various platforms...

  • Download Gist
darwin.txt
1 2 3 4 5 6 7 8 9
Output on OS X:
 
ret = 6
fh1 read
fh2 read
fh1 write
fh2 write
fh1 error
fh2 error
debian.txt
1 2 3 4 5 6 7
Output on Linux:
 
ret = 4
fh1 read
fh2 read
fh1 write
fh2 write
select.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <sys/select.h>
#include <fcntl.h>
#include <stdio.h>
 
int main(int argc, char* argv[]) {
int fh1 = open("README", O_RDONLY);
int fh2 = open("README2", O_WRONLY | O_CREAT, 0666);
 
fd_set rdset, wrset, erset;
FD_ZERO(&rdset);
FD_SET(fh1, &rdset);
FD_SET(fh2, &rdset);
FD_ZERO(&wrset);
FD_SET(fh1, &wrset);
FD_SET(fh2, &wrset);
FD_ZERO(&erset);
FD_SET(fh1, &erset);
FD_SET(fh2, &erset);
 
struct timeval timeout;
timeout.tv_sec = 1;
 
int nfds = fh1 > fh2 ? fh1 : fh2;
int ret = select(nfds+1, &rdset, &wrset, &erset, &timeout);
 
printf("ret = %d\n", ret);
if( FD_ISSET(fh1, &rdset) ) printf("fh1 read\n");
if( FD_ISSET(fh2, &rdset) ) printf("fh2 read\n");
if( FD_ISSET(fh1, &wrset) ) printf("fh1 write\n");
if( FD_ISSET(fh2, &wrset) ) printf("fh2 write\n");
if( FD_ISSET(fh1, &erset) ) printf("fh1 error\n");
if( FD_ISSET(fh2, &erset) ) printf("fh2 error\n");
 
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.