当前位置: 首页 > news >正文

o2o商城上的二级网站每日一则小新闻

o2o商城上的二级网站,每日一则小新闻,广西省建设厅网站,北京住房和城乡建设部网站首页学习信号量 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.信号量使用场合 …

学习信号量 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:信号量的初始值

NAMEsem_init - initialize an unnamed semaphore
SYNOPSIS#include <semaphore.h>int 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 parent's 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.h>int 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.h>int sem_post(sem_t *sem);Link with -pthread.
DESCRIPTIONsem_post()  increments  (unlocks)  the semaphore pointed to by sem.  If the semaphore's 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.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
DESCRIPTIONsem_wait()  decrements  (locks)  the semaphore pointed to by sem.  If the semaphore's 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,否则立即返回-1,errno置为EAGAIN
参数sem:指向信号量结构的一个指针

NAMEsem_getvalue - get the value of a semaphore
SYNOPSIS#include <semaphore.h>int 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_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

http://www.15wanjia.com/news/8505.html

相关文章:

  • python做网站还是数据库怎么进行seo
  • 长沙专业网络推广公司郑州怎么优化网站排名靠前
  • 有ip怎么用自己的主机做网站世界球队最新排名榜
  • 新疆生产建设兵团棉麻公司网站seo运营经理
  • 河北网站建设多少钱关于市场营销的培训课程
  • 其他公司盗用公司名做网站网址域名查询
  • 台州网站建设开发东莞网站关键词优化排名
  • 故宫文创产品搜索引擎优化论文3000字
  • 网站打开速度优化凡科网免费建站官网
  • 企业做外贸网站常用术语网络营销和网站推广的区别
  • 怎么在工商网站做实名认证seo研究中心怎么样
  • 什么是网络营销?南昌seo计费管理
  • 响应式网站一般怎么设计网络推广方法
  • 南昌网站建设价格百度指数搜索
  • jsp做网站组件湖南正规seo优化报价
  • 做证明图片的网站免费发布信息网站大全
  • 做影视后期应该关注哪些网站百度搜索引擎使用技巧
  • 支付宝接口 网站备案网络营销服务策略
  • 已备案网站外贸推广公司
  • 四川省建设网站电子签章b站视频推广网站动漫
  • 做网站的客服回访话术seo优化推广多少钱
  • 网站挂百度广告网站模板怎么建站
  • 做网站必须要购买域名怎么提交网址让百度收录
  • 网站建设的公司前景百度提问登陆入口
  • 免费的中文logo网站b站推广网站2024年
  • 网页设计公司网站设计十大接单推广app平台
  • 网站建设影音先锋网址网站页面优化包括
  • 少儿类网站怎么做灰色推广引流联系方式
  • 桥西区网站建设湖北网络推广有限公司
  • 更换网站logoseo公司北京