【问题标题】:Inotify write visibility guaranteesInotify 写入可见性保证
【发布时间】:2020-10-05 09:10:47
【问题描述】:

我很困惑在使用inotify 时是否有一些订购保证。例如:

 Process1
    |
    |
write(fd, void *, 8192)
    |
    |
    v
some.file ------- IN_MODIFY -------> Process2

是否保证当Process2 收到IN_MODIFY 事件时,Process1 编写的所有8192 都可供Process2 读取?

【问题讨论】:

  • 为了更好地理解,最好有您阅读该订购保证的链接,或者甚至可以引用相关部分。
  • @Robert 实际上,这就是问题所在。是否有一些排序或者可以观察写入时的状态。
  • inotify 甚至不提供您将收到所有事件的实际保证。请参阅inotify missing eventsWhy is inotify losing events? 等,Per the inotify man page:“健壮的应用程序应该优雅地处理丢失事件的可能性。”文件系统不是消息队列——不要试图强迫它们成为。

标签: c linux io linux-kernel inotify


【解决方案1】:

inotify 子系统没有任何关于从 用户 角度订购的保证。请参阅内核代码中的文件 /fs/notify/notification.c。开场评论是这样说的:

通知队列背后的基本思想:一个 fsnotify 组(如 inotify) 一段时间后异步发送有关事件的用户空间通知 事件发生了。当 inotify 收到一个事件时,它需要添加该事件 事件到组通知队列。

在“另一端”,inotify 处理程序位于一个循环中,等待从队列中读取事件,请参见文件 fs/notify/inotify_user.c。正如你所看到的,只有一个保证,一个事件会在它发生后的某个时间被报告。但是,从内核的角度来看,顺序是保留的。

还有一点可以回答您的问题。如果您查看可以观看的事件列表,在 linux/inotify.h 中:

/* the following are legal, implemented events that user-space can watch for */
#define IN_ACCESS       0x00000001  /* File was accessed */
#define IN_MODIFY       0x00000002  /* File was modified */
#define IN_ATTRIB       0x00000004  /* Metadata changed */
#define IN_CLOSE_WRITE      0x00000008  /* Writtable file was closed */
#define IN_CLOSE_NOWRITE    0x00000010  /* Unwrittable file closed */
#define IN_OPEN         0x00000020  /* File was opened */
#define IN_MOVED_FROM       0x00000040  /* File was moved from X */
#define IN_MOVED_TO     0x00000080  /* File was moved to Y */
#define IN_CREATE       0x00000100  /* Subfile was created */
#define IN_DELETE       0x00000200  /* Subfile was deleted */
#define IN_DELETE_SELF      0x00000400  /* Self was deleted */
#define IN_MOVE_SELF        0x00000800  /* Self was moved */

/* the following are legal events.  they are sent as needed to any watch */
#define IN_UNMOUNT      0x00002000  /* Backing fs was unmounted */
#define IN_Q_OVERFLOW       0x00004000  /* Event queued overflowed */
#define IN_IGNORED      0x00008000  /* File was ignored */

你可以看到 IN_MODIFY 将在文件打开并开始写入后设置,而没有任何关于写入完成的信息;但 IN_CLOSE_WRITE 在文件描述符关闭后发生。因此,如果您担心写入相同文件的不同用户程序之间的竞争条件,您可以观察 IN_CLOSE_WRITE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2018-02-28
    • 2021-07-15
    • 1970-01-01
    • 2019-12-25
    相关资源
    最近更新 更多