Fourth Chapter process scheduling
"Learning time: 1 hours 45 minutes compose a blog time: 2 hours 10 minutes"
"Learning content: Linux process scheduling implementation, preemption and context switching, scheduling-related system calls"
The scheduler is responsible for deciding which process to run, when to run, and how long it will run. The process scheduler can be seen as a kernel subsystem that allocates a limited amount of processor time resources between the running state processes.
The principle of maximizing processor time: whenever there is a process that can be executed, there is always a program executing.
One, multi-tasking
1. Concept: multi-tasking operating system is an operating system that can concurrently execute multiple processes simultaneously, on a single processor machine this will cause multiple processes to run at the same time, on a multiprocessor machine, which enables multiple processes to run simultaneously and concurrently on different processors.
- Multitasking operating systems can cause multiple processes to clog or sleep in a single-processor or multiprocessor machine, which means that they are not actually put into execution until the job is ready
- These tasks are not in a functioning state, although they are in memory. Instead, these processes use the kernel to block themselves until an event (the keyboard enters the network data, a period of time, and so on) occurs. Therefore, modern Linux systems may have 100 processes in memory, but only one is in a functioning state
2. Classification: Multi-tasking system can be divided into two categories
- Non-preemptive multi-tasking. The process is executed until it is actively stopped (this step is called concession)
- Preemptive multi-tasking. The Linux/unix uses a preemptive approach; the action of the forced suspend process is called preemption. The time a process can run before it is preempted is pre-set (that is, the time slice of the process)
Second, the Linux process scheduling
In the kernel of the Linux 2.5 Development series, the scheduler performed a major operation and began using a new scheduler called the O (1) Scheduler--it was named because of the behavior of its algorithms.
- It solves many of the shortcomings of the previous version of the Linux Scheduler, introducing many powerful new features and performance features, mainly thanks to the static time slice algorithm and the running queue for each processor, which helps us to get rid of the limitations of the previous scheduling program design
- The O (1) scheduler, while ideal for large server workloads, is poorly performing on desktop systems where many interactive programs are running because of the lack of interactive processes, Since the beginning of the 2.6 kernel system development, the developer introduced a new process scheduling algorithm to improve the scheduling performance of the interactive program, the most famous of which is the "inverse stair deadline scheduling algorithm, which absorbs the queue theory and introduces the concept of fair dispatch to the Linux scheduler." And finally replaced the O (1) scheduling algorithm in the 2.6.23 kernel version, which is now called the "fully Fair scheduling algorithm", or simply CFS
Third, the strategy
The policy determines when the scheduler will let the process run.
3.1 I/O consumption and processor-intensive processes
1. I/O consumption process
- Most of the process is used to commit I/O requests or wait for I/O requests
- Most user graphical interfaces (GUIs) are I/O intensive
2. Processor-intensive
- Time is mostly used to execute code.
- such as Matlab
- Often need to lengthen the running time and reduce the scheduling frequency
3. The scheduling strategy typically seeks to strike a balance between two conflicting objectives: rapid process response (short response time) and maximum system utilization (high throughput), in order to meet these requirements, the scheduler usually uses a very complex set of algorithms to determine the most worthwhile process to run, However, it is often not guaranteed that the low priority process will be treated fairly, the Unⅸ system Scheduler is more inclined to the I/O consumption program to provide better program response speed, Linux in order to ensure the performance of interactive applications and desktop system, so the response to the process is optimized (shorten the response to think of) more inclined to priority scheduling I /o consuming process, however, the scheduler does not ignore processor-consuming processes.
3.2 Process Priority
1. Priority-based scheduling: high-priority processes run first, and processes with the same priority are scheduled on a rotational basis.
2. priority is divided into two categories:
- Nice value (from -20--+19): The default value is 0; the larger the number means the lower the priority; You can view the list of system processes by Ps-el and find the priority of the NI tag column
- Real-time priority (from 0--99): higher real-time priority levels mean higher process priorities
Note: They do not interact with each other.
3.3 Time slices
- A time slice represents the time that a process can continue to run before it is preempted.
- The scheduling policy must determine a default time slice.
- The CFS scheduler for Linux does not divide the time slices directly into the process, but instead divides the processor usage into processes. That is, the timing of their preemption depends on how much processor usage is consumed by the new executable, and if the consumed usage is smaller than the current process, the new process immediately runs to preempt the current process.
3.4 Scheduling policy Activity iv. Linux Scheduling algorithm 4.1 Scheduler class
- The Linux scheduler is provided in a modular manner (that is, the Scheduler Class) to allow different types of processes to selectively select scheduling algorithms.
- The Scheduler class allows a variety of dynamically added scheduling algorithms to coexist, scheduling the processes that belong to their own category.
- Each scheduler has a priority, the underlying scheduler code is defined in the Sched_ fair.c file, it traverses the scheduling class in order of precedence, and the Scheduler class with the highest priority of an executable process wins, choosing the program to be executed below. The Complete Fair Dispatch (CS) is a scheduling class for ordinary processes, called Sched_normal in Linux.
Process scheduling in 4.2 Unix system
- If you map nice values to a time slice, you must match the nice value to the absolute time of the processor, which will cause the process switchover to be suboptimal.
- If you use a relative nice value, the effect will be greatly dependent on the initial value of Nice.
- If you perform a nice value to a time slice mapping, the time slice is greatly constrained by the timer.
The CFS approach is to fundamentally redesign the time-slice allocation (in terms of the process scheduler) by completely discarding the time slice rather than allocating it to the process, and in this way, the CFS ensures a constant fairness in the process scheduling and shifts the switching frequency to a constant change.
4.3 Fair Dispatch
CFS is based on a simple idea: the effect of process scheduling should be as good as the system has a perfect task processor. The CFS practices are as follows:
- Allow each process to run for a period of time, cycle round, select the least-run process as the next running process
- The nice value is the weight of the processor run ratio as the process gets (rather than the time slice determined entirely by Nice)
- Each process runs with a "time slice" corresponding to the proportion of its weights in all the running processes
Ideally, the perfect multitasking model should look like this: we can run two simultaneous processes within 5ms, each using half the capacity of the processor.
V. Implementation of Linux Scheduling
The CFS-related code is located in Kernel/sched_fair.c. It has four components:
- Time Billing
- Process Selection
- Dispatcher entry
- Sleep and wake up
5.1 Time Accounting
All schedulers must be billed for the process run time. Most UNIX systems allocate a time slice to each process. The time slice will be reduced by one cycle per tick when the system clock ticks occur.
1. Scheduler Entity Structure
The scheduler entity structure, as a member variable named SE, is embedded within the process descriptor struct task_ struct.
2. Virtual real-time
- Vruntime The virtual run time of the variable storage process
- Update_ Curr () calculates the execution time of the current process, which is called periodically by the system timer.
5.2 Process Selection
1. CFS algorithm core: Select the task with minimum vrntime
2. Practice: Using red-black tree rbtree (two-fork tree to store data as nodes)
3. for Example:
- Select Next task: Traverse the binary tree from the root node to the leaf node (i.e. the vrntime smallest process)
- Join a process to a tree: Create a process the first time it becomes executable or through a fork () call
- Remove a process from a tree: Occurs when a process is blocked or terminated
5.3 Dispatcher Entry
- The primary entry point for process scheduling is the function schedule (), which is defined in KERNEL/SCHED.C. This is where the internal and other parts are used to dispatch the process Scheduler's entry.
- The most important task of this function is to call Pick_ Next_ State (), examine each scheduling class in turn, and select the highest priority process from the highest priority scheduling class.
5.4 Sleep and wake up
1. Wait queue: Hibernation is processed by waiting for a queue, which is a simple list of processes that occur by certain events.
- The process marks itself as dormant, removed from the executable red-black tree
- Put into the wait queue-a list of processes that wait for some time to occur, and the kernel uses wake_ queue_ Head_ T to represent the waiting queue
- To add a process to a wait queue:
2. Wake up
The wake operation is performed by the function Wake_ up ():
- It calls the function Try_ to _ Wake_ up () sets the process to task_ running state, and calls the Enqueue_ task () to put the process into the red-black tree
- The state of a false wakeup process exists
Vi. preemption and Context switching
Context switching, which is the process of switching from one executable process to another, is handled by the CONTEXT_ switch () function defined in kernel/schedule.c. Two tasks have been completed:
- Call SWITCH_MM (), responsible for switching virtual memory from the previous process map to the new process
- Call Switch_to (), which is responsible for switching from the processor state of the previous process to the processor state of the new process
6.1 User preemption
When the kernel returns to the user space, it knows that it is safe, because since it can continue to execute the current process, it can of course choose a new process to execute. Therefore, whether the kernel returns after an interrupt handler or a system call, the need_resched flag is checked, and if it is set, the kernel chooses a different (more appropriate process to run. The return path returned from the interrupt handler or system call is architecture-related, in entry. S (this file contains not only the kernel entry part of the program, the kernel exit part of the relevant code is also in it) in the file is implemented through assembly language. In short, a user preemption occurs when the following conditions occur:
- When returning user space from system transfer.
- When returning user space from an interrupt handler
6.2 Kernel Preemption
Unlike most of the other Unⅸ variants and most other operating systems, Linux supports kernel preemption in its entirety, and in kernels that do not support kernel preemption, the kernel code can be executed until it is complete, that is, the scheduler has no way to reschedule when a kernel-level task is executing-- The tasks in the kernel are not preemptive because they are scheduled in a cooperative manner. Kernel code has been executed until completion (return to user space) or obvious blocking, in the 2.6 kernel, the kernel introduced the preemption capability, now, as long as the re-Dispatch is secure, the kernel can at any time preempt the task being performed.
Seven, real-time scheduling strategy
Linux real-time scheduling algorithm provides a soft real-time mode of operation, soft real-time meaning is that the kernel scheduling process, try to make the process before its limited time to run, but the kernel is not guaranteed to always meet the requirements of these processes. In contrast, the hard real-time system is guaranteed to meet any scheduling requirement under certain conditions. Linux does not guarantee the scheduling of real-time tasks. Although the hard real-time operation is not guaranteed, the performance of the Linux real-time scheduling algorithm is very good. The 2.6 version of the kernel can meet strict time requirements.
Viii. scheduling-related system calls 8.1 system calls related to scheduling policies and priorities
- Sched_ Setscheduler () and Sched_ Getscheduler () are used to set and get the scheduling policy and real-time priority of the process, respectively. Similar to other system invocations, their implementations are made up of many parameter checks, initialization, and cleanup. The most important job is to read or rewrite the process task_ struct's policy and Rt_ priority values
- Sched_ Setscheduler () and Sched_ Getscheduler () are used to set and get the real-time priority of a process, respectively. These two system calls get encapsulated in the Rt_ priority of the sched_ param special struct. The maximum priority of a real-time scheduling strategy: Max_ Userrt_prio minus 1. Minimum priority equals 1
- For a normal process, the nice function can increase the static priority of a given process by a given amount. Only a superuser can use a negative value when calling it, thereby increasing the priority of the process. The nice function calls the kernel's set_ user_ nice function, which sets the Static_ prio value of the task_ struct of the process
8.2 system calls related to processor bindings
The Linux Scheduler provides a mandatory processor binding mechanism. Although it tries to try to make the process run on the same processor as much as possible through a soft (or natural) affinity, it also allows the user to force the specified "this process must run on these processors anyway". This enforced affinity is stored in a bitmask flag of the process. Each bit of the mask flag corresponds to a processor that is available to the system, and all bits are set by default.
8.3 Discard Processor Time
Linux through the sched_ yield () system call provides a mechanism for the process to explicitly cede processor time to other waiting execution processes by moving the process from the active queue (because the process is executing, so it must be in this queue) to the expiration queue. The resulting effect not only grabs the process and puts it in the last side of the priority queue, but also puts it in an expired queue-which ensures that it will not be executed for a period of time, because the real-time process does not expire, so the exception is that they are moved only to the last side of their priority queue (not put into the expiration queue).
In earlier versions of Linux, the process was placed only at the end of the priority queue, and the time to discard was often not too long, and now the application and even kernel code should carefully consider whether or not to really want to discard processor time before calling Sched_ yield (). Kernel code for convenience, you can directly call Sched_ yield (), first to determine that a given process is actually in the executable state, and then call Sched_ yield (), the user space of the application directly using the Sched_ yield () system call can be.
Summarize
By learning about the process scheduling in this chapter, I learned that the process scheduler is an important part of the kernel because the running process is first using the computer. But satisfying the various needs of process scheduling is more difficult to achieve. For example, in the fair scheduling, the smaller the scheduling cycle will show the better interactivity, but also closer to the "simultaneous completion of multi-tasking" goal. However, the system must withstand higher switching costs and poorer system throughput, which means that both the fish and the paw cannot be combined. However, the new CFS scheduler for the Linux kernel is trying to meet all aspects of the requirements and provides the best solution in terms of better scalability and novel methods.
"Linux kernel Design and implementation" chapter fourth study notes