当前位置:网站首页>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.
边栏推荐
- 【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
- 3D printed this DuPont cable management artifact, and the desktop is no longer messy
- unix环境编程 第十五章 15.5FIFO
- 深度学习--生成对抗网络(Generative Adversarial Nets)
- Demand side power load forecasting (Matlab code implementation)
- RTP
- Nodejs服务端
- [贴装专题] 视觉贴装平台与贴装流程介绍
- OpenGL 2.0编程例子
- 学习NET-SNMP之二-----------VisualStudio编译Net-SNMP
猜你喜欢

Battery modeling, analysis and optimization (Matlab code implementation)

第二周作业

自启服务mock联调跨域问题

How tall is the B+ tree of the MySQL index?

【原创】JPA中@PrePersist和@PreUpdate的用法

相伴成长,彼此成就 用友U9 cloud做好制造业数智化升级的同路人

Redis + NodeJS 实现一个能处理海量数据的异步任务队列系统

使用.NET简单实现一个Redis的高性能克隆版(四、五)

深度学习--自编码器(AutoEncoder)

Transformer+Embedding+Self-Attention原理详解
随机推荐
UNIX Environment Programming Chapter 15 15.6 XSI IPC
[Halcon&定位] 解决Roi区域外的模板匹配成功
3D打印了这个杜邦线理线神器,从此桌面再也不乱了
MySQL备份与恢复
ESIM(Enhanced Sequential Inference Model)- 模型详解
Solve the ali cloud oss - the original 】 【 exe double-click response can't open, to provide a solution
笔记本电脑使用常见问题,持续更新
Redis + NodeJS 实现一个能处理海量数据的异步任务队列系统
可能95%的人还在犯的PyTorch错误
阿里神作!吃透这份资料入厂率高达99%
unix环境编程 第十五章 15.6 XSI IPC
踩坑scrollIntoView
Database connection operations for MySQL and MyEclipse
15.10 the POSIX semaphore Unix environment programming chapter 15
pytorch widedeep文档
xmms的歌词显示及音量控制OK
libavcodec.dll导致游戏不能运行及explorer关闭
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
xmms的均衡器试验成功 音效相当不错 比rockbox可能还要好
按键精灵之输出文本