Torture the soul of scheduling and switching to see if the operating system course you studied in university has learned the true skills of the teacher? If you don’t answer correctly, it means that you sleep, skip classes, speculate in stocks, play games and chat with female classmates in class
Doubts about scheduling and switching
Many friends asked Uncle Ben about scheduling and switching. According to FAE, he didn’t understand scheduling and switching thoroughly and was in a state of understanding. Even if I have learned the course of operating system in the University, I just learn some theory, but I haven’t learned the real ability of the teacher. Uncle Ben specially read several foreign classic textbooks. Some books simply mentioned it, and some seemed not to talk about switching at all, such as modern operating system.
For example, the book “in-depth understanding of computer systems” is simple and well summarized, but most people will be dizzy, and uncle stupid will also be dizzy.
Uncle stupid was bored. He also went to station B to watch several classic operating system courses at home and abroad, such as MIT, Caltech, HMC and other famous schools. These courses are very good for beginners.
California Institute of Technology
Courses at HMC College
OS course of a 985 University in China
The above courses are very, very excellent courses, and the theoretical knowledge is very good. However, if I were a student, I would still be confused about scheduling and switching, which may be the reason why Uncle stupid is stupid. The soul torture 16 mentioned below needs further exploration. To deeply understand and comprehend, the better way is:
Practice more and use the Linux kernel to debug and summarize more. You can use the RLK kernel compiled by o0 provided by run bar,
Listen to stupid FAE and watch uncle stupid’s video
Write an OS by yourself, such as stupid OS.
Scheduling and switching of soul torture – 16 questions
If you can understand the soul torture below, you really understand the scheduling and switching.
What on earth does the dispatcher want to do?
What is the timing of scheduling? When will the operating system schedule?
How to reasonably select the next process?
What is a process context? What does the process context contain?
Where is the process context saved?
What is an interruption scene? What needs to be saved at the interruption site?
Where is the interruption site kept?
What exactly needs to be switched during process switching?
From the perspective of CPU, during process switching, the CPU will distinguish who is prev process and who is next process?
Assuming that both the next process and the prev process are user processes, what is the next statement executed by the next process after the prev process switches to the next process? Is it the instruction that the next process is interrupted in user space?
Suppose a clock interrupt occurs while the prev process is executing, and then a process switch occurs to switch to the next process. When will the interrupt site of this clock interrupt be restored?
Assuming that the prev process switches under the clock interrupt drive and selects the next process as the newly created process, where does the newly created process start to execute?
Next, since the clock interrupt processing is carried out under the off interrupt, if the newly created process has been executing in the loop, does the system have no way to respond to the clock interrupt again, resulting in the system running the newly created process all the time?
In the interrupt handling function, can I directly call the schedule() function? Why?
Xiao Ming is in raw_ local_ irq_ After the disable () function, the schedule () function is called directly. If the next process selected by the scheduler is a loop execution process, does the system fail to respond to the clock interrupt and become paralyzed?
Why switch_ The to() function has three parameters? Prev and next are enough. Why do you need last?
Process scheduling and switching design to process management, process switching, context, interrupt, stack, system call and other knowledge. It’s hard to understand just switching.
We can start with a small question:
Assuming that the Linux kernel has only three kernel threads, thread 0 creates kernel thread 1 and kernel thread 2,
They will never quit. When the system clock interrupt arrives, the clock interrupt processing function will check whether any process needs to be scheduled. When scheduling is needed, the scheduler will select thread 1 or thread 2 to run.
Assuming that process 0 runs first, what happens in this scenario?
This is an interesting question, which involves a series of knowledge points, such as the implementation mechanism of the scheduler, interrupt processing, kernel preemption, how the newly created process is scheduled, process switching and so on. Only when we understand all these knowledge points can we really understand this problem.
Let’s go back to the question just now. The main steps of this scenario are as follows.
The first to appear is thread 0. We all know that in the Linux kernel, thread 0 is the thread that runs when the system starts. And start_ Kernel () runs in thread 0. Thread 0 created kernel thread 1 and kernel thread 2.
Function calling relationship: start_ kernel()->kernel_ thread()->_ do_ fork()。
In_ do_ The fork () function creates a new thread and adds it to the scheduler’s ready queue.
After creating kernel thread 1 and kernel thread 2, thread 0 enters the while loop. Thread 0 will not exit. It is waiting to be dispatched.
2. The second thing to appear is: clock interrupt. Clock interrupt is a very important thing in the system. The processor uses clock timer to provide system pulse periodically. Clock interrupt is also a kind of ordinary peripheral interrupt. The scheduler uses clock interrupt to regularly detect whether the currently running thread needs to be scheduled.
Function call relationship:
When the clock interrupt detects that the current thread needs to be scheduled, set need_ The resched flag bit.
In check_ preempt_ In the tick() function.
When the clock interrupt returns, the scheduling is determined according to whether the Linux kernel supports kernel preemption. It is discussed in two cases below.
Kernel that supports kernel preemption: when an interrupt occurs in the kernel state, check the need of the current thread when it returns_ Whether the resched flag bit is set. If it is set, it indicates that the current thread needs to be scheduled.
Check whether the kernel does not support interrupt scheduling when the kernel does not need to return.
In the Linux kernel that does not support kernel preemption, even the need of thread 0_ When the position of the resched flag is set, the Linux kernel will still not schedule kernel thread 1 or kernel thread 2 to run. Only when the interrupt in user status returns, or when the system call returns to user space, will it be checked whether scheduling is required. For example, the processing flow of this figure is as follows.
Clock interrupt occurs. When the clock interrupt is triggered, the current process (thread) may execute in user mode or kernel mode. When an interrupt occurs when the process is running in user mode, it will enter el0 of the exception vector table_ In the IRQ assembly function, when the process is interrupted in the kernel state, it will enter el1 of the exception vector table_ IRQ assembly function. In this scenario, because all three threads are kernel threads, the clock interrupt can only jump to el1_ IRQ assembly function. When entering the interrupt, the CPU will automatically close the interrupt.
At el1_ IRQ assembler function will first save the interrupt field (also known as interrupt context) to the stack of the current process. The Linux kernel uses Pt_ Regs data structure to implement a stack frame, which is used to save the interrupt field (this section is called pt_regs stack frame).
Interrupt processing process, including interrupt stack switching to Linux kernel, query of hardware interrupt number, processing of interrupt service program, etc.
After determining that the current interrupt source is a clock interrupt, the scheduler_ The tick () function will check whether the current process needs scheduling. If scheduling is required, set the need of the current process_ Resched flag bit (tif_need_resched flag bit in thread_info).
Interrupt return. Here, you need to return an end of interrupt (EOI) signal to the interrupt controller.
At el1_ The IRQ assembly function directly restores the interrupt site, where the PT of thread 0 will be used_ Regs stack box to restore the interrupt site. In systems that do not support kernel preemption, el1_ The IRQ assembler function does not check whether scheduling is required. Finally, open the interrupt, and the CPU continues to execute process 0 from the place interrupted by the interrupt.
In the Linux kernel that supports the kernel preemption function, when the interrupt returns, it will check whether the current process has set need_ The position bit of the resched flag. If set, preempt is called_ schedule_ The IRQ () function schedules other processes (threads) to run. For example, in this figure, in the Linux kernel that supports kernel preemption, the process of interrupt and scheduling is slightly different. At el1_ When the IRQ assembly function is about to return to the interrupt site, judge whether the current process needs to be scheduled. If scheduling is required, the scheduler will select the next process and switch the process. For example, in this figure, if kernel thread 1 is selected, the PT of kernel thread 1 is selected_ Restore the interrupt field in the regs stack box, open the interrupt, and then continue to execute the code of kernel thread 1.
You may have questions about the previous picture:
If kernel thread 1 is a newly created process and its stack should be empty, how to restore the interrupt scene when it runs for the first time?
If the interrupt field cannot be recovered from the stack of kernel thread 1, does kernel thread 1 always run in the off interrupt state?
For kernel threads, the following two parts will be set and saved during creation.
The hardware context of the process. It is stored in the CPU of the process_ Context data structure. The process hardware context includes X19 ~ x28 registers, FP registers, SP registers and PC registers. See section 8.1.6 for details.
For arm64 processor, set PC register to ret_ from_ Fork refers to ret_ from_ Fork assembly function. Set the SP register to point to the PT of the stack_ Regs stack box.
pt_ Regs stack box.