For more information about Embedded Linux system process scheduling-Linux general technology-Linux programming and kernel, see the following. Abstract: This article analyzes the basis, policy, and scheduler schedule () of Linux Process Scheduling Based on the source code ().
The processor (CPU) is the core resource of the entire computer system. In a multi-process operating system, the number of processes is usually more than the number of processors, which leads to the competition for processing machines between processes. Process scheduling has a decisive impact on the implementation of system functions and performance in all aspects. Its essence is to distribute the processor to various processes in a fair, reasonable, and efficient manner. Scheduling is a necessary means to achieve multi-task concurrent execution. different operating systems have different scheduling objectives. In traditional Unix time-based systems, ensuring that multiple processes use system resources fairly and providing better response time is the main goal of scheduling. In a strong real-time operating system, tasks with a higher priority always give priority to the right to use the processor.
Linux is suitable for embedded applications because of its stable kernel, powerful functions, scalability, and low cost. However, the Linux kernel itself does not have strong real-time features and has a large kernel size. Therefore, to use Linux in an embedded system, you must implement real-time and embedded Linux. Based on the characteristics of real-time and non-real-time processes (common processes), Linux integrates the preceding scheduling policies to implement efficient and flexible process scheduling.
2 Linux Process Scheduling Analysis
2.1 description of the Linux Process status
Linux describes the process status as follows:
TASK_RUNNING: running status. A process in this status can be scheduled and executed to become the current process.
TASK_INTERRUPTIBLE: an interrupted sleep state. A process in this status is awakened when the required resources are valid. It can also be awakened by a signal or scheduled interruption.
TASK_UNINTERRUPTIBLE: an uninterrupted sleep state. A process in this status is awakened only when the required resources are valid.
TASK_ZOMBIE: Zombie status. Indicates that the process has ended and resources have been released, but its task_struct has not been released.
TASK_STOPPED: paused. A process in this state can be awakened only by signals from other processes.
2. scheduling mode
Each process in Linux is allocated with a relatively independent virtual address space. The virtual storage space is divided into two parts: the user space contains the code and data of the process itself; the kernel space contains the code and data of the operating system.
In Linux, conditional deprivation scheduling is adopted. For common processes, when the time slice ends, the scheduler selects the next process in the TASK_RUNNING status as the current process (Voluntary scheduling ). If the priority of a real-time process is high enough, it will seize the CPU from the current running process to become a new current process (Force scheduling ). When forced scheduling occurs, if the process runs in the user space, it will be directly deprived of the CPU; if the process runs in the kernel space, even if it is urgently needed to discard the CPU, the CPU will not be deprived until the system space returns.
3. Scheduling Policy
3.1 three scheduling policies
(1) SCHED_OTHER. SCHED_OTHER is a time slice rotation policy for common processes. When this policy is used, the system allocates a time slice for each process in the TASK_RUNNING state. When the time slice is used up, the process scheduler selects the next process with a higher priority and grants the CPU permission.
(2) SCHED_FIFO. The SCHED_FIFO policy is applicable to Real-Time Processes with high response time requirements and short running time. When this policy is adopted, each real-time process obtains the CPU in sequence as it enters the operational queue. In addition to waiting for an event to take the initiative to give up the CPU, or a process with a higher priority to deprive it of the CPU, the process will continue to occupy the CPU.
(3) SCHED_RR. The SCHED_RR policy is applicable to Real-Time Processes with high response time requirements and long running time. When this policy is adopted, each real-time process uses the CPU in turn by time slice. When the time slice of a running process is used up, the process scheduler stops running and places it at the end of the runable queue.
3.2 process scheduling basis
In Linux, there is only one runable queue. Real-time and normal processes in the TASK_RUNNING state are added to this runable queue. In Linux, process scheduling adopts dynamic priority and weight control methods. These three scheduling policies can be implemented, and real-time processes always use CPU preferentially than normal processes. Describes the data structure of a process. task_struct uses the following data as the basis for scheduling: