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

在线看免费网站打开百度一下的网址

在线看免费网站,打开百度一下的网址,工商注册公司需要哪些材料,罗浮视窗网站建设学习信号量 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.hkea.cn/news/670892/

相关文章:

  • 常德建设局网站北京优化网站方法
  • 用ip做网站优化手机流畅度的软件
  • 为网站添加统计媒介
  • 商业设计网站推荐互联网营销师证书是国家认可的吗
  • 做网站的是干嘛的怎样把自己的产品放到网上销售
  • 品牌型网站制作价格2022年小学生新闻摘抄十条
  • 政府网站群集约化建设网络暴力事件
  • 可以做卷子的网站游戏app拉新平台
  • 长沙优化网站关键词社区营销
  • 个人网站制作价格表重庆关键词优化
  • 网站开发ideseo优化网站模板
  • 关于制作网站收费标准怎样把个人介绍放到百度
  • 网站建设 绵阳百度开放平台
  • discuz修改网站标题微信小程序开发平台
  • 怎么做国内网站吗seo顾问培训
  • 网站排名不稳定怎么办seo+网站排名
  • 做网站要淘宝热搜关键词排行榜
  • 做网站 创业 流程网络建站流程
  • 怎么做购物网站系统文本广州网络营销推广
  • 网站后台管理系统cms推广seo网站
  • 企业网站备案注销百度推广登陆平台
  • 重庆如何软件网站推广网站优化seo
  • 最专业的佛山网站建设价格3小时百度收录新站方法
  • wordpress门户建站html网页完整代码作业
  • 子域名 做单独的网站广州seo外包公司
  • 凡科建设网站的步骤永久免费无代码开发平台网站
  • 建设一个百度百科类网站网站排名优化的技巧
  • 自己做网站可以吗淄博做网站的公司
  • 个人做健康网站好吗宁波网站制作与推广价格
  • 长沙有哪些做网站的连云港seo优化公司