【问题标题】:Why is it required to call read() twice with inotify_add_watch()为什么需要使用 inotify_add_watch() 调用 read() 两次
【发布时间】:2018-08-17 20:45:34
【问题描述】:

我正在尝试在文件被修改时使用 inotify_add-watch() 获取通知 (inotify_add_watch (fd, filename.c_str(), IN_MODIFY);) 在 linux 文件系统上(linux 内核 4.9.0)。

但在收到通知后,预计 read() 会调用两次,直到我收到有关文件 /etc/temp 的下一次修改的通知。有人可以澄清为什么我需要两次调用 read() 吗?谢谢。

int fd, wd;
fd = inotify_init ();

if (fd < 0)
{
    perror ("inotify_init () = ");
}
else
{
    std::string filename = "/etc/test";
    wd = inotify_add_watch (fd, filename.c_str(), IN_MODIFY);

    if (wd < 0)
    {
    perror ("inotify_add_watch");
    }
    else
    {
        char* buffer = new char[1024];
        while(true)
        {
            //first read blocks until the /etc/temp file is modified, 
            //it returns 16 which is sizeof(struct inotify_event)
            printf("first read %d), read( fd, buffer, 1024));

            //second read() does not block and read returns 16 again
            printf("second read %d), read( fd, buffer, 1024));
         }
     }

}

【问题讨论】:

    标签: linux inotify


    【解决方案1】:

    您必须在它再次开始阻塞之前消耗所有待处理的事件。

    当你例如做echo foo &gt; /etc/test,你可能会得到两个事件:一个用于截断,一个用于写入。

    如果两个都不吃,下一个会立即返回。

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2015-04-03
      • 2020-05-19
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多