Process switching Process Detailed

Source: Internet
Author: User
Tags function definition

/*

Note: In learning the kernel, there is a difficulty, that is, any one module is not isolated, such as the scheduling of the process is designed to interrupt processing, signal processing and process context switching and so on. As a beginner, it is unrealistic to think that the whole operation of the operating system can be clearly displayed in the mind. Nevertheless, each module has its focus, and the strategy is to divide the entire operating system into several large modules, such as process management, memory management, file systems, and so on. Then these large modules are further decomposed into small modules, such as process management can be subdivided into process creation, process switching, system call processing, signal processing and so on. In the analysis of each module, the other modules to abstract, do not fall into the details of other modules, it can also be said that this is a conquer method, when you put each of the functions of the small module clear, to the end of the entire operating system of the operation process is very clear!

*/

In the previous blog, we mentioned that when a task returns from the system call handler, it checks whether a process switch is required. So when does a process switch (Task Scheduler) occur? Process switching can occur when a system hardware interrupt, system call, or clock interruption occurs. Let's take a clock break as an example to see how the process is switched.

Before we do this, we have to make a note, as we do not begin to introduce the details of the process, and the detailed introduction of the process will be placed in the Process creation blog (∩_∩), so here we begin with a rough abstraction of the process: a task (that is, a process) containing code snippets, Data segment, stack segment, and a task status segment TSS. This task state segment TSS records all status information for the current task, including registers, system parameters, and so on. The TSS segment descriptor is placed in the TR register (that is, access to the TR will be able to access the TSS segment of the current task).

Assuming the CPU is executing process 1 at the moment, we know that: the system has a clock frequency, a clock interrupt occurs every once in a while, this time period we call a tick. Assuming a tick occurs, the system has a clock interrupt, and the clock interrupt handler is automatically called (timer_interrupt), Timer_interrupt defined in Kernel/system_call.s, as shown in:

Like our last _system_call, it performs some protection work first, and then adds 1 to the value of _jiffies in the 189th Line of code (_jiffies indicates the number of ticks elapsed since the system started), The 第192-194 code then presses the privileged-level CPL that performs this clock break into the stack, which is used as a parameter for the subsequent do_timer, and then begins the execution of the Do_timer,do_timer function definition in kernel/ In SCHED.C, the primary function of this function is to add 1 of the current process's user-state execution time or kernel-state execution time, and then subtract the remaining time slices of the current process by 1.

If the time slice of the current process has remaining, then the direct return returns to continue execution, next to determine whether the current task Cpl is 0, if it is 0, the current task is broken in the kernel state, The kernel state in the Linux0.11 cannot be preempted, so the process Scheduler schedule () is executed directly, if not 0. Next we analyze the schedule () function, the schedule function is also defined in SCHED.C, which contains the following two segments of code:

The function of this code is: traverse the task array, check their alarm timing value, if the value is less than jiffies, indicating that the alarm time of the task is over, then in its signal bitmap SIGALRM signal, indicating to send sigalarm signal to the task, and then alarm zeroed, Next, check if there are other signals that are not blocked, and if so, then set the task to the ready state if one is present and the current process status can be interrupted.

The code for the 第124-142 line iterates through the entire task array, identifying the task whose status is task_runing and the longest time slice. and call the Swith_to () function to switch to that task. The SWITH_TO function is defined in include/linux/sched.h.

Switch_to is a piece of assembler code, which explains what this code means: first check that the task you want to switch is not the current task, and if so, exit directly. Next, the TSS segment of task N (the task to switch to) is placed in the _TMP.B, then the task n is placed in the _current, the current task is placed in%ecx, and then a long jump to the *&_tmp location is performed (this is the TSS address of the new task), At this point the CPU will save the contents of all registers to the TSS segment performed by the current task TR, and then restore the register information in the TSS segment of the new task to the various registers of the CPU, so that the system will formally begin to perform new tasks. 第178-180 's code is to determine if the original task has used the coprocessor, and if not, it ends directly.

Process switching Process Detailed

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.