Home >Operation and Maintenance >Linux Operation and Maintenance >What is the task scheduling mechanism of linux
The task scheduling mechanism of Linux refers to the specific command or program executed by the system at a certain event; each CPU will have a queue to store tasks in the "TASK_RUNNING" state, and task scheduling is taken out from these queues The task with the highest priority is placed next to the CPU for execution.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
refers to a specific command or program executed by the system at a certain time
In Linux, every A CPU will have a queue to store tasks in the TASK_RUNNING state. Task scheduling is to take out the highest priority tasks from these queues as the next task to be put into the CPU for execution.
Task scheduling requires two processes: context switching and selection algorithm
Context switching
Context switching from a process To the context of another process, because it occurs so frequently, it is often the key to the efficiency of the scheduler.
schedule() function calls the switch_to macro. This macro implements real switching between processes. Its code is stored in include/i386/system.h .
The switch_to macro is written in embedded assembly and is difficult to understand.
switch_to() function returns normally. The return address on the stack is the task_struct::thread::eip of the new process, which is the continuing position set when the new process was suspended last time (the last time switch_to was executed) () label "1:" position).
At this point, it is transferred to the context of the new process to run. This involves wakeup, sleepon and other functions to sleep and wake up the process.
Selection algorithm
The Linux schedule() function will traverse all processes in the ready queue and call the goodness() function to calculate the weight of each process , select the process with the largest weight and put it into operation. The Linux scheduler is mainly implemented in the schedule() function.
Scheduling steps:
Schedule function workflow is as follows:
(1) Clean up the currently running process
(2) Select A process to run (pick_next_task)
(3) Set the running environment of the new process
(4) Process context switching
The Linux scheduler divides processes into three categories
Process scheduling is the core function of the operating system.
The scheduler is only part of the scheduling process. Process scheduling is a very complex process that requires multiple systems to work together to complete.
This article only focuses on the scheduler. Its main job is to select the most appropriate one among all RUNNING processes.
As a general operating system, the Linux scheduler divides processes into three categories:
Interactive processes
This type of process has a large amount of human-computer interaction, So the process is constantly sleeping, waiting for user input. A typical application is the editor vi. This type of process has relatively high requirements on system response time, otherwise users will feel that the system is slow to respond.
Batch Process
This type of process does not require human-computer interaction, runs in the background, and requires a large amount of system resources. But can tolerate the response delay. Such as a compiler.
Real-time process
Real-time has the highest requirements on scheduling delay. These processes often perform very important operations and require immediate response and execution. For example, video playback software or aircraft flight control systems. It is obvious that such programs cannot tolerate long scheduling delays, which may affect the movie screening effect at best, or cause the aircraft to crash and kill people
Recommended study:Linux video tutorial
The above is the detailed content of What is the task scheduling mechanism of linux. For more information, please follow other related articles on the PHP Chinese website!