System V Signal Volume
When referring to the POSIX semaphore, it refers to a two-value semaphore or a count semaphore, while the System V semaphore refers to the count semaphore set
Two value semaphore: Its value is 0 or 1, similar to a mutex, the resource is locked for 0, and the resource is available as 1
count Semaphore: The semaphore whose value is between 0 and a certain limit, the value of the semaphore is the number of available resources
count Semaphore set: one or more semaphores constitute a set, each element in the collection is a count semaphore (there is a limit on the number of semaphores per collection)
SEMID_DS structure:
The information structure maintained by the kernel for each semaphore set
#include <sys/sem.h>structsemid_ds{structIpc_perm Sem_perm;//access rights for current semaphore structSEM *sem_base;//internal data structure used by the kernel to maintain a set of values for a given semaphore ushortSem_nsems;//number of semaphores in the collectiontime_t Sem_otime;//last time the SEMOP () was executedtime_t Sem_ctime;//collection creation time or last Ipc_set time};structsem{ushort_t Semval; //Semaphore actual value ShortSempid;//process ID of the process that performed the last operation on the semaphore valueushort_t semncnt;//number of processes waiting for semaphore value to growushort_t semzcnt;//number of processes waiting for semaphore value to become 0};
Semget () function:
#include <sys/sem.h>// Create a semaphore set or access an existing semaphore set // successfully return a non-negative semaphore identifier, error 1 int int int oflag); // KEY:IPC Key // Nsems: Specifies the amount of semaphores in the collection // Oflag:sem_r (Read) and sem_a (modified) combinations can also be bitwise and ipc_creat or ipc_creat| IPC_EXCL
Semop () function:
#include <sys/sem.h>//operation of one or more semaphores in a semaphore set//successfully returned 0, error returned-1intSemopintSemid,structSEMBUF *opsptr, size_t nops);//Semid: Semaphore identifier returned by the Semget () function//Nops: Indicates the number of elements of the structure array that the opsptr points to//opsptr: Point to an array of the followingstructsembuf{ ShortSem_num;//the index value of the semaphore in the semaphore set: 0, 1, ..., nsems-1 (Sem_base[sem_num], specifying the operation of a particular semaphore) ShortSem_op;//Specify the operation for a specific semaphore ShortSEM_FLG;//operating signs, 0, ipc_nowait, Sem_undo};//The order of the elements within the SEMBUF structure is not guaranteed to be guaranteed to have the above three elements in the structure as described above//sembuf struct cannot be statically initialized (struct sembuf value = {0, 0, 0}//Error)//SEMBUF structure requires runtime initialization (struct SEMBUF value; value.sem_num = 0; ...)//Sem_op: Each specific operation is SEM_OP determined, it can be negative, 0, positive//when the sem_op is positive, the value is added to the Semval (current value of the semaphore), which corresponds to the resource that releases the semaphore control//If the Sem_undo flag is specified, subtract sem_op from the corresponding semaphore's Semadj (the specified semaphore is the adjusted value of the calling process)//Sem_op is 0, the caller waits for Semval to become 0, and if Semval is already 0, returns immediately//Sem_op is negative, the caller wants to wait for semval to become an absolute value greater than or equal to Sem_op, which corresponds to allocating resources
Semctl () function:
#include <sys/sem.h>//perform various control operations on a semaphore//successful return non-negative, error returned-1intSemctl (intSemid,intSemnum,intCmd.../*Union Semun Arg*/);//Semid: Identifies the set of semaphores whose operation is to be controlled//Semnum: Identifies a member within the semaphore set (Sem_base[sem_num])//cmd: The optional values are as follows (unless otherwise noted, the return value of the operation success function is 0 and the error is-1)//getval: Returns the current value of Semval as a function return value//Setval: Set Semval to Arg.val (if the operation is successful, then the Semadj of the corresponding semaphore is set to 0)//getpid: Returns the current value of Sempid as a function return value//getncnt: The current value of semncnt (the number of threads waiting for semval to become greater than its current value) is returned as a function return value//getzcnt: Returns the current value of semzcnt (the number of threads waiting for Semval to 0) as a function return value//GETALL: Returns the Semval value of each member in the specified semaphore set (these values are returned by the Arg.array pointer)//SETALL: Sets the Semval value of each member in the specified semaphore set (these values are passed through the Arg.array pointer)//Ipc_rmid: The set of semaphores specified by Semid is removed from the system//ipc_set: Sets Sem_perm.uid, Sem_perm.gid, sem_perm.mode in the SEMID_DS structure of the specified semaphore set, which are specified by the corresponding member in the structure pointed to by the ARG.BUF parameter//Ipc_stat: Returns the current SEMID_DS structure of the specified semaphore set (via Arg.buf)//arg: Optional parameter, depending on the cmd parameterUnion semun{intVal; structSemid_ds *buf; ushort*array;};
System V semaphore usage correlation function