简体   繁体   English

Inotify 写入可见性保证

[英]Inotify write visibility guarantees

I'm confused if there are some ordering guarantees when working with inotify .如果在使用inotify时有一些订购保证,我会感到困惑。 For example:例如:

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

Is it guaranteed that when Process2 receives IN_MODIFY event all 8192 written by Process1 will be available for reading by the Process2 ?是否保证当Process2收到IN_MODIFY事件时, Process1写入的所有8192都可供Process2读取?

The inotify subsystem does not have any gaurantees about ordering from a users perspective.用户的角度来看, inotify子系统没有任何关于订购的保证。 See the file /fs/notify/notification.c in the kernel code.请参阅 kernel 代码中的文件 /fs/notify/notification.c。 The opening comment says this:开场评论是这样说的:

Basic idea behind the notification queue: An fsnotify group (like inotify) sends the userspace notification about events asynchronously some time after the event happened.通知队列背后的基本思想:fsnotify 组(如 inotify)在事件发生一段时间后异步发送用户空间通知。 When inotify gets an event it will need to add that event to the group notify queue.当 inotify 收到一个事件时,它需要将该事件添加到组通知队列中。

At the "other end", the inotify handler sits in a loop waiting to read events off the queue, see the file fs/notify/inotify_user.c.在“另一端”,inotify 处理程序位于一个循环中,等待从队列中读取事件,请参见文件 fs/notify/inotify_user.c。 As you can see, there is only a gaurantee that an event will be reported some time after it happens.正如你所看到的,只有一个保证,一个事件会在它发生后的某个时间被报告。 However, from the kernels point of view, ordering is preserved.但是,从内核的角度来看,顺序是保留的。

There is another point that should answer up your question though.还有一点应该回答你的问题。 If you look at the list of events that can be watched, in linux/inotify.h:如果您查看可以观看的事件列表,在 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 */

You can see that IN_MODIFY will be set after a file was opened and writing was started without anything about writing being finished;您可以看到 IN_MODIFY 将在文件打开并开始写入后设置,而没有任何关于写入完成的信息; but IN_CLOSE_WRITE happens after the file descriptor was closed.但 IN_CLOSE_WRITE 在文件描述符关闭后发生。 So, if you're worried about race conditions between different user programs writing to the same files you can watch for IN_CLOSE_WRITE.因此,如果您担心写入相同文件的不同用户程序之间的竞争条件,您可以查看 IN_CLOSE_WRITE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM