"Linux kernel design and implementation" CHAPTER4 reading notes
The scheduler is responsible for deciding which process to run, when to run, and for how long, and the process scheduler can be seen as a kernel subsystem that allocates a limited amount of processor time resources between the running state processes.
One, multi-tasking
A multitasking operating system is an operating system that can concurrently interact with multiple processes concurrently.
Multi-tasking systems can be divided into two categories:
- Non-preemptive multi-tasking
- The process will continue until it has voluntarily stopped running
- Preemptive multi-tasking
- The Linux/unix uses a preemptive approach; the action of the forced suspend process is called preemption.
Like all Unix variants and many other modern operating systems, Linux provides a preemptive multitasking model.
The time slice of the process: the time the process can run before it is preempted is pre-set.
Second, the Linux process scheduling
The O (1) scheduler, while still showing near-perfect performance and scalability in a multi-processor environment with several 10 (not hundreds) of processors, proves that the scheduling algorithm has some inherent weaknesses in scheduling response time-sensitive programs, These programs we call interactive process one it undoubtedly includes all programs that require user interaction.
So:
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 "complete Fair scheduling algorithm", or simply referred to as CFS.
III. Strategy 1. I/O consumption and processor-consumed processes
- 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
- Processor-intensive
- Time is mostly used to execute code.
- such as Matlab
- Often need to lengthen the running time and reduce the scheduling frequency
2. Process priority
The most basic class of scheduling algorithm is the priority-based scheduling, which is a process based on the value of the process and its demand for processor time to grade the idea.
Usual procedure:
High-priority processes run first, low after run, and the same priority processes are scheduled on a rotational basis (one after the other, repeating).
The priority is divided into two categories:
- The first is the Nice value (―20~+19)
- The default value is 0; the higher the value, the lower the priority; You can view the list of system processes by Ps-el and find the priority of the NI tag column
The second range is real-time priority (0~99):
- Higher real-time priority levels mean higher process precedence
3. Time slices
Time slice: A numeric value that indicates how long 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.
4. Activity of scheduling strategy Iv. Linux scheduling algorithm 1. Scheduler class
The Linux scheduler is provided in a modular manner, which is intended to allow different types of processes to selectively select scheduling algorithms. This modular structure is called the Scheduler class. Allows 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.
The scheduler code traverses the scheduling class in order of precedence, and the Scheduler class with the highest priority of an executable process wins, selecting the program to be executed below.
Process scheduling in a 2.Unix system
If you map the nice value to a time slice, you must match the nice value to the absolute time of the processor.
3. Fair Dispatch
The effect of process scheduling should be the same as the system-a perfect multitasking in the ideal, and we can dispatch them to an infinitely small time period, so that in any measurable cycle, we give the same number of running times for each process in a process.
5, the implementation of Linux scheduling 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.
2. Process Selection
The core of the CSF scheduling algorithm: Select the task with minimum and vruntime.
How to: Use Red-black tree rbtree (two-fork tree that stores data as nodes)
- Select Next Task
- Join a process to the tree
- Remove a process from the tree
3. Dispatcher entry
The primary entry point for process scheduling is schedule (), which is defined in file KERNEL/SCHED.C.
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.
4. Sleep and Wake up
Dormant (blocked) processes are in a special non-executable state.
Cause of process hibernation:
- Event may be a period of time to read more data from a file
- or a hardware event
- It is also possible to be forced into hibernation when trying to acquire an already occupied kernel semaphore
- A common cause is that the file i/o--, such as a process performing a read () operation on a file, which needs to be read from disk
- The process also needs to wait while getting the keyboard input
- In either case, the operation of the kernel is the same: the process marks itself as a hop-off state, moves out of the tree of the licensed course trees, puts it into the waiting queue, and then calls schedule () to select and execute the other process
- The process of awakening is the opposite: the process is set to the executable state and then moved from the wait queue to a clear red-black tree.
Vi. preemption and Context switching 1. User preemption
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.
ProduceUser preemption Scenario:
- When returning user space from system transfer
- When returning user space from an interrupt handler
2. Kernel preemption Linux fully supports kernel preemption, and in kernels that do not support kernel preemption, kernel code can be executed until it is complete. The scheduler has no way to reschedule when a kernel-level task is executing-the tasks in the kernel are not preempted by cooperative scheduling. Seven, real-time scheduling strategy
Soft real-time: Kernel scheduling process, trying to make the process run before its limited time, but the kernel does not guarantee that these processes can always meet the requirements
Hard real-time: The system is guaranteed under certain conditions, can meet any scheduling requirements.
Viii. scheduling-related system calls 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 overwrite the values of the policy and rt_priority of the process task_struct.
- 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 structure. The maximum priority of a real-time scheduling strategy: Max_ Userrt_prio minus 1. The minimum priority is equal to 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 task_struct Static_prio value of the process.
2. System calls related to processor bindings
The Linux Scheduler provides a mandatory processor binding mechanism.
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.
Ix. Summary
Process Scheduler is an important part of the kernel, but it is not easy to meet the various needs of process scheduling.
- It's hard to find a "one-size-fits-all" algorithm for many running processes
- Has scalability
- Balance between scheduled cycles and throughput
- Meet the demands of various loads
The new CFS scheduler of the Linux kernel tries 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" CHAPTER4 reading notes