The process of understanding process scheduling and process switching during the time-tracking analysis process

Source: Internet
Author: User
Tags prev git clone

Qin Dingtao "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

First, theoretical study:

1, the process of scheduling time and process switching

The operating system principle introduces a large number of process scheduling algorithms, which from the implementation point of view is only from the operational

rows in the queue Choose a new process that uses different strategies in the process of selection. For understanding Operations

system's working mechanism, instead, the scheduling timing of the process and the switching mechanism of the process are more critical.

2, the timing of process scheduling

(1) The interrupt processing process (including clock interrupts, I/O interrupts, system calls, and exceptions) is called directly

Schedule () , or call schedule () based on the need_resched tag when returning to the user state;

(2) Kernel threads can directly call schedule () for process switching, or during interrupt processing

Scheduling, that is, the kernel thread as a class of special processes can be active scheduling, can also be passive

Scheduling

(3) The user state process can not realize the active scheduling, only through the kernel state after a certain point in time

scheduling, which is scheduled during interrupt processing.

3, the process of switching

(1) In order to control the execution of the process, the kernel must have the ability to suspend a process that is executing on the CPU and restore

The execution of a process that was previously suspended, which is called process switching, task switching, context switching;

(2) suspend the process that is executing on the CPU, which is different from saving the scene at the time of interruption, before and after the interruption is in the same

In a process context, only the user state is switched to the kernel state execution;

(3) The process context contains all the information required for process execution

User address space: Includes program code, data, user stack, etc.

Control information: Process descriptor, kernel stack, etc.

Hardware context (note that interrupts are also saved by the hardware context knowledge Save method)

(4) The schedule () function selects a new process to run, and calls Context_switch to go up and down

This macro calls Switch_to to make a critical context switch.

Next = Pick_next_task (RQ, prev);//process scheduling algorithms encapsulate this Han Hong

Context_switch (RQ, Prev, next);//process Context switch

Switch_to takes advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process

4, the general implementation process of Linux system

The most common scenario: The running user-state x switches to the process of running user-state process y

(1) Running user-state process X

(2) An interrupt occurred--save cs:eip/esp/eflags (current) to kernel stack, then load CS:EIP (entry of a specific ISR) and Ss:esp (Point to Ke Rnel stack).

(3) Save_all//Save site

(4) Schedule () is invoked during interrupt processing or before the interrupt is returned, where SWITCH_TO does a critical process context switch

(5) After the label 1 starts to run the user-state process y (where Y has been switched out through the above steps so you can continue from the label 1)

(6) Restore_all//recovery site

(7) Iret-pop cs:eip/ss:esp/eflags from kernel stack

(8) Continue to run user-state process y

A few special cases:

(1) through the terminal processing in the timing of the process, the user state process and kernel threads switch between each other and the kernel threads switch between each other,

Very similar to the most common situation, only the kernel thread during the operation of the interruption of the user-state and the kernel-state conversion;

(2) kernel thread actively calls schedule (), only the process context switch, there is no interrupt context switch,

And the most general situation is slightly abbreviated;

(3) The execution starting point and returning user state, such as fork, that the system call of the child process is created in the subprocess;

(4) When a new executable program is loaded and returned to the user state, such as EXECVE;

Third, the experimental process

Open the virtual machine and enter the following command:

CD Linuxkernel

RM MENU-RF

git clone https://github.com/mengning/menu.git

CD Menu

MV TEST_EXEC.C test.c

Make Rootfs

Then in terminal input: Qemu-kernel. /linux-3.18.6/arch/x86/boot/bzimage-inird Rootfs.img-s-S

Open a different shell terminal and enter the following command:

Gdb

File Linux-3.18.6/vmlinux

Target remote:1234

B Schedule

C

Then trace debugging, you can explore the switch_to and other functions of the execution process

The main processing procedure for the schedule () function is:

1. Handling for preemption

2, RAW_SPIN_LOCK_IRQ (&rq->lock);

3. Check the status of the prev and reset state

4, Next = Pick_next_task (RQ, prev); Process scheduling algorithm

5. The clock of the update-ready queue

6, Context_switch (RQ, Prev, next); Process Context Switch

The schedule () function is used to select a new process to run and invoke Context_switch () to switch context.

This macro calls Switch_to () for critical context switching, where the Pick_next_task () function encapsulates the process scheduling algorithm.

The switch_to implements a true switchover between processes:

(1) The contents of ESI, EDI and EBP registers are saved first in the kernel stack of the current process prev.

(2) then the Prev kernel stack pointer ebp is deposited into the PREV->THREAD.ESP

(3) Assign the kernel stack pointer next->thread.esp that will run the process next to the ESP register

(4) Save the address of the POPL instruction in the PREV->THREAD.EIP, this address is prev the next scheduled address

(5) JMP transfer to __switch_to () function

(6) To restore the stack contents of next when it was last transferred, the next process becomes the current process to begin execution.

Iv. Summary of the experiment

Through the course of learning, I have a process scheduling time and process scheduling and process switching process has a certain

the understanding. Timing of process scheduling: During interrupt processing, call schedule directly, or return to the user state based on

The need_resched tag calls schedule; The kernel thread can call schedule directly for process switching.

Can also be scheduled during interrupt processing, that is, kernel threads as a special kind of process can be actively scheduled,

Also can not move the dispatch, the user state process can not realize the active scheduling, can only be dispatched during the interrupt processing.

Process switch: The schedule function selects a new process to run, and calls Context_switch for

Context switch, this macro calls switch_to to switch the critical context.

The process of understanding process scheduling and process switching during the time-tracking analysis process

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.