当前位置:网站首页>15.8 the semaphore Unix environment programming chapter 15
15.8 the semaphore Unix environment programming chapter 15
2022-08-09 10:29:00 【Mary Soda Meatloaf】
A semaphore, unlike IPC, is a counter that provides multiple processes with access to a shared data object.
In order to obtain a shared resource, a process needs to do the following:
- Test the semaphore that controls the resource.
- If the value of this semaphore is positive, the process can use the resource, in which case the process will decrement the semaphore value by 1, indicating that it uses one resource unit.
- If the value of this semaphore is 0, the process goes to sleep until the value of the semaphore is greater than 0.
When a process is not using a shared resource controlled by a semaphore, the semaphore value is incremented by 1.If any processes are sleeping waiting for this semaphore, wake them up.The increment and decrement of the semaphore value is an atomic operation.
A common form of semaphore is called a binary semaphore, which controls a single resource and has an initial value of 1.
When we want to use XSI semaphore, we first need to get a semaphore ID by calling the function semget.
#include int semget(key_t key,int nsems,int flag);//Successful return semaphore ID, error return -1
nsems is the number of semaphores in this collection, if creating a new collection, nsems must be specified.If referring to an existing collection (a client process), specify 0 for nsems.
The smectl function includes a variety of semaphore operations
#include int semctl(int semid,int semnum,int cmd,...)
The fourth parameter is optional, its use depends on the command requested, if it is used, its type is senum, which is a union of several command-specific parameters:
union semun{int val; /*for SETVAL*/struct semid_ds *buf; /*for IPC_STAT and IPC_SET*/unsigned short *array;/*for GETALL and SETALL*/}
The function semop automatically executes the operation data on the semaphore collection
#include int semop(int semid,struct sembuf semoparray[],size_t nops);//successful return 0, error return -1
The parameter semoparray is a pointer to a semaphore operation array represented by a sembuf structure:
struct sembuf{unsigned short sem_num;short sem_op;short sem_flg;}
The parameter nops specifies the number of operations in the array.
边栏推荐
- 如何快速打通镜像发布流程?
- 15.10 the POSIX semaphore Unix environment programming chapter 15
- Technology Sharing | How to simulate real usage scenarios?mock technology to help you
- numpy的ndarray取数操作
- 在犹豫中度过了老多天,今天的工作时记录
- OneNote 教程,如何在 OneNote 中搜索和查找笔记?
- 通过Doc在MySQL数据库中建表
- 【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
- 关于页面初始化
- 基本运算符
猜你喜欢
Qt 国际化翻译
机器学习--线性回归(Linear Regression)
【报错记录】解决华擎J3455-ITX不插显示器无法开机的问题
How tall is the B+ tree of the MySQL index?
Loop nesting and basic operations on lists
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
OneNote 教程,如何在 OneNote 中搜索和查找笔记?
3D printed this DuPont cable management artifact, and the desktop is no longer messy
Transformer+Embedding+Self-Attention原理详解
今天做了手机播放器的均衡器
随机推荐
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
cesium加载地图
京东物流与五菱将开发联名版定制产品
Battery modeling, analysis and optimization (Matlab code implementation)
深度学习--自编码器(AutoEncoder)
Electron application development best practices
pytorch widedeep文档
Loop nesting and basic operations on lists
[贴装专题] 视觉贴装平台与贴装流程介绍
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
GeoScene Pro 2.1下载地址与安装基本要求
Oracle数据库常用函数总结
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
Cpolar内网穿透的面板功能介绍
开源SPL,WebService/Restful广泛应用于程序间通讯,如微服务、数据交换、公共或私有的数据服务等。
StratoVirt 中的虚拟网卡是如何实现的?
【 size_t is unsigned integer (1 > 10) - > 1.
hover内部指定子类的样式
unix环境编程 第十五章 15.9 共享存储
百度云大文件网页直接下载