1. Background: Some resources when multiple processes are simultaneously accessed, data confusion may occur
2. Definition: Semaphore signal semaphore. It is mainly used to protect critical resources (process mutex). The process can determine whether a critical resource can be accessed based on his decision. It can also be used for process synchronization.
Substance: A number
Action: Get and release. Use the value of the semaphore to determine whether it can be manipulated, greater than 0. 0 Not available. So the initial value is generally guaranteed to be 1 before the operation is created. You can use the SEMCTL function to get and set the initial value.
3. Classification:
(1) Two value semaphore: The value of the semaphore can only be 0 and 1
(2) Count semaphore: The value of the semaphore can take any non-negative
4. And the difference between documents
(1) Open semaphore to get identifier
(2) using identifiers to manipulate the semaphore
5. Key value: Similar to file name, to open a file you need to know her file name, so to open a semaphore to know the key value he belongs to.
The key value points to a semaphore collection.
Nature: It is a number that is available before the semaphore is opened.
Function: Indicates the semaphore.
Specify the key value:
(1) arbitrarily select a number
It may be used by other IPC objects, so it is invalid.
(2) Ftok function (file name (real number), Project ID)
The key values of the semaphore collection are constructed using the file name (the Linux kernel specifies the actual number) and the project ID. The key_t type is returned.
5. Function Learning:
8 . 1 Create/Open Semaphore collection
8 . 1.1 Function Names
Semget
8.1.2 function prototype
int Semget (key_t key, int nsems, int semflg);
8.1.3 function function
Gets the identifier of the semaphore collection
When the semaphore specified by key does not exist, and the SEMFLG contains Ipc_creat, a semaphore set is created
8.1.4 Owning header file
<sys/types.h>
<sys/ipc.h>
<sys/sem.h>
8.1.5 return value
Success: Semaphore Set Identifier
Failed:-1
8.1.6 parameter Description
Key : key value (corresponds to Semaphore set)
Nsems : the number of semaphores contained in the created Semaphore collection.
SEMFLG : mark, can take ipc_creat mark.
8.1.7 Sample Code
8 . 2 Operating signal Volume
8.2. 1 function Names
Semop
8.2.2 Function prototype
int semop (int semid, struct sembuf *sops, unsigned nsops);
8.2.3 function function
Operating Signal Volume
8.2.4 Owning header file
<sys/types.h>
<sys/ipc.h>
<sys/sem.h>
8.2.5 return value
Success: 0
failed: -1
8.2.6 Parameter Description
Semid:The marker for the Semaphore collection to manipulate (returned by Semget)
Sops:What to do (essentially an array, the length is determined by theNsopsSpecify)
unsigned short sem_num;indicate which semaphore to manipulate
Short sem_op;a positive number is a release, a negative number is
Short sem_flg; don't care
Nsops:how much semaphore to manipulate
6. Specific Operation
(0) Create a key value (all two processes must specify a key value, as long as you know the file name and Project ID.) )
(1) Create and open semaphore, return int (if it already exists, it will not be created again)
(2) Open file
(3) to get the semaphore, to get the failure, the wait is to use the operation Semaphore function. Note the definition of the SOPS structure.
(4) Access to files (resources)
(5) Release signal volume
(6) Close the file
Lesson 14th-Signal Mutex programming