inotify 中, 在 read 其 struct inotify_event 时, 缓冲区必须大于读取的结构体的大小
2012-09-14 16:07
579 查看
linux中监控文件的 inotify 提供了以下几个编程函数, inotify_init(), inotify_add_watch(),
inotify_rm_watch(). 从 inotify_init() 返回的文件描述符 read, 读取监听到的事件.
事件的结构为:
假设 EVENT_LEN = sizeof(sturct inotify_event), NAME_LEN = len (事件结构体中的len, 为保持内存对齐, 可能大于 strlen(name));
假设inotify的事件队列中有很多未读取的, 读取事件 ret = read(fd, buf, BUF_LEN);
如果 BUF_LEN < EVENT_LEN + NAME_LEN, ret = 0;
如果 EVENT_LEN + NAME_LEN <= BUF_LEN < 2 * (EVENT_LEN + NAME_LEN ), ret = EVENT_LEN + NAME_LEN;
如果 2 * (EVENT_LEN + NAME_LEN) <= BUF_LEN < 3* (EVENT_LEN + NAME_LEN _, ret = 2 * (EVENT_LEN + NAME_LEN );
......
也就是, 只能整个的读取 struct inotify_event, 不能读取部分结构.
- - - - - - - -
以上是我个人的测试.
inotify_rm_watch(). 从 inotify_init() 返回的文件描述符 read, 读取监听到的事件.
事件的结构为:
struct inotify_event { int wd; /* Watch descriptor */ uint32_t mask; /* Mask of events */ uint32_t cookie; /* Unique cookie associating related events (for rename(2)) */ uint32_t len; /* Size of ’name’ field */ char name[]; /* Optional null-terminated name */ };因为每个文件名的长度不定, 所以每个事件占用的内存大小也不一定相等.
假设 EVENT_LEN = sizeof(sturct inotify_event), NAME_LEN = len (事件结构体中的len, 为保持内存对齐, 可能大于 strlen(name));
假设inotify的事件队列中有很多未读取的, 读取事件 ret = read(fd, buf, BUF_LEN);
如果 BUF_LEN < EVENT_LEN + NAME_LEN, ret = 0;
如果 EVENT_LEN + NAME_LEN <= BUF_LEN < 2 * (EVENT_LEN + NAME_LEN ), ret = EVENT_LEN + NAME_LEN;
如果 2 * (EVENT_LEN + NAME_LEN) <= BUF_LEN < 3* (EVENT_LEN + NAME_LEN _, ret = 2 * (EVENT_LEN + NAME_LEN );
......
也就是, 只能整个的读取 struct inotify_event, 不能读取部分结构.
- - - - - - - -
以上是我个人的测试.
相关文章推荐
- struct input_event结构体定义
- 定义一个学生结构体类型struct student,里边含有学号,姓名,分数,定义结构体数组struct student s[3],给数组赋初值后, 写入文件,然后通过lseek偏移到开头,然后再读取
- C语言之struct大小、首地址与内存对齐—由结构体成员地址得到结构体首地址
- libevent源码分析(2)--2.1.8--结构体 struct event和struct event_callback
- libevent基础文件event-internal.h(struct event_base结构体)
- libevent源码分析(3)--2.1.8--结构体struct event_base和struct eventop
- Windows socket通信出现接收数据时出现recv返回大于0,但缓冲区为大小为0的问题
- libevent源码分析(4)--2.1.8--结构体struct event_config
- 实例讲解c语言结构体大小 sizeof(struct A)
- 索引(从零开始)必须大于或等于零,且小于参数列表的大小的错位问题
- struct结构体占内存大小计算
- 索引(从零开始)必须大于或等于零,且小于参数列表的大小。
- IO流三:字节流文件读写操作、拷贝图片、带缓冲区的字节流、read()原理、读取键盘录入
- 程序集(CLR)调用web service出现"在执行用户定义历城或聚合期间出现.net framework错误,索引(从零开始必须大于或等于零,且小于参数列表的大小)"
- sizeof(struct{bit-field})——包含位域的结构体的空间大小
- 缓冲区大小对read,write系统调用效率的影响
- struct结构体的大小计算:
- libevent基础文件changelist-internal.h(struct event_change结构体)
- FileInputStream.read(byte[] b),数组长度大于读取内容字节数长度
- struct结构体占内存大小计算