Signal Volume Communication Module
semaphore function : The main role is to protect the street resources, that is, if the next year
Process requires a resource, the semaphore allows process a and process B to occupy
A certain amount of semaphore, each semaphore can only allow one process access, the semaphore is time
Limit, so that two processes have access to the same resource.
Classification:
Two value semaphore : The semaphore can only take 0 and 1, similar to the concept of mutual exclusion. When the resource is available
, the signal lights can be modified to become available. Two Mutual exclusion lock overview
The emphasis is on the process, which must be unlocked by the process after it has consumed the resources.
Count Semaphore : The value of the semaphore can be arbitrary non-negative. It's like I'm working in a factory,
Every time after work in the card, the first time the card is unsuccessful, you can not brush, in the back of the first let others
In the brush, and so other people brush well, you are in the brush, because everyone has a limited amount of time to swipe, can this
This avoids the problem of dead-lock lights.
Create Open
int Semget (key_t key, int nsems,int SEMFLG)
Key: Keys, the previous blog has mentioned, the first step to do.
Nsems: The number of sets of open or worn lights;
SEMFLG: Identity, similar to Message Queuing;
Operation
int semop (int semid, struct sembuf*sops, unsigned nsops)
Parameters:
Semid: The IP of the semaphore set
SOPs Operand, indicating what to manipulate
The number of elements of the array to which the nsops:sops points;
struct sembuf{
unsigned short sem_num; Gets the number of semaphores, the array below the table
Short Sem_op; Get or, release semaphore, 1 for release, 1 get
Short SEM_FLG; Flag, Ipc_nowait, returns an error immediately when the operand is not satisfied
Ipc_undo program whether normal or not normal to release the semaphore, to avoid deadlocks.
}
Signal volume notes for process communication