"Linux kernel Design and implementation" Chapter 4th reading notes: Process scheduling

Source: Internet
Author: User

Fourth Chapter process scheduling

Order

The process appears to be the manifestation of the operating state of the program.

4.1 Multi-tasking

Multi-tasking operating system: concurrently interactively executing multiple processes

Multitasking operating systems can cause multiple processes to clog or sleep. These tasks, although they are in memory, are not in a functioning state. These processes use the kernel to block themselves until an event occurs.

Multi-tasking systems can be divided into two categories: non-preemptive and preemptive.

Preemption: Forced suspend.

Time slices: The processor time period that is assigned to each running process.

Process scheduling for 4.2 Linux

O (1) Sculpture program

4.3 strategy

The policy determines when the scheduler will let the program run.

4.3.1 I/O consumption and processor-intensive processes

I/O consumption: Most of the time the process is used to commit I/O requests or wait for I/O requests. Such a process is often operational, but usually runs for just a few moments because it is always blocked while waiting for more I/O requests.

Processor consumption type: Most of the time is spent on executing code. Unless they are preempted, they are generally kept running. Their scheduling strategy is usually to minimize their dispatch frequency and prolong their uptime.

Scheduling Policy requirements: 1, short response time 2, high throughput

4.3.3 Process Priority

Thought: Grading processes based on the value of the process and its need for processor time

The scheduler always chooses the process in which the time slice is not exhausted and the highest priority is run.

Linux uses two different priority ranges:

1, nice value: Range -20~+19, default value is 0; the larger nice value means lower priority. High-priority processes can get more processor time.

2, real-time priority: The value is configurable, by default the range is 0~99. The higher the real-time priority value means the higher the process priority. Any real-time process has a higher priority than a normal process. Refer to the UNIX-related

Standard: Posix.1b.

Real-time priorities and nice priorities are in the two areas of disjoint.

4.3.3 Time Slice

Too long a time slice can cause the system to respond poorly to interactions, and too short can significantly increase the processor time that is brought by process switching.

The processor time obtained by the process is related to the system load nice value.

4.4linux Scheduling algorithm

4.4.1 Scheduler Class

The Linux scheduler is provided in a modular manner, allowing different types of processes to selectively select the scheduling algorithm. This modular structure is called the Scheduler class.

Each scheduler has a priority, the underlying scheduler code is defined in the Kernel/sched.c file, it traverses the scheduler in order of precedence, and the Scheduler class with the highest priority of an executable process wins, choosing a program to execute below.

Fully Fair Dispatch (CFS): The scheduling class for a common process, known in Linux as the SCHED_NORMAL,CFS algorithm, is defined in the file KERNEL/SCHED_FAIR.C.

4.4.4 process scheduling in UNIX processes

The following issues exist:

1. To map a nice value to a time slice, it is necessary to correspond the Nice unit value to the absolute time of the processor, but doing so will result in process switching not being optimized.

2. The effect of reducing the nice value of the process by 1 greatly depends on the initial value of its nice.

3. If you perform a nice value to time slice mapping, the time slice must be an integer multiple of the timer beat, the system timer limits the difference of two time slices.

4, priority-based scheduler in order to optimize the interactive task to wake up the relevant process, in order to process can be put into operation as soon as possible, and to the new to wake up the process of priority, even if their time slice has been exhausted, so that a given process to break the principle of fairness, to obtain more processor time, damage to other processes in the system

Improvements to CFS:

Completely discard the time slices but assign to the process a processor to use the weighting.

4.4.3 Fair Dispatch

We want the process to run only a very short period, and the CFS implementation first ensures that the system performance is not lost.

CFS approach: Allow each process to run for a period of time, cycle round, select the least-run process as the next running process, instead of taking the practice of allocating time slices to each process. The nice value is the weight of the processor running in CFS as the process gets: the higher the nice value process gets the lower the processor usage weight.

How to calculate the accurate time slice?

CFS sets a "target delay" for the approximate value of the infinitely small scheduling period in the perfect multitasking, and the smaller the scheduling cycle brings the better interactivity and closer to the perfect multi-tasking. CFS introduces the time slice bottom line that each process obtains-the minimum granularity. By default, this value is 1ms. Even if the number of running processes tends to be infinite, at least 1ms uptime can be achieved, ensuring that switching consumption is limited to a certain range.

Summary: The processor time obtained by any process is determined by the relative difference of the nice value of its own and all other operational processes. Nice values are geometrically weighted against time slices, and the absolute time corresponding to any nice value is no longer an absolute value, but rather a processor's use ratio.

Implementation of 4.5 Linux scheduling

4.5.1 Time Accounting

The time slice is reduced by one tick cycle each time the system clock beats occur. When the time slice of a process is reduced to 0, it will be preempted by another time slice that has not yet been reduced to 0.

1. Scheduler entity structure: Used to track process running bookkeeping

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, which is calculated by standardizing the total number of running processes. The virtual time is in NS, so it is no longer relevant to the timer Street beat. CFS uses the Vruntime variable to record how long a program is running and how long it should run.

Update_curr () Calculates the execution time of the current process and stores it in the variable delta_exec. Then he passes the run time to __update_curr (), which then calculates the run time based on the total number of currently running processes, and finally adds the above weights to the vruntime of the current running process.

Update_curr () is called periodically by the system counter.

4.5.2 Process Selection

The core of the CFS scheduling algorithm: Choosing a process with minimum vruntime worth.

CFS uses a red-black tree to organize a running process queue and use it to quickly find the minimum vruntime worth process.

1. Pick the next task

The process selection algorithm of CFS can be summed up as "the process represented by the leftmost leaf node in the run Rbtree tree." The function to implement this procedure is __pick_next_entity ().

2. Join the process to the tree

Occurs when the process becomes operational (wake-up) or the first time the process is created through a fork () call. The enqueue_entity () function achieves this purpose.

The function updates the run time and some other statistics, and then calls __enqueue_entity () for a heavy insert operation that actually inserts the data item into the red-black tree.

3. Remove a process from the tree

Delete actions occur when a process is blocked or terminated.

4.5.3 Scheduler Entry

The main entry point for process scheduling is the function schedule (), which is the portal that other parts of the kernel use to invoke the process Scheduler: Select which process can run and when it will be put into operation. The only important thing in this function is to call Pick_next_task (), which takes precedence, from high to low, checks each schedule class at a time, and selects the highest-priority process from the highest-priority scheduling class. The core of the function is the for () loop, which implements the traversal.

4.5.4 Sleep and wake up

Hibernate: The process marks itself as dormant, moves out of the executable red-black tree, puts in the wait queue, and then calls schedule () to select and execute a different process.

Wake up: The process is placed in an executable state and then moved from the wait queue to the executable red-black tree.

There are two related process states for hibernation: Task_interruptible and task_uninterruptible. The only difference is that the task_uninterruptible process ignores the signal, and the process of the task_interruptible state receives a signal that wakes up early and rings the signal. The signals in both states are on the same waiting queue, waiting for certain events to run.

1. Waiting queue

The process adds itself to a wait queue by following several steps:

2. Wake up

Through the function wake_up (), it wakes up all the processes on the specified wait queue. It calls the function try_to_wake_up (), which is responsible for setting the process to the task_running state, calling Enqueue_task () to place the process into a red-black tree, setting the need if the awakened process has a higher priority than the currently executing process. _resched logo.

4.6 Preemption and Context switching

This is handled by the Context_switch () function defined in KERNEL/SCHED.C. The function is called by schedule () when a new process is selected to be ready to run. It completed the following two basic tasks:

Invoke SWITCH_MM () declared in <asm/mmu_contesxt.h>, which is responsible for switching virtual memory from the previous process map to the new process.

Invoke Switch_to () declared in <asm/system.h>, which is responsible for switching from the processor state of the previous process to the processor state of the new process.

4.6.1 User preemption

Occurs in:

When the user space is returned from the system call.

When the user space is returned from the interrupt handler.

4.6.2 kernel preemption

As long as the lock is not held, the kernel can take a preemption.

Occurs in:

The interrupt handler is executing and before the kernel space is returned.

The kernel code once again has the time to be preemptive.

If the task in kernel space explicitly calls schedule ().

If the task in the kernel is blocked. (Schedule () is also called).

4.7 Real-Time scheduling strategy

Linux offers two real-time scheduling strategies: Sched_fifo and SCHED_RR.

Sched_fifo: First-in first-out algorithm.

SCHED_RR: First-in first-out algorithm with time slices.

4.8 Scheduling-related system calls

"Linux kernel Design and implementation" Chapter 4th reading notes: Process scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.