First, the signal volume: (Data operation Lock) control inter-process mutual exclusion, synchronization, etc., coordinate multiple programs to access a shared resource at the same time.
How it works: The semaphore can only have two operation waits and operation, namely: P v operation, must be atomic operation.
P (SV): If the value of SV is greater than 0 minus one, if its value is zero, it hangs;
V (SV): If there is a suspended, resume operation, if not, add one.
Second, the function prototype:
int Semget (key_t key, int nsems,int SEMFLG);
int semop (int semid,struct sembuf *sops,size_t nsops);
int semctl (int semid, int semnum, int cmd, ...);
/*
* Semid: identifier of the semaphore set; Semnum: Number of semaphores;
* cmd: The command to execute, depending on the command, the function has three or four parameters (Union)
*/
Cmd:ipc_rmid, immediately delete the signal set, wake up the blocked process;
Cmd:setval, sets the value of a single semaphore in the semaphore set.
The Cmd:getall is used to read the values of all semaphores in the semaphore set.
Union Semun
{
int Val;
struct Semid_ds *buf;
unsigned short *array;
struct Seminfo *_buf;
};//users need to define their own claims
Third, the code implementation:
Iv. Results of implementation:
Signal Volume (SEM)