Linux设备驱动第五章(并发和竞争)读书笔记(国外英文资料)
Linux设备驱动第五章(并发和竞争)读书笔记(国外英文资料)
The fifth chapter is concurrency and competition
Reference article: /u1/34474/showart.php? Id=408682?
5.3 flags and mutex
1) flag is the core of a single integer value. Combined with a pair of functions, also known as PV operations".
One wants to enter the critical section of the P process with the flag raised, if the flag value is greater than 0, this value decreased 1, and the process continues. If the flag is small than or equal to 0, indicating that the flag has been occupied by other processes, the process must wait until the flag is released.
Unlock the flag, flag is released, to complete the operation by calling the V V operation, increasing the value of the flag, and the flag can wake up waiting process.
2) mutex function is: mutual exclusion, to prevent multiple processes simultaneously in the same critical area. The mutex is initialized to 1 flags. Because that is initialized to 1, when the first process of P, 1>0, so the process can run at the same time, the flag value is 0, 1 decline, the second process to obtain the flag is used to access the critical section, but found that the flag value is 0, so the process can not run until the 2 release of the flag process 1.
5.3.1 implementation of the LINUX flag
1) to use the flag, must type contains . is related to the struct semaphore;
2) the flag: create a flag, then use sema_init to set it
Such as:
Struct semaphore *sem; / / define flag
Sema_init (SEM, Val); / / initialize Val to initialize the value of flag flag.
3) usually, flag is used to model the mutex. As a result, the kernel provides a series of macros to initialize.
DECLARE_MUTEX (name); / / initialize a mutex, a value of 1
DECLARE_MUTEX_LOCKED (name); / / initialize a mutex, a value of 0 is: flag after initialization is not available.
To use this flag any process must first unlock it again.
When we need to initialize at runtime (that is, dynamic creation), we use the:
Void init_MUTEX (struct semaphore