Summary of linux Device Drivers (iv): 3. preemption and context switching

Source: Internet
Author: User

Bytes

The previous section introduced some basic concepts of process scheduling, and briefly introduced how to assign the priority and time slice of processes to CFS scheduling without preemption. However, the kernel does not simply allocate time slices for processes to run.

Bytes

1. What is context switching?

Context switching refers to switching from an executable process to another executable process. In the Linux kernel, context switching can be performed in two ways: the first is that the process voluntarily gives up the CPU, and such an operation becomes "concession ". The second is that the kernel scheduler determines the running time of the process, and forces the process to be suspended when the running time ends (such as the time slice is exhausted) or when a high-priority process needs to be switched. Such an operation is called "preemption ".

Preemption is a concept I have always misunderstood. I always thought that preemption is a process forced to switch to another process. I recently learned that the kernel is being preemptible, not a process.

There are two types of preemption: User preemption and kernel preemption. The following is a one-to-one introduction.

Bytes

Ii. User Preemption

It is unwise to manually discard the CPU by each process. Therefore, the kernel uses the need_resched flag (in the thread_info structure of each process) to indicate whether the process needs to be scheduled. When the time slice of a process is exhausted, or a process with a higher priority enters the executable queue, the need_reched mark corresponding to the currently running process is set.

When the kernel is about to return to the user space, if the need_resched flag is set, the kernel will call the scheduler before continuing to execute the original process. In this case, user preemption will occur.

Note that a user preemptible occurs before the user space is returned. The kernel calls the scheduler and selects a more suitable process to run (such as a high priority ), of course, it can also be the original program.

In general, user preemption occurs in the following situations:

1. Return the user space from the system call.

2. Return the user space from the interrupt handler.

Similar to life, it is easy to understand. If someone is occupying a restroom to unlock a big hand, someone may need to clear a small hand when they come in, or the time required to allow him to undo the big hand is exhausted, the man in charge of the restroom will make a mark. When the big-handed man rushed to the toilet, the man in the bathroom saw his mark and immediately rushed out the man in the big-handed man, give the restroom To the unhandled man.

User preemption is not a new feature of the 2.6 kernel, but a process scheduling policy. After the kernel returns to the user space, each process has an independent 4G virtual space. At this time, process scheduling does not compete for kernel resources.

Bytes

Iii. kernel preemption

Kernel preemption is a new feature of the 2.6 kernel. In the previous kernel, the Scheduler cannot schedule processes running in the kernel, even if the time slice is used up, scheduling can be performed only when the process returns to the user space or is blocked.

In the 2.6 kernel, the kernel provides the opportunity for high-priority processes to seize processes running in the kernel. However, for multiple processes running at the same time, the resources in the kernel space are shared. Therefore, when the kernel is preemptible and scheduling processes, it is necessary to ensure that scheduling is safe. The so-called security means that the scheduling of new processes will not lead to confusion of shared resources. This is the kernel synchronization to be discussed in the next section.

Kernel processes can call functions to prohibit kernel preemption. During this period of time, preemption is not allowed. This is also a way to protect Kernel shared resources.

Therefore, kernel preemption occurs when:

1. When the terminal processing program is about to be executed, before returning to the kernel space.

2. Execute the code that can be preemptible.

3. After the kernel process gets out of the CPU, such as blocking.

I will not mention the examples in my life. It is really disgusting to describe the process in half, and I think of myself if I am interested.

Bytes

Iv. Summary

Today, I introduced the concept of preemption, which is my understanding and cannot be sure to be correct. At the same time, I compressed the long introduction in the book into just a few words, there must be many shortcomings. I hope you can correct them.

The preemption described in many books is mostly multi-kernel preemption, not to mention user preemption. User preemption can be understood as a process scheduling method.

Because of the emergence of kernel preemption, it is particularly important to use shared resources to protect the kernel. This is because all processes are running in the kernel, and the resources of another process will be changed without any worries, the following section describes how to implement kernel resource protection.

At the same time, it should be emphasized that the introduction of process scheduling does not involve interruptions. Generally, as long as the interruption occurs, the kernel will immediately respond, regardless of whether the process is in the kernel or user space, therefore, this is also part of kernel resource protection.

Bytes

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.