gpt4 book ai didi

c - select() 如何等待常规文件描述符(非套接字)?

转载 作者:太空狗 更新时间:2023-10-29 16:47:42 26 4
gpt4 key购买 nike

这是来自“man select”的代码示例加上几行以读取正在写入的实际文件。我怀疑当 ./myfile.txt 被写入时,select 会返回它现在可以从该 fd 读取。但是,只要 txt 文件存在,select 就会在 while 循环中不断返回。我希望它仅在将新数据写入文件末尾时返回。我认为它应该是这样工作的。

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int
main(void)
{
fd_set rfds;
struct timeval tv;
int retval;

int fd_file = open("/home/myfile.txt", O_RDONLY);

/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
FD_SET(fd_file, &rfds);

/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;

while (1)
{
retval = select(fd_file+1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */

if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\n");
}

exit(EXIT_SUCCESS);
}

最佳答案

磁盘文件随时可以读取(但如果您已经在文件末尾,读取可能会返回 0 字节),因此您不能使用 select()在磁盘文件上找出新数据何时添加到文件中。

POSIX 表示:

File descriptors associated with regular files shall always select true for ready to read, ready to write, and error conditions.

另外,作为 cnicutar在现已删除的帖子中指出,一般来说,您必须在每次迭代时初始化 FD_SET。在您的代码中,您正在监视一个 fd,并且该 fd 始终准备就绪,因此 FD_SET 实际上并未发生变化。但是,如果您有 5 个描述符要监视,并且 select 检测到只有一个准备就绪,那么在下一次迭代中,将只监视那个描述符(除非您重置 FD_SET)。这使得使用 select 变得棘手。

关于c - select() 如何等待常规文件描述符(非套接字)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11901884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com