<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">
	<id>https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=Inotify</id>
	<title>Inotify - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=Inotify"/>
	<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=Inotify&amp;action=history"/>
	<updated>2026-07-01T12:05:55Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://arolstar52-zhtest.hf.space/index.php?title=Inotify&amp;diff=909382&amp;oldid=prev</id>
		<title>imported&gt;SchlurcherBot：​Bot: http → https</title>
		<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=Inotify&amp;diff=909382&amp;oldid=prev"/>
		<updated>2026-01-27T14:56:06Z</updated>

		<summary type="html">&lt;p&gt;Bot: http → https&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{lowercase}}&lt;br /&gt;
{{NoteTA&lt;br /&gt;
| G1=IT&lt;br /&gt;
| 1=zh-cn:掩码; zh-tw:遮罩;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Proofreader needed|en|inotify}}&lt;br /&gt;
{{expert-subject}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;inotify&amp;#039;&amp;#039;&amp;#039;是[[Linux核心]]子系統之一，做為[[檔案系統]]的附加功能，它可監控檔案系統並將異動通知應用程式。本系統的出現取代了舊有Linux核心裡，擁有類似功能之[[dnotify]]模組。&lt;br /&gt;
&lt;br /&gt;
inotify的原始開發者為[[John McCutchan]]、[[羅伯特·拉姆]]與[[Amy Griffis]]。於Linux核心2.6.13發行時(2005年六月十八日)，被正式納入Linux核心&amp;lt;ref&amp;gt;{{Cite web |url=https://kernelnewbies.org/Linux_2_6_13 |title=Linux 2.6.13, kernelnewbies.org |accessdate=2012-03-06 |archive-date=2020-10-19 |archive-url=https://web.archive.org/web/20201019213140/https://kernelnewbies.org/Linux_2_6_13 |dead-url=no }}&amp;lt;/ref&amp;gt;。儘管如此，它仍可透過[[修補程式]]的方式與2.6.12甚至更早期的Linux核心整合。&lt;br /&gt;
&lt;br /&gt;
inotify的主要應用於[[桌面搜索]]軟體，像：[[Beagle]]，得以針對有變動的檔案重新[[索引]]，而不必沒有效率地每隔幾分鐘就要掃描整個檔案系統。相較於主動輪詢檔案系統，透過作業系統主動告知檔案異動的方式，讓Beagle等軟體甚至可以在檔案更動後一秒內更新索引。&lt;br /&gt;
&lt;br /&gt;
此外，諸如：更新目錄檢視、重新載入設定檔、追蹤變更、備份、同步甚至上傳等許多自動化作業流程，都可因而受惠。&lt;br /&gt;
&lt;br /&gt;
== 優點 ==&lt;br /&gt;
相較於被inotify取代較舊的 [[dnotify]]模組，inotify有諸多益處。&amp;lt;ref&amp;gt;{{Cite web |url=http://www.developertutorials.com/tutorials/linux/monitor-linux-inotify-050531/page2.html |title=Why inotify? |accessdate=2012-03-06 |archive-date=2010-01-16 |archive-url=https://web.archive.org/web/20100116105916/http://www.developertutorials.com/tutorials/linux/monitor-linux-inotify-050531/page2.html |dead-url=no }}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://www.kernel.org/pub/linux/kernel/people/rml/inotify/README |title=&amp;#039;&amp;#039;inotify&amp;#039;&amp;#039; README file |accessdate=2012-03-06 |archive-date=2016-12-29 |archive-url=https://web.archive.org/web/20161229103708/https://www.kernel.org/pub/linux/kernel/people/rml/inotify/README |dead-url=no }}&amp;lt;/ref&amp;gt;在舊的模組中，程式必須為每一個被監控的目錄建立[[文件描述符|file descriptor]]，這種作法很容易讓行程擁有的file descriptor逼近系統允許的上限，進而形成瓶頸。dnotify產生的file decriptor也會導致系統資源忙碌，使可移除裝置無法被[[umount|移除]]，徒增使用上的困擾。&lt;br /&gt;
&lt;br /&gt;
由於dnotify只能讓程式設計師監控目錄層級的變化，「精細度」亦是“dnotify”的劣勢之一。為此，程式設計師必須付出額外的心力，自行撰寫程式碼以期追蹤更細微的檔案系統事件。&lt;br /&gt;
&lt;br /&gt;
inotify相較之下使用較少的file descriptor，亦允許select()與poll()介面，優於[[dnotify]]使用的[[signal notification system|信號系統]]。這也使得inotify與既有以select()或poll()為基礎之函式庫(如：[[Glib]])整合更加便利。&lt;br /&gt;
&lt;br /&gt;
== 運作方式 ==&lt;br /&gt;
&lt;br /&gt;
inotify擁有專為其設計的系統函式。十分容易上手。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/inotify.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
要使用inotify必須先引用上面的標頭檔。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 int inotify_init(void)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
建立一個inotify的實體並回傳一個file descriptor，此檔案描述子可供讀取檔案事件。隨後，可透過read()接收事件，為了避免不斷輪詢檔案，read()預設將採用同步I/O的模式，直到事件發生後才會返回。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 int inotify_add_watch(int fd, const char* pathname, int mask)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
透過路徑名稱(pathname)並選定遮罩(mask)以監控[[inode]]。inotify_add_watch()會回傳一個監控器（watch descriptor），它代表pathname指向的inode(不同的pathname有可能指向相同的inode)。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 int inotify_rm_watch(int fd, int wd)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
取消對某個路徑之監控。&lt;br /&gt;
&lt;br /&gt;
如同之前所描述的，當檔案系統異動時，核心將會依據程式設定的條件，觸發相應的事件。事件的結構如下：&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 欄位名稱&lt;br /&gt;
! 內容描述&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;wd&amp;lt;/tt&amp;gt;&lt;br /&gt;
| 監控子 &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;mask&amp;lt;/tt&amp;gt;&lt;br /&gt;
| 事件遮罩&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;cookie&amp;lt;/tt&amp;gt;&lt;br /&gt;
| 用來辨別&amp;lt;tt&amp;gt;IN_MOVED_FROM&amp;lt;/tt&amp;gt;與&amp;lt;tt&amp;gt;IN_MOVED_TO&amp;lt;/tt&amp;gt;事件&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;len&amp;lt;/tt&amp;gt;&lt;br /&gt;
| name欄位長度&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt;&lt;br /&gt;
| 觸發該事件的檔案名稱(以上層目錄為基準)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
可供應用程式追蹤的事件有:&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_ACCESS&amp;lt;/tt&amp;gt; - 讀取檔案&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_MODIFY&amp;lt;/tt&amp;gt; - 檔案被修改&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_ATTRIB&amp;lt;/tt&amp;gt; - 檔案屬性變更&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_OPEN&amp;lt;/tt&amp;gt; - 檔案被開啟&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_CLOSE_WRITE&amp;lt;/tt&amp;gt; - 被開啟為「可寫入」狀態的檔案遭關閉&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_CLOSE_NOWRITE&amp;lt;/tt&amp;gt; - 被開啟為「非寫入」狀態的檔案遭關閉&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_MOVED_FROM&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;IN_MOVED_TO&amp;lt;/tt&amp;gt; - 檔案被搬動或更名&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_DELETE&amp;lt;/tt&amp;gt; - 檔案或目錄被刪除&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_CREATE&amp;lt;/tt&amp;gt; - 監控中的目錄下有新檔案產生&lt;br /&gt;
* &amp;lt;tt&amp;gt;IN_DELETE_SELF&amp;lt;/tt&amp;gt; - 監控中的檔案遭刪除&lt;br /&gt;
&lt;br /&gt;
== 缺點 ==&lt;br /&gt;
inotify無法監控软链接型的子目錄。&lt;br /&gt;
&lt;br /&gt;
== 歷史沿革 ==&lt;br /&gt;
* August 2005 - 整合至Linux 2.6.13&lt;br /&gt;
* July 2004 - [http://groups.google.com/group/fa.linux.kernel/browse_thread/thread/6366aaa10cb23bcc/a54e97d545ad66fe 首次發表] {{Wayback|url=http://groups.google.com/group/fa.linux.kernel/browse_thread/thread/6366aaa10cb23bcc/a54e97d545ad66fe |date=20121104100935 }}&lt;br /&gt;
&lt;br /&gt;
== 相關條目 ==&lt;br /&gt;
* [[File alteration monitor]]&lt;br /&gt;
* [[Gamin]]&lt;br /&gt;
* [[DMAPI]]&lt;br /&gt;
* [[kqueue]]&lt;br /&gt;
* [[FSEvents]]&lt;br /&gt;
&lt;br /&gt;
==參考資料==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
== 外部連結 ==&lt;br /&gt;
* [https://www.linuxjournal.com/article/8478 Kernel Korner ] {{Wayback|url=https://www.linuxjournal.com/article/8478 |date=20200921161518 }}  - Intro to inotify by Robert Love (2005)&lt;br /&gt;
* [https://lwn.net/Articles/104343/ LWN Article on Inotify] {{Wayback|url=https://lwn.net/Articles/104343/ |date=20200401141326 }} Watching filesystem events with inotify (partly out of date)&lt;br /&gt;
* [https://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html?ca=drs- IBM Article] {{Wayback|url=https://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html?ca=drs- |date=20170310174633 }} Monitor Linux file system events with inotify.&lt;br /&gt;
&lt;br /&gt;
[[Category:Linux内核功能]]&lt;br /&gt;
[[Category:操作系统技术]]&lt;/div&gt;</summary>
		<author><name>imported&gt;SchlurcherBot</name></author>
	</entry>
</feed>