The amount of signal, read and write signal of lock mechanism in Linux kernel
In the last blog post, I analyzed the contents of memory barrier, read-write spin lock and sequential lock, and this blog post will focus on the content of Semaphore and read/write signal volume.
six, the signal volume
As for the content of the semaphore, it is actually a concept similar to a spin lock, where only the process that gets the semaphore can execute the critical section of the code, except that when the semaphore is not acquired, the process does not spin in place and goes into a dormant wait state. It is defined in the Include\linux\semaphore.h file, as shown in struct 6.1. The Count variable is counted to protect the count variable by using the lock variable, while wait_list is the waiting queue for the process maintenance of the request semaphore.
Let's start by looking at how it's used, first defining a semaphore, and then initializing the semaphore, which consists of two methods. as shown in 6.2. Method 1: Simple initialization, define the number of semaphores to be determined by Val, in fact, the Val value is assigned to the semaphore structure of the count variable; method 2 is to directly set the count value in the struct body to 1, the semaphore can be used to implement the mutex between processes. Note: For the initialization function of the semaphore the latest version of Linux changes, the Linux version used in this article does not exist such as Init_mutex and init_mutex_locked initialization functions, but also changed the name, etc. This is what readers need to read, so I recommend that you try to use the sema_init (struct semaphore *sem, int val) function in programming later when you need a semaphore, because the function has not changed so far.
Figure 6.2 The initial function of the semaphore
Below we discuss how to get the semaphore, it mainly consists of three functions, the first function indicates that the process will hibernate when the signal request is not reached, and for the second function, it means that if the process can be interrupted by a signal after it has entered sleep because it has not received a semaphore, the signal referred to here is the signal of interprocess communication, such as our Ctrl + C, but this time the return value of this function is not 0; the third function indicates that the semaphore will return immediately, whether or not it is obtained, but the return value will depend on the success of the application, and this function will not cause sleep. The last up function, which is well understood, is the process of releasing the semaphore, which in turn wakes up the waiting semaphore in the queue. Function 6.3 is shown in general.
Figure 6.3 Acquiring and releasing functions of the semaphore
Next I will give a few examples, in turn 6.4, figure 6.5 shows. The example of Figure 6.4 still implements a device that can only be opened by a single process. The example is very simple, here is no longer elaborate, mainly shows how to use the semaphore. 6.4 shows.
Figure 6.4 Example of use of semaphores
6.5 implementation is a synchronization problem between processes. In fact, when the initial value of the semaphore is 0, it is possible to synchronize. Positive 6.5 is shown generally, for the execution unit A, if the execution unit B does not execute the UP function, the execution unit A will sleep because the application is not process, until the up function is called, so before executing code b must wait until the execution unit b Finish executing code c. This content is believed to be mentioned in the operating system course.
Figure 6.5 Semaphore Implementation synchronization example
After discussing the examples, I believe readers have a better understanding of the use of semaphores. Let's take a brief discussion of its implementation mechanism. By understanding the structure of the semaphore, it can be found that in addition to using the spin lock mechanism in the structure, it is the change in the Count value (as previously mentioned). It's source code 6.6, as shown in Figure 6.7.
Figure 6.6 semaphore down function core source code Figure 6.7 semaphore up function kernel source
From the source code can see the signal volume using spin lock correlation function to achieve the protection of the count variable, by judging whether the variable is greater than 0 and self-increment to achieve the signal volume application and release. Also because of this, the semaphore can implement the synchronization mechanism.
In addition, about the implementation of the __down and __UP functions in the source code , they are actually the list queue (increment, delete operation) of the process that maintains the request semaphore, and some information about the process scheduling (timeout mechanism ). This enables the process to hibernate. As a result of the large amount of source code, the content involved is also more, so there is no further discussion, interested in the relevant information can be consulted.
There is one more thing to mention about the semaphore, and we know the process level of the semaphore, so the use of it must be when it takes a long time to take up resources. This point in the use of the use of semaphores need to focus on, on the contrary, it is easy to affect the performance of the program. OK, so far, the content of the semaphore is discussed here.
Vii. Read and write signal volume
Next I will discuss the content of reading and writing signals, this part is a more difficult part, need to analyze more source code. Again, we first see what it can do: the relationship between read and write semaphores and semaphores is like the relationship between spin lock and read-write spin lock, which allows N read operations to access shared resources at the same time, but can only have one write operation. It is defined in arch\x86\include\asm\rwsem.h and kernel\rwsem.c. The definition of its structure is shown in7.1. Obviously, its structure definition and the semaphore structure definition are the same.
Figure 7.1 Structure implementation of the Read and write signal volume
Then understand its specific use,7.2 of the functions shown. Similar to other locking mechanisms, the interface functions it provides are simpler, including the functions that these functions can achieve.
Figure 7.2 Interface functions for reading and writing semaphores
After understanding the definition and use of Read and write semaphores, the next reader will discuss the specific source code implementation. Its specific implementation is the implementation of the Assembly, compared around, need to cooperate with the source code to one by one, the source order 7.3 to Figure 7.8. For ease of analysis, the following source only gives the most critical content.
For the content shown in Figure 7.3 is mainly read and write semaphore in the need to use some of the macro definitions, why the value of the author does not understand, follow-up study in depth may have some experience.
Figure 7.3 The kernel source of the read-write spin lock
Figure 7.4 The kernel source of the read-write spin lock
Figure 7.5 The kernel source of the read-write spin lock
Figure 7.4 and the same test instructions to detect count variable value to achieve. The content of the successful application is well understood, in which the xadd instruction represents the exchange of the original operand and the value of the purpose operation, and then the sum is saved into the target operand. Once the read-write semaphore request failed, you need to jump to 7.6, figure 7.6, the figure 7.7 in the source code shown in not much content, and finally jump to rwsem_down_read_failed and rwsem_down_write_ In the failed function, the register that changes the corresponding storage variable is saved by compiling the source code, and then returned to c codes, rwsem_down_ The write_failed function has not been studied thoroughly, so there is no further study.
At this point, the content of the reading and writing signal is basically ended, indeed due to the author's limited ability, the content of reading and writing semaphores and Ben Boven series content, readers can further study, and welcome the discussion. Progress together.
Figure 7.6 Read and write spin lock kernel source code Figure 7.7 Read and write spin lock kernel source code
Figure 7.8 The kernel source of the read-write spin lock
Due to the limitations of the article, this blog post to this end, the following will give the "big talk Linux kernel lock mechanism of completion, mutual exclusion," interested readers can continue to read the latter post. Due to the author's level limit, there are inevitably mistakes in the blog, readers are welcome to point out that we discuss each other and common progress.
Reprint Please specify source: http://blog.sina.com.cn/huangjiadong19880706