在网络共享文件系统 (NFS) 上生成了许多文件。有一个类似的问题没有适当的解决方案:inotify with NFS。
我使用 select() 来测试文件是否可以读取新数据。(其实有些是来自socket描述符,这里只是简化了)。
但是,我发现即使文件直到文件结束,它仍然返回准备读取状态。
你能建议更好的方法来编写这段代码吗?
fd_set rfds;
struct timeval tv;
int retval;
int i,n,f1,f2,maxfd;
char buf[512];
f1 = fileno(fopen("f1", "rb"));
f2 = fileno(fopen("f2", "rb"));
maxfd = (f1 > f2) ? f1 : f2;
for (i=0; i<3; i++) {
FD_ZERO(&rfds);
FD_SET(f1, &rfds);
FD_SET(f2, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
if (retval == -1)
perror("select()");
else if (retval) {
printf("Data is available now.\n");
if (FD_ISSET(f1, &rfds)) {
n = read(f1, buf, sizeof(buf));
printf("f1 is ready:%d read %d bytes\n", i, n);
}
if (FD_ISSET(f2, &rfds)) {
n = read(f2, buf, sizeof(buf));
printf("f2 is ready:%d read %d bytes\n", i, n);
}
} else
printf("No data within five seconds.\n");
}
如果我的 f1 和 f2 包含 3 个字节,输出将如下所示。
Data is available now.
f1 is ready:0 read 3 bytes
f2 is ready:0 read 3 bytes
Data is available now.
f1 is ready:1 read 0 bytes <- I wish won't enter here
f2 is ready:1 read 0 bytes <- I wish won't enter here
Data is available now.
f1 is ready:2 read 0 bytes <- I wish won't enter here
f2 is ready:2 read 0 bytes <- I wish won't enter here