Linux 2.4 Process Scheduling Analysis 4

zhaozj2021-02-17  58

B. Prev

In Schedule, the current process (that is, the process that may be scheduled) is accessed with the prev pointer.

For the real-time process of SCHED_RR, only when the process time piece ends (counter == 0), it will switch to another process, and will reset the Counter according to the NICE value, and the process is placed at the end of the ready queue. Of course, if there is no other real-time process in the current ready queue, the scheduler will still select to the process according to the goodness () algorithm mentioned earlier.

If the process in the Task_Interruptible status is required to process (this may happen to wait for the signal to wait for the signal to discard the CPU, before the CPU is abandoned, the signal has occurred), the scheduler does not immediately execute the process, but will This process is ready (the process is also deleted in the future and from the ready queue), participating in the immediate Goodness selection.

If Prev is not in the read, it is not in the above-mentioned hanging state (PREV is waiting for resources, actively calling Schedule () Abandon CPU), then remove it from the ready queue, after which it is awakened Operation resets the process back to the ready queue, otherwise it will not participate in the schedule.

When the passive manner starts the scheduler, the NEED_RESCHED attribute of the current process will be set (see "Scheduler Working Timer"). In Schedule (), this bit will be cleared, indicating that the process has been processed in the scheduler (of course, this process does not mean that the process must obtain a CPU).

C. Goodness

The scheduler traverses all the processes in the ready queue, as long as it is currently scheduled (CPUS_Runnable & CPUS_ALLOWED & (1 << CPU), indicating that the process can run on the CPU of the currently running scheduler, and is not running on other CPUs), Calculate your weight to call Goodness (). Next pointer is used to point to the maximum weight, the default points to iDLE_TASK, and if the reader is empty, use the default idle_task as Next.

As mentioned earlier, if the maximum weight after the end of the traversal is 0, it means that the time slice of all the ready-to-scheduled processes is used, and the scheduler will need to reset all processes (including ready and Hally, the COUNTER value is not completed (for example, the currently hang process or process currently running on other CPUs), half of its remaining time slice will be superimposed into the new time film.

Setting the selected process to run on this CPU (task_set_cpu ()), Runqueue_lock can be unconnected, and next will configure the next.

D. Next

The new process selected may just need to replace the old process. At this time, because there is no need to switch, you can skip the configuration next and "Switch" and "Schedule_tail".

The operating environment of the new process is actually mainly the memory. There are two memory properties associated with the scheduler: MM and Active_mm, the former pointing to the memory area owned by the process, and the latter pointing to the memory actually used by the process. For most processes, MM and Active_mm are the same, but the core thread is not autonomous memory, and their MM pointers are always NULL. We know, in the false page table of any process, the core space will always map the high-end fixed position of the virtual memory, so the memory used by the core thread is the same, so there is no need to switch the process. . In the scheduler, you can know if NEXT-> MM is empty, you can know that the process is a core thread. If so, continue using prev's Active_mm (Next-> Active_MM = Prev-> Active_MM), and by setting CPU_TLBSTATE [CPU] .State is TLBSTATE_LAZY, telling the memory management component not to refresh the TLB; otherwise, call the switch_mm () function to switch the switch (the specific process involves the knowledge of the memory management module, here is omnidably). In fact, the prev-> Active_mm and Next-> mm will be judged once in Switch_MM (), if two values ​​are equal, the two processes are two "threads" belonging to a "process" (actually light The amount process), does not need to perform memory switching, but this situation is still refreshed. After setting the NEXT memory environment, you can call MMDrop () to release the memory structure of the Prev. All processes that are not running, their Active_MM properties should be empty.

E. Switch

The process of process switching has been described in detail in above.

F. Schedule_tail

After the switch is complete, the scheduler will call __schedule_tail (). This function has no impact on the UP system. For the SMP system, if the process of being switched (reply) is still in the read state and is not dispatched by any CPU, __ schedule_tail () will call reschedule_idle (), pick one for P Idle (or the running process priority than P low) CPU and forces the CPU to re-schedule to re-enter P. When the process wakes up from the sleep state, a suitable CPU is also required to operate, which is implemented by calling reschedule_idle () in the wake_up_process () function. The principle of picking the CPU is as follows:

p The last running CPU is currently idle. Obviously, this is the best choice, because it does not need to seize the CPU, CPU Cache is most likely to match P. However, since the P is running, the scheduler cannot schedule to IDLE_TASK, so this situation will only occur when Wake_up_process ().

One of the least recently active in all idle CPUs (Last_Schedule (CPU) minimum). The Cache information in the CPU is most likely to use, so this option can be as best possible to reduce the cost of seize the CPU, while avoiding frequent preemptions as much as possible. It is worth noting that on the SMP platform that supports the CPU supporting the Hyper-Treading Technology, once the two logical CPUs of the physical CPU are found, one of the logical CPUs of the CPU immediately becomes the Scheduled CPU of the P candidate without the need. Continue to find the least recently active CPU.

The CPU is not idle, but the running process priority is lower than the priority of P, and the difference is the largest. Calculate the priority is a Goodness () function because the information it contains is most. After finding the appropriate CPU, reschedule_idle () will set the target process (the process running on the CPU, may be iDle_task), so that the scheduler can work (see "scheduler working opportunity"). At the same time, because iDLE_TASK is in many cases, the CPU is stopped (HALT) state to power saving, so it is necessary to call SMP_SEND_RESCHEDULE (CPU) to send a reschedule_vector interrupt (via the IPI interface) to the CPU to awaken the CPU.

Note: The target process is the case of idle_task, but also to determine its NEED_RESCHED flag, only when it is 0, start scheduling, because the idle_task itself is not checking the need_resched value, which will start Schedule () (See "iDLE process").

G. Clear

There are two results in the scheduler work: switching, no switching, but the cleaning operation before the scheduler exits is the same, that is, the state of restoring the new process. Mainly includes two actions:

The SCHED_YIELD bit of the clever process (regardless of whether it is set);

If the new process (P) Lock_Depth is greater than or equal to 0, it is restored to the core lock kernel_flag (see "related lock").

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

New Post(0)