信号量

1767阅读 0评论2010-10-03 snailshen
分类:C/C++

在pthreads标准库里面, 库本身并不提供对信号量的支持,因为POSIX标准并没有对信号量做出定义,但是如果你一定要使用信号量来完成程序的话,那么所有的内容都会包含在semphore.h文件

请注意:不要混合着使用系统V自带的信号量,系统V的信号量位于sys/sem.h文件中

关于Semphore的API有下面几点说明:

  • sem_init: 初始化一个新的semphore变量,第二个参数指出信号量的共享方式,0意味着该信号量是在线程间共享的而不是在进程间共享,最后的一个参数说明了信号量初始化时候的初始值
  • sem_destroy: 析构掉一个已经退出的semphore变量
  • sem_wait: 相当于P()操作  
  • sem_post: 相当于V()操作

下面让我们用表格的形式总结一下关于线程操作的一些方法

 基本操作绝缘量互斥量信号量
生成pthread_createpthread_barrier_initpthread_mutex_initsem_init
析构pthread_exitpthread_barrier_destroypthread_mutex_destroysem_destroy
挂起等待pthread_joinpthread_barrier_wait------
获得资源------pthread_mutex_locksem_wait
释放------pthread_mutex_unlocksem_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.













man sem_post

                Technical Reference: Base Operating System and Extensions, Volume 2

sem_post Subroutine

Purpose

       Unlocks a semaphore.

Library

       Standard C Library (libc.a)

Syntax

       #include
       int sem_post (sem)
       sem_t *sem;

Description

       The sem_post subroutine unlocks the semaphore referenced by the sem parameter by performing a
       semaphore unlock operation on that semaphore.

       If the semaphore value resulting from this operation is positive, no threads were blocked
       waiting for the semaphore to become unlocked, and the semaphore value is incremented.

       If the value of the semaphore resulting from this operation is zero, one of the threads
       blocked waiting for the semaphore is allowed to return successfully from its call to the
       sem_wait subroutine. If the Process Scheduling option is supported, the thread to be
       unblocked is chosen in a manner appropriate to the scheduling policies and parameters in
       effect for the blocked threads. In the case of the schedulers SCHED_FIFO and SCHED_RR, the
       highest priority waiting thread shall be is unblocked, and if there is more than one highest
       priority thread blocked waiting for the semaphore, then the highest priority thread that has
       been waiting the longest is unblocked. If the Process Scheduling option is not defined, the
       choice of a thread to unblock is unspecified.

       If the Process Sporadic Server option is supported, and the scheduling policy is
       SCHED_SPORADIC, the semantics are the same as SCHED_FIFO in the preceding paragraph.

       The sem_post subroutine is reentrant with respect to signals and may be invoked from a
       signal-catching function.

Parameters
       sem
            Specifies the semaphore to be unlocked.

Return Values

       If successful, the sem_post subroutine returns zero. Otherwise, it returns -1 and sets errno
       to indicate the error.

Error Codes

       The sem_post subroutine fails if:
       EACCES
            Permission is denied to access the unnamed semaphore.
       EFAULT
            Invalid user address.

       EIDRM
            Semaphore was removed during the required operation.
       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_trywait and sem_wait Subroutine.

上一篇:结构体对齐
下一篇:写文件造成PgSp 持续增长