日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

詳解Linux 2.6內(nèi)核新文件系統(tǒng)變化機(jī)制( 二 )


下面函數(shù)用于添加一個(gè) watch:

int wd = inotify_add_watch (fd, path, mask);

fd 是 inotify_init() 返回的文件描述符,path 是被監(jiān)視的目標(biāo)的路徑名(即文件名或目錄名),mask 是事件掩碼, 在頭文件 linux/inotify.h 中定義了每一位代表的事件 ??梢允褂猛瑯拥姆绞絹硇薷氖录诖a,即改變希望被通知的inotify 事件 。Wd 是 watch 描述符 。
下面的函數(shù)用于刪除一個(gè) watch:

int ret = inotify_rm_watch (fd, wd);

fd 是 inotify_init() 返回的文件描述符,wd 是 inotify_add_watch() 返回的 watch 描述符 。Ret 是函數(shù)的返回值 。
文件事件用一個(gè) inotify_event 結(jié)構(gòu)表示,它通過由 inotify_init() 返回的文件描述符使用通常文件讀取函數(shù) read 來獲得:

struct inotify_event {
__s32wd;;;/* watch descriptor */
__u32mask;/* watch mask */
__u32cookie; /* cookie to synchronize two events */
__u32len;;/* length (including nulls) of name */
char;name[0] /* stub for possible name */
};

結(jié)構(gòu)中的 wd 為被監(jiān)視目標(biāo)的 watch 描述符,mask 為事件掩碼,len 為 name字符串的長(zhǎng)度,name 為被監(jiān)視目標(biāo)的路徑名,該結(jié)構(gòu)的 name 字段為一個(gè)樁,它只是為了用戶方面引用文件名,文件名是變長(zhǎng)的,它實(shí)際緊跟在該結(jié)構(gòu)的后面,文件名將被 0 填充以使下一個(gè)事件結(jié)構(gòu)能夠 4 字節(jié)對(duì)齊 。注意,len 也把填充字節(jié)數(shù)統(tǒng)計(jì)在內(nèi) 。
通過 read 調(diào)用可以一次獲得多個(gè)事件,只要提供的 buf 足夠大 。

size_t len = read (fd, buf, BUF_LEN);

buf 是一個(gè) inotify_event 結(jié)構(gòu)的數(shù)組指針,BUF_LEN 指定要讀取的總長(zhǎng)度,buf 大小至少要不小于 BUF_LEN,該調(diào)用返回的事件數(shù)取決于 BUF_LEN 以及事件中文件名的長(zhǎng)度 。Len 為實(shí)際讀去的字節(jié)數(shù),即獲得的事件的總長(zhǎng)度 。
可以在函數(shù) inotify_init() 返回的文件描述符 fd 上使用 select() 或poll(), 也可以在 fd 上使用 ioctl 命令 FIONREAD 來得到當(dāng)前隊(duì)列的長(zhǎng)度 。close(fd)將刪除所有添加到 fd 中的 watch 并做必要的清理 。

int inotify_init (void);
int inotify_add_watch (int fd, const char *path, __u32 mask);
int inotify_rm_watch (int fd, __u32 mask);

三、內(nèi)核實(shí)現(xiàn)機(jī)理
在內(nèi)核中,每一個(gè) inotify 實(shí)例對(duì)應(yīng)一個(gè) inotify_device 結(jié)構(gòu):

struct inotify_device {
wait_queue_head_t;;;;wq;;;/* wait queue for i/o */
struct idr;;;idr;;/* idr mapping wd -> watch */
struct semaphore;;;;;sem;;/* protects this bad boy */
struct list_head;;;;;events; /* list of queued events */
struct list_head;;;;;watches /* list of watches */
atomic_t;;;;;count/* reference count */
struct user_struct;;;*user/* user who opened this dev */
unsigned int;queue_size;;;/* size of the queue (bytes) */
unsigned int;event_count;;/* number of pending events */
unsigned int;max_events;;;/* maximum number of events */
u32;;last_wd /* the last wd allocated */
};

wq 是等待隊(duì)列,被 read 調(diào)用阻塞的進(jìn)程將掛在該等待隊(duì)列上,idr 用于把 watch 描述符映射到對(duì)應(yīng)的 inotify_watch,sem 用于同步對(duì)該結(jié)構(gòu)的訪問,events 為該 inotify 實(shí)例上發(fā)生的事件的列表,被該 inotify 實(shí)例監(jiān)視的所有事件在發(fā)生后都將插入到這個(gè)列表,watches 是給 inotify 實(shí)例監(jiān)視的 watch 列表,inotify_add_watch 將把新添加的 watch 插入到該列表,count 是引用計(jì)數(shù),user 用于描述創(chuàng)建該 inotify 實(shí)例的用戶,queue_size 表示該 inotify 實(shí)例的事件隊(duì)列的字節(jié)數(shù),event_count 是 events 列表的事件數(shù),max_events 為最大允許的事件數(shù),last_wd 是上次分配的 watch 描述符 。
每一個(gè) watch 對(duì)應(yīng)一個(gè) inotify_watch 結(jié)構(gòu):

struct inotify_watch {
struct list_head;;;;;d_list; /* entry in inotify_device"s list */
struct list_head;;;;;i_list; /* entry in inode"s list */

推薦閱讀