是否保证 inotify 事件读取不会跨缓冲区拆分?
Are inotify event reads guaranteed not to be split across buffers?
inotify 的所有代码示例都执行如下操作:
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + NAME_MAX + 1))
char buf[BUF_LEN];
num = read(_fd, buf, BUF_LEN);
所以大概这允许单个 read()
调用 return 最多 1024 个事件。
如果实际上有比队列中更多的事件怎么办? inotify 会保证 return 事件的总数,即使这意味着短读吗?或者 inotify 会将事件拆分为两次读取?
来自 man
页面 inotify
:
The behavior when the buffer given to read(2) is too small to return
information about the next event depends on the kernel version: in
kernels before 2.6.21, read(2) returns 0; since kernel 2.6.21, read(2)
fails with the error EINVAL. Specifying a buffer of size
sizeof(struct inotify_event) + NAME_MAX + 1
will be sufficient to read at least one event.
所以历史上它四舍五入 向下 ,在这一点上,听起来它会失败 EINVAL
。
inotify 的所有代码示例都执行如下操作:
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + NAME_MAX + 1))
char buf[BUF_LEN];
num = read(_fd, buf, BUF_LEN);
所以大概这允许单个 read()
调用 return 最多 1024 个事件。
如果实际上有比队列中更多的事件怎么办? inotify 会保证 return 事件的总数,即使这意味着短读吗?或者 inotify 会将事件拆分为两次读取?
来自 man
页面 inotify
:
The behavior when the buffer given to read(2) is too small to return information about the next event depends on the kernel version: in kernels before 2.6.21, read(2) returns 0; since kernel 2.6.21, read(2) fails with the error EINVAL. Specifying a buffer of size
sizeof(struct inotify_event) + NAME_MAX + 1
will be sufficient to read at least one event.
所以历史上它四舍五入 向下 ,在这一点上,听起来它会失败 EINVAL
。