青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆-167  評論-8  文章-0  trackbacks-0

SYNOPSIS
       #include <semaphore.h>

       int sem_init(sem_t *sem, int pshared, unsigned int value);
//初始化信號量
       int sem_wait(sem_t * sem);
//等待信號,獲取擁有權(quán)
       int sem_trywait(sem_t * sem);

       int sem_post(sem_t * sem);
//發(fā)出信號即釋放擁有權(quán)
       int sem_getvalue(sem_t * sem, int * sval);

       int sem_destroy(sem_t * sem);
//注銷信號量,在linux中其本質(zhì)是沒有任何作用的,它不做任何事情。
DESCRIPTION
       This manual page documents POSIX 1003.1b semaphores, not to be confused with SystemV semaphores as described in ipc(5),
       semctl(2) and semop(2).

       Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the
       counter atomically, and wait until the counter is non-null and decrement it atomically.
//信號量是在多線程環(huán)境中共享資源的計數(shù)器。對信號量的基本操作無非有三個:對信號量的增加;然后阻塞線程等待,直到信號量不為空才返回;然后就是對信號量的減少。
// 在編程中,信號量最常用的方式就是一個線程A使用sem_wait阻塞,因為此時信號量計數(shù)為0,直到另外一個線程B發(fā)出信號post后,信號量計數(shù)加 1,此時,線程A得到了信號,信號量的計數(shù)為1不為空,所以就從sem_wait返回了,然后信號量的計數(shù)又減1變?yōu)榱恪?br style="line-height: normal !important; " />       sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set initially to
       value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to
       be shared between several processes ( pshared is not zero). LinuxThreads currently does not support process-shared
       semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.
//在使用信號量之前,我們必須初始化信號。第三個參數(shù)通常設(shè)置為零,初始化信號的計數(shù)為0,這樣第一次使用sem_wait的時候會因為信號計數(shù)為0而等待,直到在其他地方信號量post了才返回。除非你明白你在干什么,否則不要將第三個參數(shù)設(shè)置為大于0的數(shù)。
//第二個參數(shù)是用在進程之間的數(shù)據(jù)共享標志,如果僅僅使用在當前進程中,設(shè)置為0。如果要在多個進程之間使用該信號,設(shè)置為非零。但是在Linux線程中,暫時還不支持進程之間的信號共享,所以第二個參數(shù)說了半天等于白說,必須設(shè)置為0.否則將返回ENOSYS錯誤。
       sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically
       decreases the semaphore count.
//當信號的計數(shù)為零的時候,sem_wait將休眠掛起當前調(diào)用線程,直到信號量計數(shù)不為零。在sem_wait返回后信號量計數(shù)將自動減1.

       sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is
       atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately
       returns with error EAGAIN.
//sem_trywait是一個立即返回函數(shù),不會因為任何事情阻塞。根據(jù)其返回值得到不同的信息。如果返回值為0,說明信號量在該函數(shù)調(diào)用之前大于0,但是調(diào)用之后會被該函數(shù)自動減1.至于調(diào)用之后是否為零則不得而知了。如果返回值為EAGAIN說明信號量計數(shù)為0。
       sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be
       used in asynchronous signal handlers.
//解除信號量等待限制。讓信號量計數(shù)加1.該函數(shù)會立即返回不等待。
       sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.
//獲得當前信號量計數(shù)的值。
       sem_destroy destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the
       semaphore at the time sem_destroy is called. In the LinuxThreads implementation, no resources are associated with
       semaphore objects, thus sem_destroy actually does nothing except checking that no thread is waiting on the semaphore.
// 銷毀信號量對象,釋放信號量內(nèi)部資源。然而在linux的線程中,其實是沒有任何資源關(guān)聯(lián)到信號量對象需要釋放的,因此在linux中,銷毀信號量對象的 作用僅僅是測試是否有線程因為該信號量在等待。如果函數(shù)返回0說明沒有,正常注銷信號量,如果返回EBUSY,說明還有線程正在等待該信號量的信號。

CANCELLATION
       sem_wait is a cancellation point.

ASYNC-SIGNAL SAFETY
       On processors supporting atomic compare-and-swap (Intel 486, Pentium and later, Alpha, PowerPC, MIPS II, Motorola 68k),
       the sem_post function is async-signal safe and can therefore be called from signal handlers. This is the only thread syn-
       chronization function provided by POSIX threads that is async-signal safe.

       On the Intel 386 and the Sparc, the current LinuxThreads implementation of sem_post is not async-signal safe by lack of
       the required atomic operations.
//現(xiàn)在sem_post被POSIX所規(guī)范,當它改變信號量計數(shù)器值的時候是線程安全的。

RETURN VALUE
       The sem_wait and sem_getvalue functions always return 0. All other semaphore functions return 0 on success and -1 on
       error, in addition to writing an error code in errno.
//通常返回值往往都為0,表示成功,如果有誤將返回-1,并且將錯誤的代碼號賦值給errno。

ERRORS
       The sem_init function sets errno to the following codes on error:
//sem_init失敗時,常見錯誤有:
              EINVAL value exceeds the maximal counter value SEM_VALUE_MAX
   //第三個參數(shù)value值超過了系統(tǒng)能夠承受的最大值SEM_VALUE_MAX.

              ENOSYS pshared is not zero
   //你將第二參數(shù)設(shè)置為了非零,如果是linux系統(tǒng),請將第二個參數(shù)設(shè)置為零

       The sem_trywait function sets errno to the following error code on error:

              EAGAIN the semaphore count is currently 0

       The sem_post function sets errno to the following error code on error:

              ERANGE after incrementation, the semaphore value would exceed SEM_VALUE_MAX (the semaphore count is left unchanged
                     in this case)
   //信號量的計數(shù)超過了系統(tǒng)能夠承受的最大值SEM_VALUE_MAX。

       The sem_destroy function sets errno to the following error code on error:

              EBUSY some threads are currently blocked waiting on the semaphore.
   //某些線程正在使用該信號量等待。

其實線程臨界區(qū)可以使用信號量來實現(xiàn),將信號量的信號初始化為1,然后在臨界區(qū)使用完畢后再置信號量為1我們就可以輕松實現(xiàn)mutex了。

具體實現(xiàn),自己慢慢琢磨一下吧。

posted on 2011-08-05 11:21 老馬驛站 閱讀(1320) 評論(0)  編輯 收藏 引用 所屬分類: linux
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美一区日韩一区| 欧美一级久久久久久久大片| 欧美福利视频网站| 美女性感视频久久久| 久久午夜激情| 裸体丰满少妇做受久久99精品| 久久亚洲高清| 欧美日韩妖精视频| 国产精品在线看| 狠狠做深爱婷婷久久综合一区| 精品av久久久久电影| 亚洲激情偷拍| 午夜精品影院| 欧美高清视频在线播放| 亚洲乱码国产乱码精品精天堂 | 亚洲私人影院在线观看| 亚洲天堂网在线观看| 久久夜色精品国产噜噜av| 欧美日韩国产色综合一二三四| 欧美视频中文一区二区三区在线观看| 欧美影院午夜播放| 亚洲韩国精品一区| 在线视频一区观看| 欧美电影免费观看| 国内精品视频久久| 亚洲欧美一区二区三区极速播放 | 欧美主播一区二区三区美女 久久精品人| 午夜久久99| 国产精品三上| 亚洲一区二区在| 日韩视频免费观看高清在线视频 | 亚洲欧洲三级| 久久精品成人| 国内久久视频| 欧美成人午夜激情| 久久亚洲综合色一区二区三区| 国产日韩欧美精品综合| 欧美一区亚洲| 久久青青草综合| 在线播放中文字幕一区| 欧美高清视频在线播放| 免费欧美高清视频| 亚洲手机在线| 欧美资源在线观看| 亚洲欧洲中文日韩久久av乱码| 欧美高清在线播放| 国产精品黄页免费高清在线观看| 亚洲一区二区三区四区中文| 亚洲欧美激情一区| 亚洲国产精品综合| 亚洲精品社区| 一二三四社区欧美黄| 国产精品夫妻自拍| 久久这里只有精品视频首页| 久久综合网络一区二区| 午夜激情亚洲| 欧美日韩国产一区二区三区| 欧美中文在线字幕| 欧美视频在线观看一区二区| 免费久久99精品国产| 欧美性开放视频| 欧美激情亚洲国产| 黄色一区二区在线观看| 亚洲午夜高清视频| 亚洲在线黄色| 欧美色图麻豆| 日韩午夜电影| 亚洲无限乱码一二三四麻| 欧美国产亚洲精品久久久8v| 美女999久久久精品视频| 国产精品一区视频网站| 在线一区二区三区四区| 亚洲欧美在线免费观看| 欧美午夜精品伦理| 在线午夜精品| 欧美一区亚洲二区| 国产亚洲欧美色| 久久男女视频| 亚洲电影在线| 亚洲欧美一区二区在线观看| 国产精品久久久久9999高清| 亚洲少妇中出一区| 久久精品国产99精品国产亚洲性色 | 国产一区二区欧美日韩| 一区二区三区导航| 久久精品国亚洲| 欧美在线不卡视频| 美女视频网站黄色亚洲| 国模私拍视频一区| 欧美精品色网| 欧美影视一区| 亚洲免费av电影| 久久久777| 制服丝袜亚洲播放| 国产在线视频不卡二| 久久天堂av综合合色| 99re6这里只有精品| 欧美不卡视频一区发布| 亚洲一区久久| 亚洲精品久久久久久一区二区 | 亚洲视屏在线播放| 欧美激情第五页| 久久久久99| 亚洲欧美日韩国产成人精品影院| 黄色成人精品网站| 国产精品一区二区在线观看不卡| 免费在线日韩av| 另类综合日韩欧美亚洲| 欧美一区亚洲| 欧美一区二区三区的| 亚洲性人人天天夜夜摸| a91a精品视频在线观看| 亚洲国产高清高潮精品美女| 免费成人美女女| 欧美激情欧美激情在线五月| 久久久亚洲精品一区二区三区| 欧美中日韩免费视频| 久久精品国产免费| 久久亚洲视频| 亚洲国产cao| 亚洲精品久久久蜜桃| 99re热精品| 亚洲欧美网站| 欧美不卡在线| 久久久久久精| 久久精品国产一区二区三| 老司机67194精品线观看| 麻豆av福利av久久av| 亚洲欧美日韩在线播放| 欧美福利视频网站| 久久影音先锋| 国产日韩欧美不卡在线| 一区二区三区日韩| 91久久久亚洲精品| 美女露胸一区二区三区| 每日更新成人在线视频| 国产亚洲精品久久久| 亚洲欧美福利一区二区| 亚洲欧美日韩网| 国产精品日韩欧美一区二区三区| 99精品福利视频| 亚洲一区二区免费在线| 欧美日韩精品中文字幕| 99re热这里只有精品免费视频| 99国内精品久久| 欧美揉bbbbb揉bbbbb| 一区二区三区色| 欧美一区二区高清| 国产午夜一区二区三区| 久久久综合网站| 亚洲国产成人精品视频| 亚洲第一毛片| 模特精品裸拍一区| 亚洲精品在线看| 亚洲在线成人| 国产一区二区三区在线观看精品| 欧美一区二区免费观在线| 久久最新视频| 一区二区av在线| 国产精品一区二区视频| 久久久精品国产免费观看同学| 免费的成人av| 国产精品99久久久久久宅男| 国产精品一区二区久久精品| 久久精品二区三区| 亚洲日本va午夜在线影院| 亚洲欧美成aⅴ人在线观看| 国产一区二区日韩精品| 欧美精品二区三区四区免费看视频| 一区二区久久久久久| 久久躁日日躁aaaaxxxx| 一区二区av| 国内精品久久久久影院优| 欧美阿v一级看视频| 亚洲欧美日韩在线| 亚洲国产午夜| 久久久久久久一区二区| 中文亚洲字幕| 亚洲国产精品久久久久| 国产精品乱码久久久久久| 麻豆亚洲精品| 午夜在线一区| 99在线热播精品免费| 欧美顶级艳妇交换群宴| 午夜精品久久久久久久蜜桃app| 亚洲大胆av| 国产午夜精品理论片a级大结局| 欧美精品观看| 狂野欧美激情性xxxx| 午夜国产精品视频| 一区二区三区精密机械公司 | 午夜精品久久久久| 精品成人在线| 国产精品日韩二区| 欧美成人a∨高清免费观看| 午夜免费在线观看精品视频| 亚洲精品永久免费精品| 欧美成人免费全部观看天天性色| 欧美一区国产一区| 亚洲永久网站|