LINUX study notes 16-5 Semaphores for inter-process communication
Source: Internet
Author: User
A) role: protect critical resources and control processes. When you access a critical resource, first view the semaphore. if the semaphore is 1, the resource is empty, then you can access the critical resource. At the same time, the semaphore-1; 1. ESSENCE: it is an integer with the initial value 1; 2. two-value signal lamp: The value can only be 0 or 1; a maximum... information & a): protects critical resources and controls processes. When you access a critical resource, first view the semaphore. if the semaphore is 1, the resource is empty, then you can access the critical resource. At the same time, the semaphore-1;
1. essence: an integer with an initial value of 1;
2. two-value signal light: the value can only be 0 or 1; a maximum of one process can be accessed.
3. count signal lamp: The value can be any non-negative value, that is, multiple processes can be allowed to access
B) enable semaphores: int semget (key_t key, int nsems, int senflg)
1. # include , # Include , # Include
2. Key: Key value, obtained by ftok
3. Nsems: number of signals that will be contained in the create or enable traffic signal set
4. Semflg: flag, same as Message Queue
C) operation: int semop (int semid, struct sembuf * sops, unsigned nsops)
1. function: controls semaphores
2. Semid: the ID of the semaphore set
3. Sops: Operation array, indicating the operation to be performed
A) Struct sembuf
{Unsigned short sem_num;: indicates the number of semaphores.
Short sem_op;: 1 indicates release,-1 indicates get
Short sem_flg:}: Flag
1. IPC_NOWAIT:
2. IPC_UNDO: the semaphore is released when the program ends. Prevent resources from being locked forever
4. Nsops: number of elements in the array to which sops points
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service