Linux 2.4 Process Scheduling Analysis 2

zhaozj2021-02-17  52

3. Current

The core often needs to know the Task_struct of the process currently run on a CPU, point to this descriptor with the Current pointer in Linux. The implementation of the Current uses a tip to achieve efficient access speed, this tip is related to the storage method of the Linux process task_struct.

In Linux, the process used in the core level is different from the stack assigned and used in user-level. Because this stack is not high, only two pages (8kb) are allocated when the process is created, and the task_struct of the process is arranged on the top of the stack. (In fact, these two pages are applied at the time of assigning task_struct, and then the ESP is preset to the end of the tail of the process after the Task_Struct is initialized, and the TASK_STRUCT direction extends.

Therefore, to access the task_struct of this process, just do the following simple operations:

__ASM __ ("ANDL %% ESP,% 0;": "= r": "0" (~ 8191 uL);

This sentence will work with the 0x0ffffe000 as "and", obtain the home base base of the core stack, which is the address of the task_struct.

4. Schedule_data

Task_struct is a data structure for describing the process, which contains properties to the running CPU. In Linux, another data structure corresponds to the CPU, which can be used to access the process running on a CPU, which is defined as the Schedule_Data structure, including two properties: Curr pointer, pointing to the process running on the CPU Task_struct, usually accessed with a CPU_Curr (CPU) macro; Last_ScHEDule timestamp records the last time the process switching on the CPU, usually uses the Last_ScHEDule (CPU) macro to access.

In order to make the access of the data structure can be consistent with the CPU's Cache Line size, schedule_data is organized to an aligned_data unit in SMP_CACHE_BYTES, and each CPU corresponds to an element on each CPU in the system.

5. Init_tasks

The scheduler does not directly use the init_task as the process linked list of the header, but only use "iDLE_TASK". This process is in the CPU_IDLE () loop after guiding the system (see "IDLE process") of "other core applications"). In the SMP system, each CPU corresponds to an IDLE_TASK, and their Task_Struct pointer is organized into the init_tasks [nr_cpus] array, and the scheduler accesss these "iDLE" processes via the IDLE_TASK (CPU) macro (see "scheduler work Process").

6. Runqueue_Head

The linked list of Runqueue_Head is recorded all the ready-to-state process (except for the current running process), but iDLE_TASK is except), but the scheduler always selects the most suitable scheduled process to run.

three. Ready process selection algorithm

The Linux Schedule () function will traverse all the processes in the ready queue, call the goodness () function to calculate the weight Weight of each process, and select the maximum amount of weights to run.

The calculation of process scheduling weights is divided into real-time processes and non-real-time processes. For non-real-time processes (SCHED_OTHER), the factors affecting weights have the following: 1. The number of Ticks left in the current time tablet, ie Task_struct's Counter, the greater the chance to get the CPU, the greater the CPU, because the initial value of the Counter is related, so this factor represents the priority of the process, and on the other hand The process of "unrequench"; (weight = p-> counter;)

2. Whether the CPU running last time is the current CPU, if yes, the weight increases a constant, indicates that the priority is not migrated, because the cache information is still valid; (Weight = proc_change_penalty;)

3. Does switching if the switch needs to switch memory, if not required (or switch between the two threads of the same process, or if there is no core thread of the MM property), the weight plus 1, indicates that (slight) Priority is not switched Memory process; (Weight = 1;)

4. The user visible priority Nice, the smaller the weight, the smaller the weight. (The NICE value in Linux is selected between -20 to 19, the default is 0, the Nice () system call can be used to modify the priority.) (Weight = 20 - P-> nice;) For real-time processes (Sched_FIFO, SCHED_RR), the weight size is only determined by the RT_Priority value of the process (Weight = 1000 P-> RT_PRIORITY;), the reference quantity of 1000 makes the weight of the real-time process are larger than all non-real-time processes, so as long as There is a real-time process in the ready queue, and the scheduler will give priority to its operational needs.

If the weight is the same, select the procedure in the previous column in the ready queue to put into operation.

In addition to the above standard values, Goodness may also return -1, indicating that the process sets the SCHED_YIELD bit, at which time it is only when there is no other ready process.

If you traverse all ready processes, the weight value is 0, indicating that the current time slice is over, and the counter value of all processes (not just ready process) is recalculated, and then the ready process is selected (see "scheduler. work process").

转载请注明原文地址:https://www.9cbs.com/read-28427.html

New Post(0)