当前位置:网站首页>15.10 the POSIX semaphore Unix environment programming chapter 15
15.10 the POSIX semaphore Unix environment programming chapter 15
2022-08-09 10:28:00 【Mary Soda Meatloaf】
POSIX semaphores come in two forms: named and unnamed.They differ in the form of creation and destruction, but otherwise work the same.
Unnamed semaphores exist only in memory and require that processes that can use the semaphore must have access to memory.This means they can only be applied to threads in the same process, or threads in different processes that have mapped the same memory contents into their address spaces.
Named semaphores can be accessed by name and thus can be used by threads in any process whose name is known.
We can call the sem_open function to create a new named semaphore or use an existing semaphore.
#include sem_t *sem_open(const char *name,int oflag,...)//Successfully returns the pointer of the semaphore, if error returns SEM_FAILED
When using an existing named semaphore, we only specify two parameters: the name of the semaphore and the 0 value of the oflag parameter.
When we specify the O_CREAT flag, we need to provide two additional parameters.The mode parameter specifies who can access the semaphore.
When creating a semaphore, the value parameter is used to specify the initial value of the semaphore.Its value is 0-SEM_VALUE_MAX.
If we want to ensure that we are creating a semaphore, we can set the oflag parameter to O_CREAT|O_EXCL.If the semaphore already exists, it will cause sem_open to fail.
In order to increase portability, certain rules must be followed when choosing semaphore names:
- The first character of the name should be a slash.
- Names should not contain card slashes to avoid implementation-defined behavior
- The maximum length of a semaphore name is implementation-defined.
If you want to operate on the semaphore, the sem_open function will return a semaphore pointer for us to pass to other semaphore functions, but when the semaphore operation is completed, you can call the sem_close function to release any semaphorerelated resources.
#include int sem_close(sem_t *sem);//successful return 0, error return -1
If the process exits without first calling sem_close, the kernel will automatically close any open semaphores.
The sem_unlink function deletes the semaphore name. If there is no open semaphore reference, the semaphore will be destroyed.Otherwise, destruction will be delayed until the last open reference is closed.
#include int sem_unlink(const char *name);//successful return 0, error return -1
You can use the sem_wait or sem_trywait functions to reduce the semaphore by 1
#include int sem_trywait(sem_t *sem);//When the semaphore is 0, it will not blockint sem_wait(sem_t *sem);//If the semaphore is 0, it will block, and it will return when the semaphore is successfully decremented by 1 or interrupted by the signal//Two functions, return 0 for success, -1 for error
Call the sem_timewait function to block for a certain period of time
#include #include int sem_timedwait(sem_t *restrict sem,const struct timespec *rstrict tsptr);//successful return 0, error return -1
When you want to give up waiting for a semaphore, you can specify an absolute time with the tsptr parameter.
Call the sem_post function to increase the semaphore value by 1.
#include semaphore.h>int sem_post(sem_t *sem);//Success returns 0, failure returns -1
When calling sem_post, if the process is blocked during the call to sem_wait, the process will be woken up and the semaphore count that will be incremented by 1 by sem_post will be decremented by 1 again by sem_wait.
Use the sem_init function to create an unnamed semaphore
#include int sem_init(sem_t *sem,int pshared,unsigned int value);//Success returns 0, failure returns -1
The pshared parameter indicates whether to use the semaphore in multiple processes.If so, set it to a non-zero value.The value parameter specifies the initial value of the semaphore.
When the use of the unnamed semaphore has been completed, the sem_destroy function can be called to discard it
#include int sem_destroy(sem_t *sem);//Success returns 0, failure returns -1
After calling sem_destroy, you cannot use any semaphore function with sem unless it is reinitialized by calling sem_init.
The sem_getvalue function can be used to retrieve the semaphore value
#include int sem_getvalue(sem_t restrict sem,int *restrict valp);//Success returns 0, failure returns -1
边栏推荐
猜你喜欢
RTP
拿下跨界C1轮投资,本土Tier 1高阶智能驾驶系统迅速“出圈”
分类预测 | MATLAB实现CNN-LSTM(卷积长短期记忆神经网络)多特征分类预测
需求侧电力负荷预测(Matlab代码实现)
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
相伴成长,彼此成就 用友U9 cloud做好制造业数智化升级的同路人
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
【报错记录】解决华擎J3455-ITX不插显示器无法开机的问题
随机推荐
上传张最近做的E2用的xmms的界面的截图
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
KMP& sunday
Loop nesting and basic operations on lists
unix系统编程 第十五章 15.2管道
[相机配置] 海康相机丢包配置环境
函数组件和类组件和dva视图更新问题
公里周日
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
阿里神作!吃透这份资料入厂率高达99%
使用.NET简单实现一个Redis的高性能克隆版(四、五)
通过Doc在MySQL数据库中建表
WUSTOJ:n个素数构成等差数列
1005 继续(3n+1)猜想 (25 分)
[贴装专题] 视觉贴装平台与贴装流程介绍
unix环境编程 第十四章 14.8 存储映射I/O
Technology Sharing | How to simulate real usage scenarios?mock technology to help you
需求侧电力负荷预测(Matlab代码实现)
可能95%的人还在犯的PyTorch错误
LM小型可编程控制器软件(基于CoDeSys)笔记二十六:plc的数据存储区(模拟量输入通道部分)