Scheduling and switching of the soul torture, see if your college operating system class learned the teacher’s real skills? Did not answer right, explain you have a class and stupid uncle same sleep, truant, fry stocks, play games, and female classmates chat

Scheduling and switching confusion

Many friends asked Uncle Ben about scheduling and switching. In the opinion of FAE, he did not understand the scheduling and switching thoroughly and was in a state of whether he understood or not. Even if the university has learned the operating system this course, it is only to learn some theory, did not learn the teacher’s real ability. Stupid uncle deliberately read several foreign classic teaching materials, some books simply mentioned, some seem to simply do not talk about switching, such as “modern operating system”. For example, this book “in-depth understanding of computer system”, simple a few sentences, summary is very good, but the average person will see dizzy, stupid uncle also dizzy.

Stupid uncle is bored, and he specially went to B station to see several classic operating system courses at home and abroad, such as MIT, California Institute of Technology, HMC and other famous courses, these courses are very good for beginners.

California institute of technology,

HMC College courses

The OS course of a 985 college in China

The above courses are very, very good courses with excellent theoretical knowledge. However, if I am a student, I am still in a fog about scheduling and switching, which is to understand or not to understand. It may be the reason why stupid uncle is more stupid. The 16 questions of soul torture mentioned below still need to be explored, to have a deep understanding and understanding, the better way is:

  1. For more practice, use the Linux kernel for debugging and summarizing, you can use the O0 compiled RLK kernel provided by Running Bar.
  2. Listen to stupid FAE, watch stupid uncle video
  3. Write your own OS, such as the stupid OS.

Scheduling and switching of soul torture – 16 questions

If the following soul torture can understand, that the scheduling and switching, is really understand.

  1. What exactly is the dispatch trying to do?
  2. What is the timing of the dispatch? When does the operating system schedule?
  3. How to properly choose the next process?
  4. What is process context? What does the process context contain?
  5. Where is the process context saved?
  6. What is an interrupt scene? What should be saved at the interrupt site?
  7. Where is the interruption site kept?
  8. What exactly do I need to switch when I switch processes?
  9. From the CPU’s point of view, when a process switches, does the CPU distinguish between the prev process and the next process?
  10. Assuming that both the next and prev processes are user processes, what is the next statement executed by the next process when the prev process switches to the next process? Was it the instruction where the next process was interrupted in user space?
  11. Suppose a clock interrupt occurs while the PREv process is executing, and then a process switch occurs, switching to the next process, when will the interrupt scene of this clock interrupt be restored?
  12. Suppose the prev process has a process switch under the clock interrupt driver, and the next process is selected as the newly created process, where does the newly created process start execution?
  13. If the new process is always executing in the loop, then the system will never be able to respond to the clock interrupt again. As a result, the system will always be running the new process.
  14. In an interrupt handler, can I call the schedule() function directly? Why is that?
  15. If the next process selected by the scheduler isa loop process, is the system unable to respond to clock interrupts and therefore breaks down?
  16. Why does the switch_to() function take 3 arguments? Prev and Next are enough. Why last?

Process scheduling and switching design to process management, process switching, context, interrupt, stack, system call and other aspects of knowledge. Just switching is hard to understand. We can start with a little question:

Suppose the Linux kernel has only three kernel threads, and thread 0 creates kernel thread 1 and kernel thread 2, which never exit. When a system clock interrupt arrives, the clock interrupt handler checks to see if any processes need to be scheduled. When scheduling is required, the scheduler selects thread 1 or thread 2 to run. Assuming process 0 runs first, what happens in this scenario?Copy the code

This is an interesting problem, involving the implementation mechanism of the scheduler, interrupt handling, kernel preemption, how the newly created process is scheduled, process switching and so on. We can’t really understand the problem until we understand all of this.

Scenario analysis

To go back to the problem, the main steps of this scenario are as follows.

  1. The first thing that comes out is thread 0. We all know that in the Linux kernel, thread 0 is the thread that runs when the system starts. Start_kernel () runs on thread 0. Thread 0 creates kernel thread 1 and kernel thread 2.

    Function call relationships: start_kernel()->kernel_thread()->_do_fork(). The _do_fork() function creates a new thread and adds it to the scheduler’s ready queue. After thread 0 has created kernel thread 1 and kernel thread 2, it enters the while loop. Thread 0 does not exit. It is waiting to be scheduled. 2. The second one: clock interrupt. Clock interrupt is a very important thing in the system, the processor uses a clock timer to periodically provide the system pulse. Clock interrupt is also a common peripheral interrupt. The scheduler uses clock interrupts to periodically detect whether currently running threads need to be scheduled. Function call relationships:

el1_irq->handle_domain_irq->__handle_domain_irq->generic_handle_irq_desc->handle_percpu_devid_irq->arch_timer_handler_vi rt->timer_handler->tick_handle_periodic->tick_periodic->update_process_times->scheduler_tickCopy the code
  1. Set the need_resCHED flag bit when clock interrupts detect that the current thread needs to be scheduled. In the check_preempt_tick() function.
  2. When a clock interrupt returns, scheduling is determined based on whether the Linux kernel supports kernel preemption. The following two cases are discussed. Kernel preemption: When an interrupt occurs in the kernel state, check whether the current thread’s need_resCHED flag bit is set. If set, the current thread needs to be scheduled. Kernels that do not support kernel preemption: Interrupts that occur in kernels are not checked for scheduling when the interrupt returns.

In a Linux kernel that does not support kernel preemption, the kernel will not schedule kernel thread 1 or kernel thread 2 to run even if thread 0’s need_resCHED flag is set. Only when interrupts that occur in user-mode return, or when a system call returns to user-space, are checked for scheduling. In this picture, for example, the process is as follows.

  1. A clock interrupt occurred. The current process (thread) may execute in user mode or kernel mode when a clock interrupt is triggered. When a process is interrupted while running in user mode, it will enter the el0_IRQ assembly function of the abnormal vector table. When the process is interrupted while running in kernel mode, it will enter the EL1_IRq assembly function of the abnormal vector table. In this scenario, because all three threads are kernel threads, a clock interrupt can only jump to the el1_IRq assembler function. When an interrupt is entered, the CPU automatically closes the interrupt.
  2. In the el1_IRQ assembler function, the interrupt scene (also known as the interrupt context) is first stored on the stack of the current process. The Linux kernel uses the PT_regS data structure to implement a stack box that holds the interrupt scene (referred to in this section as the PT_regS stack frame).
  3. Interrupt processing process, including switch to Linux kernel interrupt stack, hardware interrupt number query, interrupt service procedure processing.
  4. When it is determined that the current interrupt source is a clock interrupt, the scheduler_tick() function checks whether the current process needs to be scheduled. If scheduling is required, set the need_resCHED flag bit (TIF_NEED_RESCHED flag bit in thread_info) for the current process.
  5. Interrupt return. You need to return an End Of Interrupt (EOI) signal to the Interrupt controller.
  6. In the el1_IRq assembler function to restore the interrupt scene directly, thread 0’s PT_regs stack is used to restore the interrupt scene. On systems that do not support kernel preemption, the el1_IRq assembler function does not check for scheduling. Finally, the interrupt is turned on, and the CPU continues to execute process 0 from where the interrupt left off.

In Linux kernels that support kernel preemption, the interrupt returns and checks whether the current process has the need_resCHED flag bit set. If set, the preempt_schedule_irq() function is called to schedule other processes (threads) to run. For example, in the Linux kernel, which supports kernel preemption, interrupts and schedules flow slightly differently. Determines whether the current process needs to be scheduled when the el1_IRq assembler function is about to return to the interrupt scene. If scheduling is required, the scheduler selects the next process and switches the process. For example, if kernel thread 1 is selected, the interrupt scene is recovered from the pT_regS stack box of kernel thread 1, the interrupt is opened, and kernel thread 1’s code continues to execute.

You might have some questions about the previous picture:

  1. If kernel thread 1 is a newly created process and its stack should be empty, how does it recover the interrupt scene the first time it runs?
  2. If the interrupt scene cannot be recovered from kernel thread 1’s stack, is kernel thread 1 always running in the off interrupt state?

 

For kernel threads, the following two parts are set and saved during creation.

  • The hardware context of the process. It is the CPU_context data structure stored in the process. The process hardware context includes the X19 to X28 registers, FP registers, SP registers, and PC registers, as described in Section 8.1.6 (p.

For ARM64 processors, set the PC register to REt_FROm_fork, pointing to the ret_from_fork assembler function. Set the SP register to point to the pT_regs stack box.

  • Pt_regs stack frame.

Please subscribe to this video

Due to lack of space, this article does not cover soul Torture 16 in detail. If you are interested in the above soul torture 16 questions or would like to hear in-depth answers from stupid FAE, please subscribe to the video.

Running is the flagship video, according to the theme content is divided into multiple “seasons”, as long as the subscription, recording the same theme of the video is free update yo.

If you’re interested, you can subscribe to running! Click “Read the article” to subscribe!