Skip to content

Instantly share code, notes, and snippets.

@Benabik
Created August 25, 2011 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benabik/1171339 to your computer and use it in GitHub Desktop.
Save Benabik/1171339 to your computer and use it in GitHub Desktop.
Testing select on various platforms...
Output on OS X:
ret = 6
fh1 read
fh2 read
fh1 write
fh2 write
fh1 error
fh2 error
Output on Linux:
ret = 4
fh1 read
fh2 read
fh1 write
fh2 write
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment