Linux Application Development-semaphore mutex programming
A corresponding function
1 Create/Open Semaphore collection
Function name Semget
function prototype int semget (key_t key, int nsems, int semflg)
function function
Gets the identifier of the semaphore collection
When the semaphore indicated by key does not exist, and the SEMFLG contains Ipc_creat, a set of semaphores is created
Owning header file
<sys/types.h> <sys/ipc.h> <sys/sem.h>
return value
The identifier of the successful return semaphore
Failed return-1
Parameter description
Key value, that is, the kernel number is combined with the project ID
The number of Nsems to generate the semaphore
SEMFLG logo, can go to ipc_creat
2 Operating Signal Volume
Function name Semop
function prototype int semop (int semid, struct sembuf *sops, unsigned nsops)
function function
The amount of semaphore in the set of Operation Semaphores
Owning header file
<sys/types.h><sys/ipc.h><sys/sem.h>
return value
Successful return 0
Failed return-1
Parameter description
Semid: identifier of the semaphore to be manipulated
SOPs: How many semaphores to operate
Nsops: What to do with the semaphore
3 Generating Key values
The key value is the number of values the file generates in the kernel combined with the project ID.
Key = key_t Ftok (char *fname, int id)
4 Control Signal Volume
Semctl (int semid, int semnum, int cmd ...)
Semid Signal Volume identifier
Semnum Semaphore number
CMD corresponding instructions into the getval,setval and so on
for a-process, B-process, both using semaphores to communicate
Linux application Development-semaphore mutex programming