interprocess Communication-Semaphore
1, why to use the semaphore
To prevent multiple programs from accessing a shared resource at the same time, there is a way to have only one critical section that executes thread access code at any one time (the critical section refers to the code that accesses the critical resource), and the semaphore provides such an access mechanism, allowing only one thread to access the critical section at a time , which means that the semaphore is used to coordinate the access of the process to shared resources, which means that the semaphore is used to coordinate the process access to the shared resources, where shared memory is implemented with semaphores.
2. How the Semaphore works
Since semaphores can only perform two operation waits and send signals, they are P (SV) operation, V (SV) operation.
P (SV) Operation: If the value of the SV is greater than 0. He will lose 1 if the value of the SV equals 0, it suspends execution of the process.
V (SV) Operation: If there are other processes suspended because of waiting for SV, let him run again, and if no process hangs up waiting for it, let him add 1.
For example, two processes share the semaphore SV, and once one of the processes performs a P (SV) operation, it will get the semaphore and can enter the critical section, reducing the SV by 1. The second process is prevented from entering the critical section because when it attempts to execute P (SV), the SV is 0, it is suspended to wait for the first process to leave the critical area and performs a V (SV) Release semaphore, and the second process can resume execution.
3. Linux semaphore mechanism
Linux provides a set of carefully configured semaphore interfaces to operate on semaphores. These functions are used to manipulate the semaphore of a group and are declared in sys/sem.h.
4, the use of signal volume
(1) Create a semaphore
The Semget function creates a semaphore set or accesses an existing semaphore set
int Semget (key_t key, int nsem, int oflag)
The return value is an integer called the semaphore identifier that the SEMOP and SEMCTL functions will use. the indicator of the successful return semaphore, failed to return 1
key: obtained by the Ftok () function,
Nsem: Create the number in the semaphore
Oflag
Ipc_creat: If there is no semaphore set with key values equal to key in the kernel, it is created, otherwise, returns the identifier of this semaphore set
IPC_EXCL: No meaning for use alone
Ipc_creat | IPC_EXCL: Creates a new semaphore set and returns the identifier of the semaphore set, otherwise returns-1.
(2) Open semaphore (PV operation to complete the semaphore)
Once a semaphore is opened with Semget, one or more of the semaphore operations are performed using SEMOP.
int semop (int semid, struct sembuf * opsptr, size_t Nops)
Semid: Semaphore Set Identifier
Nsops: The number of operational semaphores , that is, the number of SOPs structure variables, need to be greater than or equal to 1.
OPSPT: is a pointer to an array of semaphore operations represented by the SEMBUF structure
struct sembuf{ short sem_num; // unless a set of semaphores is used, it is 0 short sem_op; // semaphore the data that needs to be changed in a single operation, usually two number, // one is-1, that is, p (wait) operation, one is + 1, that is, V (send signal) operation short sem_flg; // is usually sem_undo, which causes the operating system to trace the signal and terminates when the process does not release the semaphore, // The operating system releases the Semaphore };
when the semaphore (SEMOP) is manipulated, the FLG can set the Sem_undo identity,Sem_undo is used to exit the modified semaphore in the process gracefully (call exit exit or main execution) or exit abnormally (such as segment exception, The semaphore is returned in addition to 0 exceptions, receiving a kill signal, and so on. After the process is Sem_undo, the value of the semaphore can be changed when the process exits, and the modified value is returned to the semaphore at the time of the process exit, and the signal is converted to the original value.
(3) operation of a signal on a specified signal set or signal set
int semctl (int semid,int semnum,int cmd,union semun Arg)
Semid: Semaphore Set Identifier
Semnum: Subscript on an array of semaphore sets, indicating a semaphore
The fourth parameter is optional, depending on the first signal (operand)
The parameter cmd specifies one of the following 10 commands that are executed on the Semid specified semaphore collection.
Ipc_stat reads the data structure of a semaphore set Semid_ds and stores it in the BUF parameter in Semun.
Ipc_set sets the element ipc_perm in the data structure Semid_ds of the semaphore set, whose value is taken from the BUF parameter in Semun.
Ipc_rmid removes the semaphore set from memory.
The GETALL is used to read the values of all semaphores in the semaphore set.
GETNCNT returns the number of processes that are waiting for a resource.
Getpid returns the PID of the last process that performed the SEMOP operation.
Getval returns the value of a single semaphore in the semaphore set.
GETZCNT returns the number of processes that are waiting for a fully idle resource.
SETALL sets the value of all semaphores in the semaphore set.
Setval sets the value of a single semaphore in the semaphore set.
5. Example
Signal Volume (semaphore)