The process scheduler can be seen as a kernel subsystem that allocates a limited amount of processor time resources between running too processes. The scheduler is the basis for multitasking operating systems. Through the reasonable scheduling of the scheduler, the system resources can play a maximum role, the multi-process will have the effect of concurrent execution.
The principle of maximizing processor time is that as long as there are processes that can be executed, there will always be processes executing.
4.1 Multi-tasking
A multitasking operating system is an operating system that can concurrently interact with multiple processes concurrently. Multitasking can cause multiple processes to clog or sleep. Is that the task is in memory but not executed until an event occurs.
Classification:
L Non-preemptive: it is executed unless the process itself stops running on its own initiative. The process actively hangs its own operation called concession. Cons: Process exclusive time may be beyond the thought. The process of never yielding will collapse.
L Preemptive: (Linux provided) the scheduler decides when to stop a process so that other processes can be executed. The time a process can run before being preempted is fixed, called a time slice. The effective management of time slices can make scheduling decision from the system global angle, and can avoid the exclusive system resources of individual processes.
4.2 Linux the process scheduling
Prior to version 2.4, the Linux scheduler was fairly rudimentary. 2.5 in the kernel of the development series, the O (1) Scheduler is starting to be used. O (1) Time-sensitive procedures are inherently deficient. The O (1) scheduler, while ideal for large server workloads, introduces a new process scheduling algorithm to the scheduling performance of desktop interaction programs that have many interactive programs to run. In order to improve the scheduling of interactive programs, the algorithm draws the queue theory and introduces the concept of fair dispatch to the Linux scheduler. "Complete Fair scheduling algorithm", referred to as CFS.
4.3 Strategy
I/O consumption and processor-consumed processes: The former refers to the process most of the time used to submit I/O requests or wait for I/O requests. The latter process uses most of the time to execute the code, unless it is preempted and runs continuously. For such processes, the scheduling strategy is to minimize their scheduling frequency. A scheduling strategy usually seeks to balance the two conflicting goals.
Process priority: The most basic class of scheduling algorithm is priority-based scheduling. Linux uses two different priority ranges. The first kind uses Nice value ( -20~+19), the larger the nice value means the lower priority (inversely proportional). The second range is the real-time priority (default 0~99), its value is configurable, and the value is proportional to the priority.
Time slice: Indicates how long a process can continue to run before it is preempted. The scheduling policy must specify a default time slice. Too long a time slice can cause the system to respond poorly to interactions, and too short can significantly increase processor consumption from process switching. I/O consumption does not require a long time slice, and the longer it takes to process it, the better.
Scheduling policy activity: Ideally, the scheduler should give the editing program more processor time than the video codec program. You need to rely on the system to assign a higher priority and more time slices to the text editor than the video decoder program.
4.4 Linux Scheduling Algorithm
Scheduler class: Linux is provided as a module to allow different types of processes to selectively select scheduling algorithms. The Complete Fair Dispatch (CFS) is a scheduling class for normal processes.
Process scheduling in UNIX systems: processes with high priority will run more frequently, will also be given more time slices, and the priority on Unxi is output to user space on a nice value, which will result in process switching not being optimized. Compared to Nice values, the effect of good is well worth the change, depending on the initial value. If you perform a nice value to a time slice mapping, we need to be able to allocate an absolute time slice, which requires that the time slice must be an integer multiple of the timer beat, so.
Fair Dispatch: The concept of CFS: The effect of process scheduling should be as good as the ideal multitasking processor in the system. Each process can obtain 1/n processor time. But this is just the ideal state. But it still needs to be as small a cycle as possible. The CFS approach is to allow each process to run for a period of time, cycle-by-turn, and select the least-run process as the next running process. When the number of processes tends to infinity, the cycle tends to zero then the switching consumption can not be answered, so there is a minimum granularity, is the smallest time slice, the default is 1ms.
4.5Linux implementation of scheduling
Related code in KERNEL/SCHED_FAIR.C, there are four components: time accounting, process selection, scheduler entry, sleep and wake.
Time Accounting: All schedulers must be billed for the process run time. CFS to ensure fairness.
Process selection: CFS will pick a process with minimal vruntime. CFS uses a red-black tree (self-balancing binary tree) to organize the running process queue and quickly find the vruntime smallest process in it.
Dispatcher entry: The primary entry for process scheduling is the function schedule ().
Sleep and wake: Process hibernation mechanism, the process is marked as dormant, it will be removed from the red black tree. Awakening is the opposite.
4.6 preemption and Context switching
Context switch: Switches from one executable process to another.
User preemption: When the kernel is about to return to user space, if the need_resched flag is set, it will cause schedule () to be called and user preemption will occur. Occurs when the user space is returned from the interrupt handler when the user space is returned from the system, in the following cases.
Kernel preemption: Linux supports kernel preemption in its entirety. As long as the control room is safe, the kernel can preempt the tasks that are being performed at any time. How to be safe: Just do not hold the lock on the line.
"Linux kernel design and implementation" Reading notes 4 process scheduling