• <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>
            隨筆-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);
            //等待信號,獲取擁有權
                   int sem_trywait(sem_t * sem);

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

                   int sem_destroy(sem_t * sem);
            //注銷信號量,在linux中其本質是沒有任何作用的,它不做任何事情。
            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.
            //信號量是在多線程環境中共享資源的計數器。對信號量的基本操作無非有三個:對信號量的增加;然后阻塞線程等待,直到信號量不為空才返回;然后就是對信號量的減少。
            // 在編程中,信號量最常用的方式就是一個線程A使用sem_wait阻塞,因為此時信號量計數為0,直到另外一個線程B發出信號post后,信號量計數加 1,此時,線程A得到了信號,信號量的計數為1不為空,所以就從sem_wait返回了,然后信號量的計數又減1變為零。
                   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.
            //在使用信號量之前,我們必須初始化信號。第三個參數通常設置為零,初始化信號的計數為0,這樣第一次使用sem_wait的時候會因為信號計數為0而等待,直到在其他地方信號量post了才返回。除非你明白你在干什么,否則不要將第三個參數設置為大于0的數。
            //第二個參數是用在進程之間的數據共享標志,如果僅僅使用在當前進程中,設置為0。如果要在多個進程之間使用該信號,設置為非零。但是在Linux線程中,暫時還不支持進程之間的信號共享,所以第二個參數說了半天等于白說,必須設置為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.
            //當信號的計數為零的時候,sem_wait將休眠掛起當前調用線程,直到信號量計數不為零。在sem_wait返回后信號量計數將自動減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是一個立即返回函數,不會因為任何事情阻塞。根據其返回值得到不同的信息。如果返回值為0,說明信號量在該函數調用之前大于0,但是調用之后會被該函數自動減1.至于調用之后是否為零則不得而知了。如果返回值為EAGAIN說明信號量計數為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.
            //解除信號量等待限制。讓信號量計數加1.該函數會立即返回不等待。
                   sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.
            //獲得當前信號量計數的值。
                   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.
            // 銷毀信號量對象,釋放信號量內部資源。然而在linux的線程中,其實是沒有任何資源關聯到信號量對象需要釋放的,因此在linux中,銷毀信號量對象的 作用僅僅是測試是否有線程因為該信號量在等待。如果函數返回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.
            //現在sem_post被POSIX所規范,當它改變信號量計數器值的時候是線程安全的。

            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
               //第三個參數value值超過了系統能夠承受的最大值SEM_VALUE_MAX.

                          ENOSYS pshared is not zero
               //你將第二參數設置為了非零,如果是linux系統,請將第二個參數設置為零

                   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)
               //信號量的計數超過了系統能夠承受的最大值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.
               //某些線程正在使用該信號量等待。

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

            具體實現,自己慢慢琢磨一下吧。

            posted on 2011-08-05 11:21 老馬驛站 閱讀(1298) 評論(0)  編輯 收藏 引用 所屬分類: linux
            精品久久一区二区三区| 亚州日韩精品专区久久久| 久久久精品国产sm调教网站| 国产精品99精品久久免费| 日本久久久久久中文字幕| 亚洲精品NV久久久久久久久久 | 久久青青草视频| 久久久久女人精品毛片| 久久99久久无码毛片一区二区| 色综合久久中文字幕综合网| 久久久久亚洲av无码专区导航| segui久久国产精品| 久久久久久久久无码精品亚洲日韩 | 国产福利电影一区二区三区,免费久久久久久久精| 久久免费高清视频| 色欲综合久久中文字幕网| 亚洲综合婷婷久久| 久久精品国产亚洲av麻豆小说 | 亚洲AV日韩精品久久久久久 | 91精品国产9l久久久久| 亚洲国产精品狼友中文久久久| 久久久久久久亚洲Av无码| 精品伊人久久大线蕉色首页| 国内精品久久久久久久亚洲| 91精品国产91久久久久福利| 久久久久免费精品国产| 久久精品18| 国产女人aaa级久久久级| 国产精品无码久久综合| 97精品伊人久久久大香线蕉| 一本久久精品一区二区| 久久久久亚洲精品无码网址 | 蜜臀av性久久久久蜜臀aⅴ| 久久综合久久综合亚洲| 久久夜色精品国产| 九九久久精品无码专区| 久久久久亚洲?V成人无码| 久久久久亚洲精品中文字幕| 一本色综合久久| 五月丁香综合激情六月久久| 精品免费久久久久久久|