SYNOPSIS
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
//初始化信號(hào)量
int sem_wait(sem_t * sem);
//等待信號(hào),獲取擁有權(quán)
int sem_trywait(sem_t * sem);
int sem_post(sem_t * sem);
//發(fā)出信號(hào)即釋放擁有權(quán)
int sem_getvalue(sem_t * sem, int * sval);
int sem_destroy(sem_t * sem);
//注銷(xiāo)信號(hào)量,在linux中其本質(zhì)是沒(méi)有任何作用的,它不做任何事情。
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.
//信號(hào)量是在多線程環(huán)境中共享資源的計(jì)數(shù)器。對(duì)信號(hào)量的基本操作無(wú)非有三個(gè):對(duì)信號(hào)量的增加;然后阻塞線程等待,直到信號(hào)量不為空才返回;然后就是對(duì)信號(hào)量的減少。
// 在編程中,信號(hào)量最常用的方式就是一個(gè)線程A使用sem_wait阻塞,因?yàn)榇藭r(shí)信號(hào)量計(jì)數(shù)為0,直到另外一個(gè)線程B發(fā)出信號(hào)post后,信號(hào)量計(jì)數(shù)加 1,此時(shí),線程A得到了信號(hào),信號(hào)量的計(jì)數(shù)為1不為空,所以就從sem_wait返回了,然后信號(hào)量的計(jì)數(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.
//在使用信號(hào)量之前,我們必須初始化信號(hào)。第三個(gè)參數(shù)通常設(shè)置為零,初始化信號(hào)的計(jì)數(shù)為0,這樣第一次使用sem_wait的時(shí)候會(huì)因?yàn)樾盘?hào)計(jì)數(shù)為0而等待,直到在其他地方信號(hào)量post了才返回。除非你明白你在干什么,否則不要將第三個(gè)參數(shù)設(shè)置為大于0的數(shù)。
//第二個(gè)參數(shù)是用在進(jìn)程之間的數(shù)據(jù)共享標(biāo)志,如果僅僅使用在當(dāng)前進(jìn)程中,設(shè)置為0。如果要在多個(gè)進(jìn)程之間使用該信號(hào),設(shè)置為非零。但是在Linux線程中,暫時(shí)還不支持進(jìn)程之間的信號(hào)共享,所以第二個(gè)參數(shù)說(shuō)了半天等于白說(shuō),必須設(shè)置為0.否則將返回ENOSYS錯(cuò)誤。
sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically
decreases the semaphore count.
//當(dāng)信號(hào)的計(jì)數(shù)為零的時(shí)候,sem_wait將休眠掛起當(dāng)前調(diào)用線程,直到信號(hào)量計(jì)數(shù)不為零。在sem_wait返回后信號(hào)量計(jì)數(shù)將自動(dòng)減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是一個(gè)立即返回函數(shù),不會(huì)因?yàn)槿魏问虑樽枞8鶕?jù)其返回值得到不同的信息。如果返回值為0,說(shuō)明信號(hào)量在該函數(shù)調(diào)用之前大于0,但是調(diào)用之后會(huì)被該函數(shù)自動(dòng)減1.至于調(diào)用之后是否為零則不得而知了。如果返回值為EAGAIN說(shuō)明信號(hào)量計(jì)數(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.
//解除信號(hào)量等待限制。讓信號(hào)量計(jì)數(shù)加1.該函數(shù)會(huì)立即返回不等待。
sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.
//獲得當(dāng)前信號(hào)量計(jì)數(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.
// 銷(xiāo)毀信號(hào)量對(duì)象,釋放信號(hào)量?jī)?nèi)部資源。然而在linux的線程中,其實(shí)是沒(méi)有任何資源關(guān)聯(lián)到信號(hào)量對(duì)象需要釋放的,因此在linux中,銷(xiāo)毀信號(hào)量對(duì)象的 作用僅僅是測(cè)試是否有線程因?yàn)樵撔盘?hào)量在等待。如果函數(shù)返回0說(shuō)明沒(méi)有,正常注銷(xiāo)信號(hào)量,如果返回EBUSY,說(shuō)明還有線程正在等待該信號(hào)量的信號(hào)。
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ī)范,當(dāng)它改變信號(hào)量計(jì)數(shù)器值的時(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,并且將錯(cuò)誤的代碼號(hào)賦值給errno。
ERRORS
The sem_init function sets errno to the following codes on error:
//sem_init失敗時(shí),常見(jiàn)錯(cuò)誤有:
EINVAL value exceeds the maximal counter value SEM_VALUE_MAX
//第三個(gè)參數(shù)value值超過(guò)了系統(tǒng)能夠承受的最大值SEM_VALUE_MAX.
ENOSYS pshared is not zero
//你將第二參數(shù)設(shè)置為了非零,如果是linux系統(tǒng),請(qǐng)將第二個(gè)參數(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)
//信號(hào)量的計(jì)數(shù)超過(guò)了系統(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.
//某些線程正在使用該信號(hào)量等待。
其實(shí)線程臨界區(qū)可以使用信號(hào)量來(lái)實(shí)現(xiàn),將信號(hào)量的信號(hào)初始化為1,然后在臨界區(qū)使用完畢后再置信號(hào)量為1我們就可以輕松實(shí)現(xiàn)mutex了。
具體實(shí)現(xiàn),自己慢慢琢磨一下吧。