select()でファイル記述子を監視しようとしているのですが、
ファイルによって(?)、select()で止まらないことがあります。
/dev/input/mouse0は、selectで正常に監視できますが
./a.txtは、selectで監視しようとすると、selectで止まってくれません。
以下にコードを示します。
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> int main() { int fd; // ファイル記述子 struct timeval timeout; // タイムアウト用の構造体 fd_set readfds; // 監視するファイル記述子の集合 int maxfd; // selectに必要 int n; // 変化があったファイル記述子の数 //char *filename = "/dev/input/mouse0"; // 正常にselectが動作する char *filename = "./a.txt"; // selectで止まらず、無限ループしてしまう if((fd = open(filename, O_RDONLY)) < 0){ perror("open"); return 1; } maxfd = fd + 1; while(1){ FD_ZERO(&readfds); FD_SET(fd, &readfds); timeout.tv_sec = 3; timeout.tv_usec = 0; n = select(maxfd, &readfds, NULL, NULL, &timeout); // エラー if(n == -1){ perror("select"); return 1; } // データ到着 if(n > 0){ if(FD_ISSET(fd, &readfds)){ char buf[BUFSIZ]; int n; printf("%s ", filename); fflush(stdout); n = read(fd, buf, BUFSIZ); if ( n == -1 ) perror("isset"); write(1, buf, n); write(1, "\n", 1); } } // タイムアウト else{ printf("time out\n"); } } return 0; }
もし、ご存じの方がおりましたら、教えて頂けると幸いです。
回答1件
あなたの回答
tips
プレビュー