• <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
            偷窥少妇久久久久久久久| 亚洲一区精品伊人久久伊人| 亚洲精品美女久久久久99小说| 99久久免费只有精品国产| 久久性生大片免费观看性| 国产精品无码久久四虎| 久久精品国产精品亚洲| 欧美国产成人久久精品| 2020久久精品国产免费| 久久久久亚洲爆乳少妇无| 麻豆av久久av盛宴av| 日韩av无码久久精品免费| 人人狠狠综合久久亚洲婷婷| 日韩电影久久久被窝网| 麻豆成人久久精品二区三区免费 | 久久精品草草草| 久久久久成人精品无码| 久久久久久久亚洲Av无码| 久久精品国产只有精品66| 亚洲va中文字幕无码久久不卡 | 亚洲伊人久久综合中文成人网| 久久精品aⅴ无码中文字字幕不卡| 久久国产精品免费一区二区三区| 亚洲中文字幕无码久久精品1| 久久精品一区二区三区中文字幕| 97久久超碰成人精品网站| 亚洲国产小视频精品久久久三级| 欧美精品一本久久男人的天堂| 人妻精品久久久久中文字幕69| 一级女性全黄久久生活片免费 | 99久久99久久久精品齐齐| 久久久黄色大片| 久久成人18免费网站| 亚洲综合久久综合激情久久| 久久国产AVJUST麻豆| 久久精品国产91久久麻豆自制| 亚洲香蕉网久久综合影视 | 一级女性全黄久久生活片免费| 久久国产热这里只有精品| 久久国产精品久久精品国产| 国产三级久久久精品麻豆三级|