Linux Kernel Study Notes (6) Process Scheduling

Source: Internet
Author: User
Linux Kernel notes-Process Scheduling

Keywords: Linux kernel Process Scheduling

 

Linux Kernel notes-Process Scheduling

Original article:

Http://www.linuxforum.net/forum/showthreaded.php? Cat = & board = linuxk & number = 294463 & page = 5 & view = collapsed & SB = 5 & O = all

Linux Kernel notes-Process Scheduling

1 Preface
2 Scheduling Algorithm
2.1.1 common concepts
2.1.2 Process Data Structure
2.1.3 Scheduling Algorithm Description
2.1.4 related functions
3. Execution of the Scheduler
3.1.1 direct call
3.1.2 delayed call
3.1.3 related functions
4. Process Scheduling
5. SMP System Scheduling
6. Questions and answers
7 References

1 Preface
The license agreement in this article complies with the GNU free document license. For more information, see http://www.gnu.org/copyleft/fdl.html. You can freely disseminate or publish this article based on the GNU free document license, but keep the integrity of this article.
You are welcome to comment on this article and correct, my email is: shisheng_liu@yahoo.com.cn.

2 Scheduling Algorithm
2.1.1 common concepts
2.1.1.1 scheduled interruption
Implemented through the hardware Programmable Interrupt Controller 8254, the frequency of scheduled interruption is defined by Hz, and the interval of occurrence is called tick.
2.1.1.2 CPU cycle (tick)
A counting unit of the computer's internal time, indicating the time interval at which a clock interruption occurs.
2.1.1.3Hz
The frequency of clock interruption. On the i386 machine, Hz is defined as 100, so the clock is interrupted every 10 ms.
2.1.1.4 CPU Period
Scheduling is based on the CPU period. During a CPU period/scheduling period, all programs in the system are executed until the current time slice is used up, and the counter values of all processes are recalculated, and start another CPU period.
2.1.1.5 process Classification
In the view of the scheduler, the processes in the system are divided into two categories: Real-Time Processes and common processes. At any time, the execution of real-time processes is higher than that of common processes. The policy member variable in the process data structure indicates the type of process, while sched_set (/get) scheduler provides a user-level programming interface for controlling the process scheduling policy.
2.1.2 Process Data Structure
2.1.2.1 1. Nice
The priority of the process, the number of CPU events that affect the process, 20 is the lowest, and-19 is the highest.
2.1.2.2 2. Counter
The number of CPU beats remaining for the process time slice. The initial value is determined by the Nice value of the process. When a clock interruption occurs, that is, a CPU cycle (tick), the counter value of the current process is reduced by 1, if the counter value is 0, it indicates that the time slice of the current process has been used up, and the system will reschedule the process to determine the next process to be executed.
2.1.2.3 3. need_resched
Flag. This bit is checked when it is returned from the interrupt and system call. When need_resched is set to 1, it indicates that the scheduler is required to be started. This usually occurs when the time slice of the process is used up, or when the current process is forcibly preemptible due to an IO event.
2.1.2.4 4. Policy
The scheduling policy of the process. If the scheduling policy is sched_rr or sched_fifo, the current process is a real-time process; otherwise (sched_other) is a common process.
2.1.3 Scheduling Algorithm Description
Linux uses a simple but proven scheduling algorithm. During scheduling, the execution weights (goodness) of all running processes are calculated, and the processes with the highest final weights receive the execution opportunity. If the obtained maximum weight is 0, it is deemed that the current CPU period has been completed, and the counter value of all processes will be recalculated to start a new CPU period. The core of the scheduling algorithm is goodness computing. The basic idea of computing is as follows:
If the waiting process is a real-time process, its goodness is the priority of 1000 +, while the goodness of a common process is much less than 1000, this ensures that the real-time process always takes precedence over normal process execution.
If the remaining counter of the process is 0, it is deemed that it has used up its CPU time slice in this period, goodness returns 0.
For other cases, use the following formula to calculate goodness:
Goodness = counter + 20-Nice;
2.1.4 related functions
1. Schedule () in kernel/sched. c
Main scheduling function, select the process to run
2. Goodness () in kernel/sched. c
Called by Schedule () to calculate the execution weight of a process
3. Execution of the Scheduler
You can activate the scheduler in two ways: direct call and delayed call.
3.1.1 direct call
When the current process is about to take the initiative to discard the CPU, it will directly call the scheduler schedule () and give the CPU to another process.
There are two reasons for the current process to voluntarily give up the CPU: one is that current needs to sleep (BLOCKED) to wait for the required resources to be ready. At this time, the current status is set to task_interruptable or task_uninterruptable, after schedule () is called, the process enters the sleep state. In another case, the process sets the sched_yield scheduling policy and then calls schedule (). At this time, the process only gives up the CPU temporarily, the process will continue to participate in CPU competition when schedule () is called in the next time.
3.1.2 delayed call
By setting the need_resched flag of the current process, the scheduler is activated at a later time. As mentioned above, the need_resched flag is checked when it is returned from the interrupt/exception/system call, And the scheduler is activated when the flag is not 0. For example, when the clock interrupt occurs, the interrupt handler checks that the time slice of the current process has been executed, and then sets the need_resched flag of the current process. In another example, when an IO interrupt occurs, the interrupt handler finds that a process is waiting for this Io event. It changes the status of the waiting process to the execution state and sets the need_resched flag of the current process. When the interrupt handler ends [NOTE 1], the system will reschedule the process. In this case, the new process transferred to the execution state may be given the execution opportunity, this allows the system to quickly respond to Io events.
[NOTE 1]: if the current process runs in the kernel state, that is, when the system call is being executed, re-scheduling will be delayed until the system call of the current process is completed.
3.1.3 related functions
1. wake_up_common () in kernel/sched. c
Activate the processes in the IO wait queue. It calls functions such as try_to_wake_up () and reschedule_idle () to reschedule the process in sequence.
2. do_timer () in kernel/Timer. c
The scheduled clock interrupt program reduces the counter value of the current process. If the counter is used up, the need_resched domain of the process needs to be rescheduled.
3. ret_from_intr/sys_call/exception in arch/i386/entry. s
The program points in the assembly language will execute this program when they are returned from the interrupt/exception/system call, and check the need_resched domain of the current process. If it is not 0, schedule () will be activated () reschedule.

4. Process Scheduling
Linux Process Scheduling 1 is shown in.

 
5. SMP System Scheduling
Different scheduling algorithms in the SMP system are mainly manifested at the end of the scheduling algorithm. The schedule_tail function is called for the process that is switched out of the current CPU running right, the purpose is to transfer it to another CPU.

6. Questions and answers
Q. What is the length of the time slice in the current system?
A. compared with the kernel of 2.2.x, the length of the time slice in kernel2.4.x is shortened. For processes with the highest priority, the length of the time slice is 100 ms, and the default length of the time slice in the priority process is 60 ms, the time slice length of the process with the lowest priority is 10 ms.
Q. in Linux, how does one ensure a relatively fast response to I/O events? Is the response speed related to the length of the scheduled time slice?
A. when an I/O event occurs, the corresponding interrupt handler is activated. When it finds that a process is waiting for this I/O event, it activates the waiting process, and set the need_resched flag of the currently executing process. In this way, when the processing program returns an interrupt, the scheduler is activated and the process waiting for the I/O event (probably) gets the execution right, this ensures a relatively fast response (in milliseconds) to I/O events ).
From the above description, we can see that when an I/O event occurs, the I/O event processing process will seize the current process. The system's response speed is irrelevant to the length of the scheduling time slice.
Q. What is the difference between a nice process and a low-priority process? For example, what is the difference between a process with a priority of-19 (highest priority) and a process with a priority of 20 (lowest priority )?
A. The absolute number of CPU time obtained by the process depends on its initial counter value. The formula for calculating the initial counter (sched. c In kernel 2.4.14) is as follows:
P-> counter = (p-> counter> 1) + (20-p-> nice)> 2) + 1)
According to the formula, for a standard process (p-> nice is 0), the initial counter is 6, that is, the time slice obtained by the process is 60 ms.
The initial counter value of the highest priority process (nice is-19) is 10, and the time slice of the process is 100 ms.
The initial counter value of the process with the lowest priority (Nice is 20) is 1, and the process time slice is 10 ms.
The conclusion is that the process with the highest priority will obtain the execution time of the process with the lowest priority by 10 times, and the execution time of normal process with the highest priority is nearly twice. Of course, this is the data when the process does not perform any Io operations. When there are Io operations, the process is often forced to sleep to wait for the completion of Io operations, the actual CPU usage is difficult to compare.
We can see that each time the counter is re-computed, the new counter value must be added with half of its remaining value. This reward is only applicable to the process where sched_yield takes the initiative to discard the CPU, the counter value is not used up only when it is re-computed. Therefore, the counter value increases after calculation, but never exceeds 20.
Q. Can a process be preemptible during system calling?
A. The process will not be preemptible during the execution of system calls. Let's imagine that process a is being executed and the interrupt processing program determines that the system needs to be rescheduled, it sets the need_resched flag of process A (see the previous description for the role of the need_resched flag), after the end of the interrupt handler (ret_from_intr ), the system checks the priority of processes interrupted by the interrupt handler. If process a is in the user State, the system will directly activate the scheduler to complete process switching; if process a is in the kernel state at this time, the system will resume the execution of process a without scheduling. Only after process a completes the system call (ret_from_sys_call ), its need_resched flag will be checked to complete the process switching.
The features that the process is not preemptible in the kernel state reduce the complexity of kernel design in a single CPU system, because the competition between different processes on the kernel code and data structure does not need to be considered.

7 References
1. Linux kernel source code version 2.4.14
Real code always provides us with the most accurate and detailed information at any time. Thanks to Linus Torvalds, Alan Cox, and other Linux developers for their hard work.
2. Daniel P. bovet & Marco cesati
< > ISBN: 0-596-00002-2 o'reilly 2001
Translated by Chen Lijun, a Chinese version of the Linux kernel, translated by ISBN: 7-5083-0719-4 China Power Press 2001.
This book is the most detailed book dedicated to introducing the Linux kernel structure. It also provides in-depth analysis on the Code. It is based on the 2.2 kernel version.
3. W. Richard Steven s
Advanced Programming in UNIX environment: ISBN: 7-111-07579-x Mechanical Industry Press 2000
Unix programming Bible, one of the essential books for programmers, has reference value for all UNIX developers, regardless of their level. The level of translation is also rare.

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.