The author of this article (), please respect the author's copyright while reading this article.
[Abstract] Linux is a multi-user and multi-task operating system. It implements fair and efficient scheduling for multiple processes, rather than using a single scheduling policy, instead, several scheduling policies are organically integrated.
[Keywords] process scheduling priority time slice rotation real-time process
In any operating system, process scheduling has always been a core issue. The selection of process scheduling policies has a crucial impact on the entire system. A good scheduling algorithm should consider many aspects: fairness, effectiveness, response time, turnaround time, system throughput, and so on, but these factors are mutually contradictory. The final trade-off depends on the target to be achieved by the system, this article takes the Linux operating system as an example to analyze its process scheduling policies, in order to have a deeper understanding of the process scheduling process.
I. Process Scheduling in Linux
Linux supports multiple processes. Process Control Block (PCB) is one of the most important data structures in the system. It is used to store all kinds of information required by processes, the PCB is represented by the structured task-struct, including the process type, Process status, priority, and clock information. In Linux, the process scheduling operation is executed by the schedule () function, this is a function that runs only in the kernel state. The function code is shared by all processes.
Ii. Linux Process Scheduling time
The process scheduling time in Linux is basically the same as that in the modern operating system. To determine whether the kernel process scheduling program can be executed to schedule the process, the process scheduling mark need-resched is set in Linux, when the flag is 1, the scheduler can be executed. generally, the scheduling time in Linux is divided into the following two situations: (1) Active scheduling: explicitly calling the schedule () function to explicitly release the CPU, causing a new round of scheduling. generally, the status of the current process changes, for example, Process Termination, process sleep, and the process processes certain signals in a moderate manner. (2) passive scheduling: The schedule () function is called, it is only the need-resched process scheduling flag in the PCB. If this field is set to 1, new process scheduling will occur, the core scheduler will actively query the need-resched status (if it is set to a bit, it will call the schedule () function ), generally, when a new process is generated, the priority of a process changes, the resources that a process waits for can be awakened, and the time slice of the current process is used up.
Iii. Linux Process Scheduling Policy
Generally, the scheduling policies for different operating systems are different. Linux Process Scheduling combines priority scheduling, time slice rotation scheduling, and first-in-first-out scheduling. in Linux, different types of process scheduling policies are different.
1. Data Structure related to process scheduling
Every process is a dynamic individual, and its lifecycle is defined in sequence as: task-running, TASK-INTERRUPTIBLE, TASK-UNINTERRUPTIBLE, task-zombie and task-stopped, the status of a process changes several times during its survival. The corresponding data structure is the status of the Linux Process, which is: running, waiting, paused, and frozen.
2. Process status and description of the conversion process
When a process is created, it cannot interrupt its sleep state. After it is awakened by the parent process before the do-Fork () process ends, it changes to the execution state, A running process is moved to the run-queue ready task queue for scheduling. When appropriate, schedule () selects it based on the scheduling algorithm to obtain the CPU. If the rotation method is used, the timer-interrupt () is triggered by a clock interruption and schedule () is called internally, causing a new round of scheduling. The current process is still in the running status, therefore, the current process is mounted to the end of the ruil-queue team.
If a CPU and running process cannot apply for a resource, sleep-on () or interruptible-sleep-on () is called to sleep, its task-struct process control block is mounted to the wait-queue of the corresponding resource. If sleep-on () is called, its status changes to non-Interrupting sleep, if interruptible-sleep-on () is called, its status changes to sleep interruption. Sleep-on () or interruptible-sleep-on () will call schedule () the function releases the sleep process.
3. Process classification and corresponding Process Scheduling Policies
In Linux, in order to efficiently schedule processes, processes are divided into two types: Real-Time Processes and common processes (also known as non-real-time or general processes). Real-time processes have a higher priority than other processes, if a real-time process is in the executable state, it will be executed first. There are two policies for Real-Time Processes: time slice rotation and first-in-first-out. In the time slice rotation policy, each executable real-time process executes a time slice in turn, while the first-in-first-out policy is executed by each process in the order of their respective running queues, and the sequence cannot be changed.
In Linux, three process scheduling policies are defined:
In Linux, each process is described in the task-struct structure. The process scheduling is based on the policy, priority, counter, and RT-priority in the task-struct structure. Policy data items are set in the PCB, its value is used to reflect the scheduling policies used for different types of processes. Sched-RR and SCHED-FIFO are used for Real-Time Processes, indicating rotation scheduling policy and first-in-first-out scheduling policy, while SCHED-OTHER indicates common process and also processing according to rotation scheduling policy. All three scheduling policies are based on priority. set the priority data item in PCB. Its value is the scheduling priority of common processes. the initial value of the available time slice of a common process is this value, which can be changed through system calls.
Set the RT-priority data item in the PCB. The value is the scheduling priority dedicated to the real-time process. The initial value of the available time slice of the real-time process is the value. The priority can also be modified by using a system call, the counter data item is set in the PCB to count the time slice values available for the process. The initial value is RT-priority or priority. After the process is started, the Count value decreases at any time.
Through a simple analysis of the Linux Process scheduling policy, we can see that multi-process management is a very complex concurrent program design, and the status of each process is not only determined by itself, it is also affected by many external factors. On the basis of process scheduling, many methods must be used to ensure the stability of the operating system, improve efficiency and increase flexibility, these are all worthy of our research and discussion.
References:
[1] Liu zhenpeng Li Yaping Wang Yu: Operating System [M]. Beijing: China Railway Publishing House, 2003
[2] Zhao mingfu, Li taifu, Chen Hongyan: real-time analysis of Linux embedded systems [J]. Computer Knowledge and Technology, pushed 29 (18): 53-55
This article from www.14edu.com (paper) Source: http://www.14edu.com/jingji/cyjjx/06044M042010_2.html