当前位置:网站首页>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 .
边栏推荐
猜你喜欢

JGG | overview of duhuilong group of Hebei University Research on plant pan genomics

【Try to Hack】vulhub靶场搭建

Learning notes on February 8, 2022 (C language)

Introduction to flexible array

Character encoding minutes

QT loading third-party library basic operation

QQ情话糖果情话内容获取并保存

Flexible layout (display:flex;) Attribute details

Std:: vector minutes

多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
随机推荐
Async await to achieve sleep waiting effect
【深度学习】多标签学习
挖财是正规的吗?股票开户安全吗?
Add the resources directory under test in idea
多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
Usage of qlist
分饼干问题
About the problem of kicad stuck in win10 version, version 6 x
Jaspersoft studio installation
QT inline dialog
Flexible layout (display:flex;) Attribute details
【Try to Hack】vulnhub DC1
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination
Is it safe to open a stock account online?
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
What moment makes you think there is a bug in the world?
如何裁剪动图大小?试试这个在线照片裁剪工具
One question per day,
Jaspersoft studio adding MySQL database configuration
The best time to buy and sell stocks