2011-07-14 33 views
2

Sourcehelp inotify C腳本

我從來沒有用過C語言。此腳本將偵聽器添加到目錄中,並通過回顯到終端來通知用戶,然後在發生文件事件時退出。我想修改腳本不退出,而是繼續監視文件夾。我認爲最關鍵的可能是這一行:

length = read(fd, buffer, BUF_LEN);

,但我真的不明白是怎麼回事。該read()功能的描述可能是爲那些誰知道的C真的很好有所幫助:

Using inotify is simple: Create a file descriptor, attach one or more watches (a watch is a path and set of events), and use the read() method to receive event information from the descriptor. Rather than burn scarce cycles, read() blocks until events occur.

但我不屬於這一類。

回答

4

該程序存在的原因很簡單,因爲一旦它發生事件,沒有什麼能夠阻止它到達exit(0);。您可以將所有從fd = inotify_init();(void) close(fd);的所有內容都包裹在一個循環中,只要您想要,它就會重新開始。

問題不在length = read(fd, buffer, BUF_LEN);。該部分只是等待事件發生,並不要求程序退出。這確實是main被設計爲在一個run-through和exit中執行。

+0

沒錯。所以讓我困惑的是'while(i lollercoaster

+0

'read()'直接讀入'buffer'並返回讀取的字節數,它存儲在'length'中。我並不完全確定'while(i Vache

+2

這是正確的。 inotify可以排隊多個事件,然後'read()'將返回它們。 'while(i