我希望在Linux C中观察文件fd是否可读,而不是EOF。现在我必须循环直到!eof,我认为这浪费了CPU时间。
static int rs_eof_read_binlog(rs_master_info_t *m, void *buf,
size_t size) {
FILE *fp;
size_t n;
fp = m->bfp;
for( ;; ) {
n = fread(buf, size, 1, fp);
if(n * size < size) {
if(ferror(fp) != 0) {
return RS_ERR;
}
if(feof(fp) != 0) {
fseek(fp, -n * size, SEEK_CUR);
sleep(1);
}
}
else {
break;
}
}
clearerr(fp);
return RS_OK;
我检查了select,当文件结束时,select会返回。
Those listed in readfds will be watched to see if characters become avail‐
able for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file)
已修复! 使用inotify的示例代码
int main(int argc, char *argv[])
{
char p, buf[1024];
int fd = open("/home/terry/1.txt", O_RDONLY, "00666");
struct inotify_event * event;
size_t len;
read(fd, &p, 1);
int ifd = inotify_init();
int wd = inotify_add_watch(ifd, "/home/terry/1.txt", IN_MODIFY);
while(len = read(ifd, buf, 1024)) {
event = (struct inotify_event *) buf;
if(event->mask & IN_MODIFY) {
printf("changed\n");
break;
}
}
return 0;
}
select
,并且没有任何地方传递fd_set,您认为什么使您认为man select
中阅读的任何内容都是适用的呢? - cHaoselect
- 他想知道何时有数据,但select
会在EOF时唤醒他。 - ugorenselect
在文件上是否能实现他想要的功能——它更适用于管道和套接字。 - cHao