日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

探讨mutex与semaphore

發(fā)布時(shí)間:2024/9/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 探讨mutex与semaphore 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
看過Linux內(nèi)核的同學(xué)都知道,Linux內(nèi)核中除了有semaphore之外,還有一個(gè)mutex lock。前者我們的操作系統(tǒng)教科書稱之為信號(hào)量,后者不知道教科書有沒有具體的名稱,但是在Linux內(nèi)核中,它的稱謂是"互斥鎖"或者“互斥體”(總之,稱謂不是問題)。為了提升一下本帖的理論密度,特從Wiki中摘錄一段關(guān)于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).
”。?

其中關(guān)鍵信息主要是“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)”,也即信號(hào)量在并行處理環(huán)境下對多個(gè)processes訪問某個(gè)公共資源進(jìn)行保護(hù),后面提到的binary semaphore,本質(zhì)上應(yīng)該就是mutex了,從same functionality that mutexes have這句話來看,mutex和binary semaphore功能應(yīng)該相同。從以上的文字中顯然可以看到,相對mutex而言信號(hào)量的適用范圍更廣(mutex只是信號(hào)量的用途之一),這個(gè)我們接下來在后續(xù)的Linux源碼中也可以看到這其中某些細(xì)微之處的區(qū)分。

*注:昨天寫這個(gè)帖子時(shí)手頭沒有操作系統(tǒng)方面的書籍拿來參考,今天我翻了一下《現(xiàn)代操作系統(tǒng)》(陳向群等譯,機(jī)械工業(yè)出版社??1999年11月第1版), 關(guān)于這個(gè)話題,書里明確提到的只有"2.2.5 信號(hào)量",至于mutex,書中并沒有作為一個(gè)獨(dú)立的概念提出來,只是在講信號(hào)量時(shí)提到了上面所說的binary semaphore,并且說“信號(hào)量mutex(應(yīng)該是指binary semaphore)用于互斥...互斥是避免混亂所必需的操作...信號(hào)量的另一種用途是用于實(shí)現(xiàn)同步(synchronization)。信號(hào)量full和empty用來保證一定的事件順序發(fā)生或不發(fā)生。在本例中,它們保證緩沖區(qū)滿的時(shí)候生產(chǎn)者停止運(yùn)行,或者當(dāng)緩沖區(qū)空的時(shí)候消費(fèi)者停止運(yùn)行。這種用法與互斥是不同的” --- P30-31 *

OK,理論上的概念有了,那么就來看看實(shí)際當(dāng)中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;
  • };
  • 復(fù)制代碼 如果count=1的話,那么semaphore就可以用來進(jìn)行互斥操作了,早先內(nèi)核源碼中曾有一個(gè)專門的宏用來定義一個(gè)count=1的信號(hào)量DECLARE_MUTEX:
  • #define DECLARE_MUTEX(name) \
  • ? ?? ?? ?? ?struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
  • 復(fù)制代碼 因?yàn)槲覀冎涝贚inux內(nèi)核源碼中還有一個(gè)DEFINE_MUTEX宏,所以Marcin Slusarz同學(xué)認(rèn)為DECLARE_MUTEX宏容易讓人困惑,畢竟它其實(shí)只是定義了一個(gè)count=1的信號(hào)量,因此該同學(xué)早在08年就向內(nèi)核社區(qū)提交了一個(gè)PATCH,要求Rename DECLARE_MUTEX to DEFINE_SEMAPHORE( https://lkml.org/lkml/2008/10/26/74 ),這個(gè)PATCH最終被社區(qū)所接受(應(yīng)該是在2.6.35和2.6.39版本之間,2.6.39已經(jīng)沒有了DECLARE_MUTEX,取而代之的是DEFINE_SEMAPHORE,但2.6.35還有,我當(dāng)時(shí)在寫《深入Linux設(shè)備驅(qū)動(dòng)程序內(nèi)核機(jī)制》時(shí),最早引用的是2.6.35的版本,雖然在寫作的中晚期將內(nèi)核版本切換到2.6.39并在定稿階段曾試圖將之前寫的文檔全部修正到39版,但是DECLARE_MUTEX的殘留顯然是條漏網(wǎng)之魚...)。因?yàn)橹皇莚ename,所以DEFINE_SEMAPHORE的定義和原來的DECLARE_MUTEX完全相同。


    那么既然count=1的信號(hào)量可以用來完成mutex lock的功能,那么內(nèi)核何必再多此一舉弄出個(gè)mutex lock出來呢??

    總結(jié)

    以上是生活随笔為你收集整理的探讨mutex与semaphore的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。