Created
June 16, 2010 21:13
-
-
Save danielsdeleo/441278 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <sys/select.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
int main(int argc, char ** argv) { | |
char * dev_short = (argc > 1) ? argv[1] : "sda1"; | |
char dev[512]; | |
snprintf(dev, sizeof(dev), "/sys/block/%s/size", dev_short); | |
int fd = open(dev, O_RDONLY); | |
if (fd == -1) { | |
char msg[512]; | |
snprintf(msg, sizeof(msg), "open %s", dev); | |
perror(msg); | |
exit(1); | |
} | |
printf("fd = %d\n", fd); | |
fd_set fdr; | |
FD_ZERO(&fdr); | |
FD_SET(fd, &fdr); | |
struct timeval timestart, timeend; | |
struct timeval timeout; | |
timeout.tv_sec = 5L; | |
timeout.tv_usec = 0L; | |
gettimeofday(×tart, NULL); | |
int selret = select(fd + 1, &fdr, NULL, NULL, &timeout); | |
printf("selret = %d\n", selret); | |
printf("FD_ISSET(%d) is %d\n", fd, FD_ISSET(fd, &fdr)); | |
gettimeofday(&timeend, NULL); | |
struct timeval timediff; | |
timediff.tv_sec = timeend.tv_sec - timestart.tv_sec; | |
timediff.tv_usec = timeend.tv_usec - timestart.tv_usec; | |
if (timediff.tv_usec < 0L) { | |
timediff.tv_usec += 1000000L; | |
timediff.tv_sec -= 1; | |
} | |
printf("time elapsed = %ld.%06ld seconds\n", timediff.tv_sec, timediff.tv_usec); | |
} | |
/* | |
good: | |
fd = 3 | |
selret = 1 | |
FD_ISSET(3) is 1 | |
time elapsed = 0.000016 seconds | |
bad: | |
fd = 3 | |
selret = 0 | |
FD_ISSET(3) is 0 | |
time elapsed = 5.000043 seconds | |
*/ |
This file contains 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
f = File.open("/sys/block/sda/sda2/size") | |
# => #<File:/sys/block/sda/sda2/size> | |
IO.select([f],nil,nil,nil) |
This file contains 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 <stdio.h> | |
#include <sys/select.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
int main(int argc, char ** argv) { | |
char * dev_short = (argc > 1) ? argv[1] : "sda1"; | |
char dev[512]; | |
snprintf(dev, sizeof(dev), "/sys/block/%s/size", dev_short); | |
int fd = open(dev, O_RDONLY); | |
if (fd == -1) { | |
char msg[512]; | |
snprintf(msg, sizeof(msg), "open %s", dev); | |
perror(msg); | |
exit(1); | |
} | |
printf("fd = %d\n", fd); | |
fd_set fdr; | |
FD_ZERO(&fdr); | |
FD_SET(fd, &fdr); | |
struct timeval timestart, timeend; | |
struct timeval timeout; | |
timeout.tv_sec = 5L; | |
timeout.tv_usec = 0L; | |
gettimeofday(×tart, NULL); | |
int selret = select(fd + 1, &fdr, NULL, NULL, &timeout); | |
printf("selret = %d\n", selret); | |
printf("FD_ISSET(%d) is %d\n", fd, FD_ISSET(fd, &fdr)); | |
gettimeofday(&timeend, NULL); | |
struct timeval timediff; | |
timediff.tv_sec = timeend.tv_sec - timestart.tv_sec; | |
timediff.tv_usec = timeend.tv_usec - timestart.tv_usec; | |
if (timediff.tv_usec < 0L) { | |
timediff.tv_usec += 1000000L; | |
timediff.tv_sec -= 1; | |
} | |
printf("time elapsed = %ld.%06ld seconds\n", timediff.tv_sec, timediff.tv_usec); | |
if (selret == 0 || !FD_ISSET(fd, &fdr)) { | |
fprintf(stderr, "Your kernel /sys select is borked. selret should == 1 and FD_ISSET(%d, &fdr) should be 1!\n", fd); | |
} | |
} | |
/* | |
BAD: | |
[root@localhost tmp]# ./fuuuuuuuu sda/sda2 | |
fd = 3 | |
selret = 0 | |
FD_ISSET(3) is 0 | |
time elapsed = 5.001028 seconds | |
Your kernel /sys select is borked. selret should == 1 and FD_ISSET(3, &fdr) should be 1! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment