1、Linux设备驱动第五章并发和竞争读书笔记Linux device drivers fifth chapters concurrency and compeLinux设备驱动第五章(并发和竞争)读书笔记(Linux device drivers, fifth chapters (concurrency and competition), reading notes)The fifth chapter is concurrency and competitionReference article: Id=408682?5.3 flags and mutex1) flag is the core
2、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, in
3、dicating 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: mu
4、tual 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, 10, so the process can run at the same time, the flag value is 0, 1 decline, the second process to obtain the fl
5、ag 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 flag1) to use the flag, must type contains . is related to the struct semaphore;2) the flag: create a flag, then use s
6、ema_init to set itSuch as:Struct semaphore *sem; / / define flagSema_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
7、 value of 1DECLARE_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 *sem); /
8、 / initialize a mutex, a value of 1Void init_MUTEX_LOCKED (struct semaphore *sem); initializes a mutex with the value of 0For example, we want to create a mutex that is initialized to 0:Struct semaphore *sem; / / define flagECLARE_MUTEX_LOCKED (SEM); / / initialize flag value is 0.The above is the i
9、nitialization flag, after initialization of course we must use the flag, or create it what is the significance?4) flag acquisition and release (can also be a mutex, because basically all flags are used to LINUX mutex)Get the flag also known as P, also called down. Down literally means drop. The flag
10、 is minus 1.The P function is as follows:Void down (struct semaphore *sem); / * is not recommended, will establish not kill process * /Int down_interruptible (struct semaphore *sem); / * recommended the use of down_interruptible need to be careful, if the operation is interrupted, the function retur
11、ns a nonzero value, and it will not have the call signal. The proper use of down_interruptible requires always checking the return value and responding accordingly. * /Int down_trylock (struct semaphore *sem); / * with _trylock never sleeps, if the signal is not available in the call, will return a
12、nonzero value. * /We usually use the down_interruptible function. Recommended drops in the book.Once the flag, the flag of the obtaining process can access the critical area to protect the flag. What do you do when you use it? Of course is to control the discarded in the critical region, is also the
13、 release flag.The release flag also known as V, also called UP up, literally rise.The flag is 1. (down and up description of the image of the flag operation).The V function is as follows:Void up (struct, semaphore, *sem);Once called UP, will not have the flag in the process.5) use flags are easy to
14、make mistakesGet the process to release the flag flag using the call to UP, but not many call UP, that is: a down for a UP.In the hold flag when an error is encountered, we must be in the return (error state) is to call UP release flag, otherwise the critical region has been the process of possessio
15、n, but perhaps the process has been kill, and the other to the use of critical areas will be due process has not been suspended critical region.5.3.2. in scull using the flagThe key to the correct use of the lock primitive is to specify exactly what resources to protect and confirm that each access
16、to these resources uses the correct locking methodFirst of all, look at a scull structure:Struct scull_dev Struct scull_qset *data Pointer to first quantum set; / * * /Int quantum the current quantum size; / * * /Int qset the current array size; / * * /Unsigned long size amount of data stored here;
17、/ * * /Unsigned int access_key used by sculluid and scullpriv; / * * /Struct semaphore SEM mutual exclusion semaphore; / * * /Struct CDEV CDEV Char device structure; / * * /;Struct semaphore SEM is our flag, and this structure is the object we want to protect the.Flag must be initialized before use.
18、 Scull this initialization at load time, in this cycle:For (I = 0; I sem)Return -ERESTARTSYS;Note that the return value of the examination of the down_interruptible; if it returns non-zero, the operation was interrupted. Usually do in this case is to return to -ERESTARTSYS. to see the return value,
19、the kernel or restart the call from the top or return the error to the user. If you return -ERESTARTSYS, you must first restore any user visible has been done to change, to ensure that when the right things happen again when the system call. If you cannot recover in this way, you should be back for
20、-EINTR.When we finished using the critical area, the wirte function must release the flag,Whether it succeeds or fails when we use the write function, for example, kmalloc fails to allocate memory, and copy_form_uesr fails from user space copy data. Remember: be sure to release the flag.Code:Out:Up
21、(&dev-sem);Return retval;When you encounter a different error, use the goto statement to jump to out.Also, you must make sure that you do not access the scull_dev structure when you dont have semaphores.5.3.3 reader / writer flagFlag for all caller exclusive, but sometimes we can think: what is the
22、flag? In general, we used in the access to the critical section, because if there is no flag, can process 1 has just completed the revision of a process variable, 2 and modified the same variable, leading to the later data on the front of the data to cover. Just like the examples in 5.1. scull. But
23、when we read only, we only allow one process to read, which makes it inefficient. Therefore, the reader / writer flag.The reader / write flag: we can realize the concurrent read exclusive write, but only. Improved efficiency. The read-only tasks can work in parallel without waiting for other readers
24、 to exit the critical area1) use rwsem to include .2) initialize a rwsem.Void init_rwsem (struct, rw_semaphore, *sem);3) function as follows:The interface that requires read-only access to the code is:Void down_read (struct, rw_semaphore, *sem);Int down_read_trylock (struct, rw_semaphore, *sem);Void
25、 up_read (struct, rw_semaphore, *sem);Provides read-only access to protected resources of the down_read call can access concurrently with other readers. Note that the down_read may be set to the calling process can not be interrupted sleep. If the down_read_trylock is not waiting for the read access
26、 is not available; if allowed to access it returns non-zero, otherwise it is 0. down_read_trylock convention different from most of the kernel function, a return value of 0 indicates success. The use of a down_read to obtain the rwsem must eventually use up_read release.The readers interface is simi
27、lar:Void down_write (struct, rw_semaphore, *sem);Int down_write_trylock (struct, rw_semaphore, *sem);Void up_write (struct, rw_semaphore, *sem);Void downgrade_write (struct, rw_semaphore, *sem);Down_write, down_write_trylock, and up_write all like their readers corresponding part, besides, of course
28、, they provide write access. If you are in such a situation, the need for a write lock to do a quick change, then read-only access for a long time, you can use downgrade_write once you have completed the change in allow other readers to enter.4) rwsem uses relatively little in driving, but sometimes they are useful. Rwsem allows multiple readers to hold the flag, and write a priority, when a writer trying to get the flag, it does not allow readers to enter until the write completes the work. But it can lead
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1