当前位置:网站首页>Semaphore function
Semaphore function
2022-06-25 14:59:00 【qq_ twenty-three million nine hundred and fifty-three thousand 】
Semaphore header file
#include<sys/types>
#include<sys/ipc.h>
#include<sys/sem.h>
semget
If the execution succeeds, a semaphore set identifier is obtained or a semaphore set object is created , Error return -1, Cause exists errno in .
int semget(key_t key,int nsems, int semflg)
Example :
semid = semget(key, 1, IPC_CREAT|0666)
Parameters key
You can customize the generation , Generally by key = ftok(".",1); produce
Parameters nsems
Is the number of semaphores in the semaphore set , This parameter is only valid when creating semaphores
Parameters semflg
Is the operation type of semaphore and the operation authority
IPC_CREAT: If the kernel has key values and key The same set of semaphores , Returns the identifier of the semaphore set ; If it doesn't exist , Create a new semaphore set
IPC_CREAT|IPC_EXCL: If the key values and... Do not exist in the kernel key Semaphore values with equal values , Then create a new message queue ; If it exists, an error is reported .
semop
Operation semaphore set
int semop( int semid, struct sembuf semoparray[], size_t nops );
void pKey(int semid)
{
struct sembuf sops;
sops.sem_num = 0;
sops.sem_op = -1;
sops.sem_flg = SEM_UNDO;
semop(semid,&sops,1);
}
void vKey(int semid)
{
struct sembuf sops;
sops.sem_num = 0;
sops.sem_op = 1;
sops.sem_flg = SEM_UNDO;
semop(semid,&sops,1);
}
Parameters semoparray
sem_num:
The number of operating signals in the signal set , The number of the first signal is 0
sem_op :
Positive numbers
Release the corresponding number of resources , take sem_op The value of is added to the value of the semaphore , If sem_flag It specifies SEM_UNDO( Restore ) Mark , Then the corresponding semaphore semadj The value will be subtracted sem_op Value .
0
The process block knows that the semaphore value is 0, When the semaphore value is 0 Go back to ; If the value of the semaphore is not 0, Of the corresponding semaphore semzcnt Add 1, The calling thread is blocked .
negative
Perform a wait operation on the semaphore , When the current value of the semaphore semval >= -sem_op when ,semval Subtract sem_op The absolute value of , Allocate a corresponding number of resources to the thread . If specified SEM_UNDO, Of the corresponding semaphore semadj Just add sem_op The absolute value of . When semval < -sem_op when , Of the corresponding semaphore semncnt Just add 1, Calling thread blocked , until semval >= -sem_op, When this condition is met , The calling thread is awakened , Perform the corresponding allocation operation , then semncnt subtract 1.
sem_flag: Attribute flags for semaphore operations , If 0, Indicates normal operation , If IPC_WAIT, Make the operation of semaphores non blocking . This flag is specified , The calling thread will not be blocked if the semaphore value does not meet the conditions , I'm going straight back -1, And will errno Set to EAGAIN. If SEM_UNDO, The semaphore adjustment value of the process will be maintained , So that the semaphore state can be restored at the end of the process .
The following explains several values related to a single semaphore :
semval: Current value of semaphore , It is mentioned in the structure of semaphores at the beginning of the article .
semncnt: wait for semval Change to more threads than the current value . It is mentioned in the structure of semaphores at the beginning of the article .
semzcnt: wait for semval Turn into 0 Number of threads for . It is mentioned in the structure of semaphores at the beginning of the article .
semadj: Specify the semaphore tuning value for a particular process . Only sembuf Structural sem_flag Designated as SEM_UNDO after ,semadj Will follow sem_op And update .
Make it simple : For a process , In the specified SEM_UNDO after , For semaphores semval The modification of values will reflect semadj On , When the process terminates , The kernel will be based on semadj Value , Restore the value before the semaphore .
struct sembuf sem_opt_wait[0] = {0, -1, SEM_UNDO};
struct sembuf sem_opt_wakeup1[1] = {0, 1, SEM_UNDO};
semctl
Information for controlling semaphores
int semctl(int _semid ,int _semnum,int _cmd ……);
The third parameter cmd
·IPC_STAT Read a set of semaphores semid_ds, And store it in semun Medium buf Parameters in .
·IPC_SET Set the data structure of the semaphore set semid_ds The elements in ipc_perm, Its value comes from semun Medium buf Parameters .
·IPC_RMID Remove semaphore set from memory .
·GETALL Used to read the values of all semaphores in the semaphore set .
·GETNCNT Returns the number of processes waiting for resources .
·GETPID Back to the last execution semop Process of operation PID.
·GETVAL Returns the value of a single semaphore in a semaphore set .
·GETZCNT Returns the number of processes waiting for fully idle resources .
·SETALL Set the value of all semaphores in the semaphore set .
·SETVAL Set the value of a single semaphore in the semaphore set .
边栏推荐
- Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?
- Biscuit distribution
- [deep learning] multi task learning of multiple datasets data sets missing labels
- Biscuit distribution
- 网上办理股票开户安全吗?
- 使用sphinx根据py源文件自动生成API文档
- 如何裁剪动图大小?试试这个在线照片裁剪工具
- [Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination
- Introduction to flexible array
- 14 -- validate palindrome string II
猜你喜欢

从408改考自主命题,211贵州大学考研改考

Source code analysis of synergetics and ntyco

电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备

Learning notes on February 18, 2022 (C language)

【中国海洋大学】考研初试复试资料分享

2022年广东高考分数线出炉,一个几家欢喜几家愁

How to crop GIF dynamic graph? Take this picture online clipping tool

One question per day,

QT loading third-party library basic operation

Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
随机推荐
JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
Usage of pure virtual functions
p1408
QT pop up open file dialog box QFileDialog
One question per day, a classic simulation question
Dynamic memory allocation
dmsetup命令
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination
Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
Is it safe to open an online stock account? Who knows
User defined data type - structure
JS capture, target, bubble phase
Custom structure type
【Try to Hack】vulnhub DC1
How to view the Chrome browser plug-in location
Qmake uses toplevel or topbuilddir
Source code analysis of zeromq lockless queue
Qlogsystem log system configuration use
Extend JS copy content to clipboard