曲阜做网站的公司,h5网站制作平台,建设网站的建议,百度站长平台链接提交学习信号量 sem_init、sem_destroy、sem_post、sem_wait、sem_trywait、sem_getvalue 概要#xff1a; 1.信号量使用场合 2.POSIX标准定义的信号量 2.1 sem_init、sem_destroy、sem_post、sem_wait、sem_trywait、sem_getvalue简介 3.在linux中相关函数位置 1.信号量使用场合 … 学习信号量 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时当前进程的所有线程共享 参数value信号量的初始值
NAMEsem_init - initialize an unnamed semaphore
SYNOPSIS#include semaphore.hint sem_init(sem_t *sem, int pshared, unsigned int value);Link with -pthread.
DESCRIPTIONsem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initialvalue 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 someaddress 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 parents memory mappings, itcan also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore usingsem_post(3), sem_wait(3), and so on.Initializing a semaphore that has already been initialized results in undefined behavior.
RETURN VALUEsem_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指向信号量结构的一个指针
NAMEsem_destroy - destroy an unnamed semaphore
SYNOPSIS#include semaphore.hint sem_destroy(sem_t *sem);Link with -pthread.
DESCRIPTIONsem_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 usingsem_init(3).
RETURN VALUEsem_destroy() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error.
ERRORSEINVAL sem is not a valid semaphore.int sem_post(sem_t * sem)功能它的作用来增加信号量的值。给信号量加1。 返回值操作成功返回0失败则返回-1且置errno 参数sem指向信号量结构的一个指针
NAMEsem_post - unlock a semaphore
SYNOPSIS#include semaphore.hint sem_post(sem_t *sem);Link with -pthread.
DESCRIPTIONsem_post() increments (unlocks) the semaphore pointed to by sem. If the semaphores value consequently becomes greaterthan 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 VALUEsem_post() returns 0 on success; on error, the value of the semaphore is left unchanged, -1 is returned, and errno is setto indicate the error.
ERRORSEINVAL sem is not a valid semaphore.EOVERFLOWThe 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指向信号量结构的一个指针
NAMEsem_wait, sem_timedwait, sem_trywait - lock a semaphoreSYNOPSIS#include semaphore.hint 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
DESCRIPTIONsem_wait() decrements (locks) the semaphore pointed to by sem. If the semaphores value is greater than zero, then thedecrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the callblocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signalhandler interrupts the call.sem_trywait() is the same as sem_wait(), except that if the decrement cannot be immediately performed, then call returnsan 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 callshould 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 isdefined 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, thensem_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 thevalue of abs_timeout. Furthermore, the validity of abs_timeout is not checked in this case.
RETURN VALUEAll of these functions return 0 on success; on error, the value of the semaphore is left unchanged, -1 is returned, anderrno is set to indicate the error.
ERRORSEINTR 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.ETIMEDOUTThe call timed out before the semaphore could be locked.int sem_trywait(sem_t * sem)功能sem_trywait()为sem_wait()的非阻塞版不进行等待 返回值如果信号量计数大于0则信号量立即减1并返回0否则立即返回-1errno置为EAGAIN 参数sem指向信号量结构的一个指针
NAMEsem_getvalue - get the value of a semaphore
SYNOPSIS#include semaphore.hint sem_getvalue(sem_t *sem, int *sval);Link with -pthread.
DESCRIPTIONsem_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 ofthe number of processes and threads currently blocked in sem_wait(3). Linux adopts the former behavior.
RETURN VALUEsem_getvalue() returns 0 on success; on error, -1 is returned and errno is set to indicate the error.
ERRORSEINVAL 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 itwith 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_sourcedistribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1utm_sourcedistribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1