Created
August 25, 2011 18:12
-
-
Save Benabik/1171339 to your computer and use it in GitHub Desktop.
Testing select on various platforms...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Output on OS X: | |
ret = 6 | |
fh1 read | |
fh2 read | |
fh1 write | |
fh2 write | |
fh1 error | |
fh2 error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Output on Linux: | |
ret = 4 | |
fh1 read | |
fh2 read | |
fh1 write | |
fh2 write |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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