探讨mutex与semaphore
生活随笔
收集整理的這篇文章主要介紹了
探讨mutex与semaphore
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看過Linux內核的同學都知道,Linux內核中除了有semaphore之外,還有一個mutex lock。前者我們的操作系統教科書稱之為信號量,后者不知道教科書有沒有具體的名稱,但是在Linux內核中,它的稱謂是"互斥鎖"或者“互斥體”(總之,稱謂不是問題)。為了提升一下本帖的理論密度,特從Wiki中摘錄一段關于semaphore的描述:
“ In computer science, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment.
A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e., without race conditions) adjust that record as units are required or become free, and if necessary wait until a unit of the resource becomes available. Semaphores are a useful tool in the prevention of race conditions and deadlocks; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores (same functionality that mutexes have). ”。?
其中關鍵信息主要是“a semaphore is a data type for controlling access by multiple processes to a common resource in a parallel programming environment... Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores (same functionality that mutexes have)”,也即信號量在并行處理環境下對多個processes訪問某個公共資源進行保護,后面提到的binary semaphore,本質上應該就是mutex了,從same functionality that mutexes have這句話來看,mutex和binary semaphore功能應該相同。從以上的文字中顯然可以看到,相對mutex而言信號量的適用范圍更廣(mutex只是信號量的用途之一),這個我們接下來在后續的Linux源碼中也可以看到這其中某些細微之處的區分。
*注:昨天寫這個帖子時手頭沒有操作系統方面的書籍拿來參考,今天我翻了一下《現代操作系統》(陳向群等譯,機械工業出版社??1999年11月第1版), 關于這個話題,書里明確提到的只有"2.2.5 信號量",至于mutex,書中并沒有作為一個獨立的概念提出來,只是在講信號量時提到了上面所說的binary semaphore,并且說“信號量mutex(應該是指binary semaphore)用于互斥...互斥是避免混亂所必需的操作...信號量的另一種用途是用于實現同步(synchronization)。信號量full和empty用來保證一定的事件順序發生或不發生。在本例中,它們保證緩沖區滿的時候生產者停止運行,或者當緩沖區空的時候消費者停止運行。這種用法與互斥是不同的” --- P30-31 *
OK,理論上的概念有了,那么就來看看實際當中Linux下的semaphone到底長的啥樣。以下是semaphore在Linux源碼中的定義,源碼來自3.2.9:
<include/linux/semaphore.h> /* Please don't access any members of this structure directly */
struct semaphore {
? ? ? ? raw_spinlock_t? ? ? ? ? ? ? ? lock;
? ? ? ? unsigned int? ? ? ? ? ? ? ? count;
? ? ? ? struct list_head? ? ? ? wait_list;
}; 復制代碼
如果count=1的話,那么semaphore就可以用來進行互斥操作了,早先內核源碼中曾有一個專門的宏用來定義一個count=1的信號量DECLARE_MUTEX:
#define DECLARE_MUTEX(name) \
? ?? ?? ?? ?struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1) 復制代碼
因為我們知道在Linux內核源碼中還有一個DEFINE_MUTEX宏,所以Marcin Slusarz同學認為DECLARE_MUTEX宏容易讓人困惑,畢竟它其實只是定義了一個count=1的信號量,因此該同學早在08年就向內核社區提交了一個PATCH,要求Rename DECLARE_MUTEX to DEFINE_SEMAPHORE(
https://lkml.org/lkml/2008/10/26/74
),這個PATCH最終被社區所接受(應該是在2.6.35和2.6.39版本之間,2.6.39已經沒有了DECLARE_MUTEX,取而代之的是DEFINE_SEMAPHORE,但2.6.35還有,我當時在寫《深入Linux設備驅動程序內核機制》時,最早引用的是2.6.35的版本,雖然在寫作的中晚期將內核版本切換到2.6.39并在定稿階段曾試圖將之前寫的文檔全部修正到39版,但是DECLARE_MUTEX的殘留顯然是條漏網之魚...)。因為只是rename,所以DEFINE_SEMAPHORE的定義和原來的DECLARE_MUTEX完全相同。
那么既然count=1的信號量可以用來完成mutex lock的功能,那么內核何必再多此一舉弄出個mutex lock出來呢??
“ In computer science, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment.
A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e., without race conditions) adjust that record as units are required or become free, and if necessary wait until a unit of the resource becomes available. Semaphores are a useful tool in the prevention of race conditions and deadlocks; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores (same functionality that mutexes have). ”。?
其中關鍵信息主要是“a semaphore is a data type for controlling access by multiple processes to a common resource in a parallel programming environment... Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores (same functionality that mutexes have)”,也即信號量在并行處理環境下對多個processes訪問某個公共資源進行保護,后面提到的binary semaphore,本質上應該就是mutex了,從same functionality that mutexes have這句話來看,mutex和binary semaphore功能應該相同。從以上的文字中顯然可以看到,相對mutex而言信號量的適用范圍更廣(mutex只是信號量的用途之一),這個我們接下來在后續的Linux源碼中也可以看到這其中某些細微之處的區分。
*注:昨天寫這個帖子時手頭沒有操作系統方面的書籍拿來參考,今天我翻了一下《現代操作系統》(陳向群等譯,機械工業出版社??1999年11月第1版), 關于這個話題,書里明確提到的只有"2.2.5 信號量",至于mutex,書中并沒有作為一個獨立的概念提出來,只是在講信號量時提到了上面所說的binary semaphore,并且說“信號量mutex(應該是指binary semaphore)用于互斥...互斥是避免混亂所必需的操作...信號量的另一種用途是用于實現同步(synchronization)。信號量full和empty用來保證一定的事件順序發生或不發生。在本例中,它們保證緩沖區滿的時候生產者停止運行,或者當緩沖區空的時候消費者停止運行。這種用法與互斥是不同的” --- P30-31 *
OK,理論上的概念有了,那么就來看看實際當中Linux下的semaphone到底長的啥樣。以下是semaphore在Linux源碼中的定義,源碼來自3.2.9:
<include/linux/semaphore.h>
那么既然count=1的信號量可以用來完成mutex lock的功能,那么內核何必再多此一舉弄出個mutex lock出來呢??
總結
以上是生活随笔為你收集整理的探讨mutex与semaphore的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于linux-2.6.35的class
- 下一篇: 函数注释参考