学习信号量 sem_init、sem_destroy、sem_post、sem_wait、sem_trywait、sem_getvalue

学习信号量 sem_init、sem_destroy、sem_post、sem_wait、sem_trywait、sem_getvalue


概要:
1.信号量使用场合
2.POSIX标准定义的信号量
2.1 sem_init、sem_destroy、sem_post、sem_wait、sem_trywait、sem_getvalue简介
3.在linux中相关函数位置


1.信号量使用场合
我理解的信号量使用场合
当两个进程(线程)通信时,一个进程(线程)需要读操作,一个进程(线程)需要写操作,
在这种情况下,当出现同一时刻有多个进程(线程)对共享内存进行读写时,会出现数据损坏或丢失,此种情况下使用信号量就可以起到保护作用。
实现方式:是一种类似锁的机制,几个进程(线程)间都可以通过获取到同一个信号量的值,判断临界资源是否被信号量“锁住”,此时能否读取。


2.POSIX标准定义的信号量
Linux环境下主要实现的信号量有两种。根据标准的不同,它们跟共享内存类似,一套XSI的信号量,一套POSIX的信号量。

无名使用 <semaphore.h> POSIX 标准定义的 semaphore 接口
有名信号量<sys/sem.h> System V 标准的 semaphore接口

2.1 sem_init、sem_destroy、sem_post、sem_wait、sem_trywait、sem_getvalue简介

int sem_init (sem_t *sem, int pshared, unsigned int value);

功能:初始化信号量
返回值:创建成功返回0,失败返回-1
参数sem:指向信号量结构的一个指针
参数pshared:不为0时此信号量在进程间共享,为0时当前进程的所有线程共享
参数value:信号量的初始值

NAME
       sem_init - initialize an unnamed semaphore
SYNOPSIS
       #include <semaphore.h>
       int sem_init(sem_t *sem, int pshared, unsigned int value);
       Link with -pthread.
DESCRIPTION
       sem_init()  initializes  the unnamed semaphore at the address pointed to by sem.  The value argument specifies the initial
       value for the semaphore.
	   
       The pshared argument indicates whether this semaphore is to be shared between the threads of a process,  or  between  pro‐
       cesses.

       If  pshared  has the value 0, then the semaphore is shared between the threads of a process, and should be located at some
       address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap).

       If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared  memory
       (see  shm_open(3),  mmap(2),  and shmget(2)).  (Since a child created by fork(2) inherits its parent's memory mappings, it
       can also access the semaphore.)  Any process that can access the shared memory region can operate on the  semaphore  using
       sem_post(3), sem_wait(3), and so on.

       Initializing a semaphore that has already been initialized results in undefined behavior.
RETURN VALUE
       sem_init() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error.

int sem_destroy(sem_t * sem)

功能:释放信号量自己占用的一切资源 (被注销的信号量sem要求:没有线程在等待该信号量了)
返回值:满足条件 成功返回0,否则返回-1且置errno为EBUSY
参数sem:指向信号量结构的一个指针

NAME
       sem_destroy - destroy an unnamed semaphore
SYNOPSIS
       #include <semaphore.h>
       int sem_destroy(sem_t *sem);
       Link with -pthread.
DESCRIPTION
       sem_destroy() destroys the unnamed semaphore at the address pointed to by sem.
	   
       Only a semaphore that has been initialized by sem_init(3) should be destroyed using sem_destroy().

       Destroying a semaphore that other processes or threads are currently blocked on (in sem_wait(3)) produces undefined behav‐
       ior.

       Using a semaphore that has been destroyed produces undefined results, until the semaphore  has  been  reinitialized  using
       sem_init(3).
RETURN VALUE
       sem_destroy() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error.
ERRORS
       EINVAL sem is not a valid semaphore.

int sem_post(sem_t * sem)

功能:它的作用来增加信号量的值。给信号量加1。
返回值:操作成功返回0,失败则返回-1且置errno
参数sem:指向信号量结构的一个指针

NAME
       sem_post - unlock a semaphore
SYNOPSIS
       #include <semaphore.h>
       int sem_post(sem_t *sem);
       Link with -pthread.
DESCRIPTION
       sem_post()  increments  (unlocks)  the semaphore pointed to by sem.  If the semaphore's value consequently becomes greater
       than zero, then another process or thread blocked in a sem_wait(3) call will be woken up and proceed  to  lock  the  sema‐
       phore.
RETURN VALUE
       sem_post()  returns 0 on success; on error, the value of the semaphore is left unchanged, -1 is returned, and errno is set
       to indicate the error.
ERRORS
       EINVAL sem is not a valid semaphore.
       EOVERFLOW
              The maximum allowable value for a semaphore would be exceeded.

int sem_wait(sem_t * sem)

功能:它的作用是从信号量的值减去一个“1”,但它永远会先等待该信号量为一个非零值(大于0)才开始做减法。(如果对一个值为0的信号量调用sem_wait(),这个函数就会等待,直到有其它线程增加了信号量这个值使它不再是0为止,再进行减1操作。)
返回值:操作成功返回0,失败则返回-1且置errno
参数sem:指向信号量结构的一个指针

NAME
       sem_wait, sem_timedwait, sem_trywait - lock a semaphore

SYNOPSIS
       #include <semaphore.h>
       int sem_wait(sem_t *sem);
       int sem_trywait(sem_t *sem);
       int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
       Link with -pthread.

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       sem_timedwait(): _POSIX_C_SOURCE >= 200112L
DESCRIPTION
       sem_wait()  decrements  (locks)  the semaphore pointed to by sem.  If the semaphore's value is greater than zero, then the
       decrement proceeds, and the function returns, immediately.  If the semaphore currently has the value zero, then  the  call
       blocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signal
       handler interrupts the call.

       sem_trywait() is the same as sem_wait(), except that if the decrement cannot be immediately performed, then  call  returns
       an error (errno set to EAGAIN) instead of blocking.

       sem_timedwait()  is  the same as sem_wait(), except that abs_timeout specifies a limit on the amount of time that the call
       should block if the decrement cannot be immediately performed.  The abs_timeout argument points to a structure that speci‐
       fies  an  absolute timeout in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).  This structure is
       defined as follows:

           struct timespec {
               time_t tv_sec;      /* Seconds */
               long   tv_nsec;     /* Nanoseconds [0 .. 999999999] */
           };

       If the timeout has already expired by the time of the call, and the  semaphore  could  not  be  locked  immediately,  then
       sem_timedwait() fails with a timeout error (errno set to ETIMEDOUT).

       If  the  operation  can be performed immediately, then sem_timedwait() never fails with a timeout error, regardless of the
       value of abs_timeout.  Furthermore, the validity of abs_timeout is not checked in this case.
RETURN VALUE
       All of these functions return 0 on success; on error, the value of the semaphore is left unchanged, -1  is  returned,  and
       errno is set to indicate the error.
ERRORS
       EINTR  The call was interrupted by a signal handler; see signal(7).
       EINVAL sem is not a valid semaphore.
       The following additional error can occur for sem_trywait():
       EAGAIN The operation could not be performed without blocking (i.e., the semaphore currently has the value zero).
       The following additional errors can occur for sem_timedwait():
       EINVAL The value of abs_timeout.tv_nsecs is less than 0, or greater than or equal to 1000 million.
       ETIMEDOUT
              The call timed out before the semaphore could be locked.

int sem_trywait(sem_t * sem)

功能:sem_trywait()为sem_wait()的非阻塞版,不进行等待
返回值:如果信号量计数大于0,则信号量立即减1并返回0,否则立即返回-1,errno置为EAGAIN
参数sem:指向信号量结构的一个指针

NAME
       sem_getvalue - get the value of a semaphore
SYNOPSIS
       #include <semaphore.h>
       int sem_getvalue(sem_t *sem, int *sval);
       Link with -pthread.
DESCRIPTION
       sem_getvalue() places the current value of the semaphore pointed to sem into the integer pointed to by sval.

       If one or more processes or threads are blocked waiting to lock the semaphore with sem_wait(3), POSIX.1 permits two possi‐
       bilities for the value returned in sval: either 0 is returned; or a negative number whose absolute value is the  count  of
       the number of processes and threads currently blocked in sem_wait(3).  Linux adopts the former behavior.
RETURN VALUE
       sem_getvalue() returns 0 on success; on error, -1 is returned and errno is set to indicate the error.
ERRORS
       EINVAL sem is not a valid semaphore.

int sem_getvalue(sem_t * sem, int * sval)

功能: 读取sem中信号量计数
返回值: 操作成功返回0,失败则返回-1且置errno
参数sem:指向信号量结构的一个指针
参数sval:信号量计数值


3.在linux中相关函数位置

在linux中定义位置usr/include/semaphore.h
usr/include/semaphore.h

//结构体
typedef union
{
  char __size[__SIZEOF_SEM_T];
  long int __align;
} sem_t;
//部分代码
/* Initialize semaphore object SEM to VALUE.  If PSHARED then share it
   with other processes.  */
extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value)
     __THROW;
/* Free resources associated with semaphore object SEM.  */
extern int sem_destroy (sem_t *__sem) __THROW;

/* Wait for SEM being posted.

   This function is a cancellation point and therefore not marked with
   __THROW.  */
extern int sem_wait (sem_t *__sem);

#ifdef __USE_XOPEN2K
/* Similar to `sem_wait' but wait only until ABSTIME.

   This function is a cancellation point and therefore not marked with
   __THROW.  */
extern int sem_timedwait (sem_t *__restrict __sem,
			  const struct timespec *__restrict __abstime);
#endif

/* Test whether SEM is posted.  */
extern int sem_trywait (sem_t *__sem) __THROWNL;

/* Post SEM.  */
extern int sem_post (sem_t *__sem) __THROWNL;

/* Get current value of SEM and store it in *SVAL.  */
extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval)

在linux中查看 e.g. man sem_init

参考资料:
1.https://cloud.tencent.com/developer/article/1005536
2.https://blog.csdn.net/amumu_123/article/details/70313307?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

  • 14
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值