请注意:不要混合着使用系统V自带的信号量,系统V的信号量位于sys/sem.h文件中
关于Semphore的API有下面几点说明:
- sem_init: 初始化一个新的semphore变量,第二个参数指出信号量的共享方式,0意味着该信号量是在线程间共享的而不是在进程间共享,最后的一个参数说明了信号量初始化时候的初始值
- sem_destroy: 析构掉一个已经退出的semphore变量
- sem_wait: 相当于P()操作
- sem_post: 相当于V()操作
下面让我们用表格的形式总结一下关于线程操作的一些方法
基本操作 | 绝缘量 | 互斥量 | 信号量 | |
生成 | pthread_create | pthread_barrier_init | pthread_mutex_init | sem_init |
析构 | pthread_exit | pthread_barrier_destroy | pthread_mutex_destroy | sem_destroy |
挂起等待 | pthread_join | pthread_barrier_wait | --- | --- |
获得资源 | --- | --- | pthread_mutex_lock | sem_wait |
释放 | --- | --- | pthread_mutex_unlock | sem_post |
---man aix
man sem_init
Technical Reference: Base Operating System and Extensions, Volume 2
sem_init Subroutine
Purpose
Initializes an unnamed semaphore.
Library
Standard C Library (libc.a)
Syntax
#include
int sem_init (sem, pshared, value)
sem_t *sem;
int pshared;
unsigned value;
Description
The sem_init subroutine initializes the unnamed semaphore referred to by the sem parameter.
The value of the initialized semaphore is contained in the value parameter. Following a
successful call to the sem_init subroutine, the semaphore might be used in subsequent calls
to the sem_wait, sem_trywait, sem_post, and sem_destroy subroutines. This semaphore remains
usable until it is destroyed.
If the pshared parameter has a nonzero value, the semaphore is shared between processes. In
this case, any process that can access the sem parameter can use it for performing sem_wait,
sem_trywait, sem_post, and sem_destroy operations.
Only the sem parameter itself may be used for performing synchronization.
If the pshared parameter is zero, the semaphore is shared between threads of the process. Any
thread in this process can use the sem parameter for performing sem_wait, sem_trywait,
sem_post, and sem_destroy operations. The use of the semaphore by threads other than those
created in the same process returns an error.
Attempting to initialize a semaphore that has been already initialized results in the loss of
access to the previous semaphore.
Parameters
sem
Specifies the semaphore to be initialized.
pshared
Determines whether the semaphore can be shared between processes or not.
value
Contains the value of the initialized semaphore.
Return Values
Upon successful completion, the sem_init subroutine initializes the semaphore in the sem
parameter. Otherwise, it returns -1 and sets errno to indicate the error.
Error Codes
The sem_init subroutine fails if:
EFAULT
Invalid user address.
EINVAL
The value parameter exceeds SEM_VALUE_MAX.
ENFILE
Too many semaphores are currently open in the system.
ENOMEM
Insufficient memory for the required operation.
ENOSPC
A resource required to initialize the semaphore has been exhausted, or the limit on
semaphores, SEM_NSEMS_MAX, has been reached.
ENOTSUP
This function is not supported with processes that have been checkpoint-restart'ed.
Related Information
sem_destroy Subroutine, sem_post Subroutine, and sem_trywait and sem_wait Subroutine.
man sem_wait
Technical Reference: Base Operating System and Extensions, Volume 2
sem_trywait and sem_wait Subroutine
Purpose
Locks a semaphore.
Library
Standard C Library (libc.a)
Syntax
#include
int sem_trywait (sem)
sem_t *sem;
int sem_wait (sem)
sem_t *sem;
Description
The sem_trywait subroutine locks the semaphore referenced by the sem parameter only if the
semaphore is currently not locked; that is, if the semaphore value is currently positive.
Otherwise, it does not lock the semaphore.
The sem_wait subroutine locks the semaphore referenced by the sem parameter by performing a
semaphore lock operation on that semaphore. If the semaphore value is currently zero, the
calling thread does not return from the call to the sem_wait subroutine until it either locks
the semaphore or the call is interrupted by a signal.
Upon successful return, the state of the semaphore will be locked and will remain locked
until the sem_post subroutine is executed and returns successfully.
The sem_wait subroutine is interruptible by the delivery of a signal.
Parameters
sem
Specifies the semaphore to be locked.
Return Values
The sem_trywait and sem_wait subroutines return zero if the calling process successfully
performed the semaphore lock operation. If the call was unsuccessful, the state of the
semaphore is unchanged, and the subroutine returns -1 and sets errno to indicate the error.
Error Codes
The sem_trywait and sem_wait subroutines fail if:
EACCES
Permission is denied to access the unnamed semaphore.
EAGAIN
The semaphore was already locked, so it cannot be immediately locked by the sem_trywait
subroutine.
EFAULT
Invalid user address.
EIDRM
Semaphore was removed during the required operation.
EINTR
A signal interrupted the subroutine.
EINVAL
The sem parameter does not refer to a valid semaphore.
ENOMEM
Insufficient memory for the required operation.
ENOTSUP
This function is not supported with processes that have been checkpoint-restart'ed.
Related Information
sem_open Subroutine and sem_post Subroutine.