Three kinds of process scheduling methods for operating system Linux kernel

Source: Internet
Author: User
Tags cpu usage

1,sched_other time- sharing scheduling strategy;
2,sched_fifo Real-time scheduling strategy, first-come-first service;
3,SCHED_RR Real Time scheduling strategy, time slice rotation.

Real-time process will get priority call, real-time process according to the real-time priority to determine the scheduling weights, timeshare process through nice and counter values determine weights, nice smaller, counter, the greater the probability of being dispatched, that is, used the CPU The least process will be prioritized.


The difference between SHCED_RR and Sched_fifo:

when the time slice of the process using the SHCED_RR policy is exhausted, the system will redistribute the time slices and place them at the end of the ready queue. Placing at the end of the queue ensures that all RR tasks with the same priority are scheduled fairly.

Sched_fifo is always running once the CPU is occupied. Run until a higher priority task arrives or abandons itself.

If a real-time process with the same priority (which is the same as the scheduling weights computed by the priority) is ready, the FIFO must wait for the process to be actively discarded before it can run the same priority task. The RR allows each task to execute for a period of time.


Same point:

both RR and FIFO are used for real-time tasks only.

The priority at creation is greater than 0 (1-99).

According to the preemptive priority scheduling algorithm.

Real-time tasks of ready state immediately preempt non-real-time tasks.


When all tasks are based on the Linux time-sharing scheduling strategy,

1, create a task to specify a time-sharing scheduling policy and specify a priority nice value ( -20~19).

2, the execution time on the CPU is determined based on the nice value of each task (counter).

3, if no resources are waiting, the task is added to the ready queue.

4, the scheduler traverses the task in the ready queue, through the calculation of the dynamic priority of each task (Counter+20-nice) results, select one of the most calculated results to run, when the time slice is exhausted (counter to 0) or the active abandonment of the CPU, The task will be placed at the end of the ready queue (the time slice runs out) or wait for the queue (the CPU is discarded as a result of waiting for the resource).

5, the scheduler repeats the above calculation process and goes to step 4th.

6, repeat the 2nd step when the scheduler finds that all the ready tasks calculate a weight of less than 0 o'clock.


When all tasks are FIFO

1, the process is created by specifying a FIFO and setting real-time priority rt_priority (1-99).

2, if no resources are waiting, the task is added to the ready queue.

3, the scheduler traverses the ready queue, according to the real-time priority calculation of the scheduling weights (1000+rt_priority), the highest-weighted task to use the CPU, the FIFO task will always occupy the CPU until a higher priority task is ready (even if the priority is the same) or actively discard ( Wait for the resource).

4, the scheduler discovers that a higher priority task arrives (the high-priority task may be interrupted or the timer task wakes up, or is awakened by the currently running task, and so on), the scheduler immediately saves all data for the current CPU register in the current task stack. When you reload the register data from the stack of the high-priority task to the CPU, the high-priority task begins to run. Repeat the 3rd step.

5, if the current task is actively abandoning the CPU usage due to waiting for the resource, the task will be removed from the ready queue, and the waiting queue is added, and the 3rd step is repeated at this time.


When all tasks are using RR scheduling policy

1, specify the schedule parameter as RR when creating the task, and set the task's real-time priority and nice value (the nice value will be converted to the length of the task's time slice).

2, if no resources are waiting, the task is added to the ready queue.

3, the scheduler traverses the ready queue, calculates the scheduling weights (1000+rt_priority) based on the real-time priority, and selects the task with the highest weights to use the CPU.

4, if the RR task time slice in the ready queue is 0, the task's time slice is set according to the Nice value, and the task is placed at the end of the ready queue. Repeat step 3.

5, the current task exits the CPU by waiting for the resource, then it joins the waiting queue. Repeat step 3.


In the system, there are time-sharing scheduling, temporal-chip rotation scheduling and FIFO scheduling.

The process of 1,RR scheduling and FIFO scheduling belongs to real-time process, and the process of scheduling with ticks is non-real-time process.

2, when the real-time process is ready, if the current CPU is running a non-real-time process, the real-time process immediately preempt the non-real-time process.

Both the 3,RR process and the FIFO process use real-time priority as the weighted standard for scheduling, and RR is an extension of the FIFO. FIFO, if two processes have the same priority, then which of the two priority processes is determined by their position in the queue, which leads to some unfairness (the priority is the same, why do you keep running?), if you set the scheduling policy for the two priority tasks to RR, The two tasks are guaranteed to be executed in a loop, guaranteeing fairness.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Three kinds of process scheduling methods for operating system Linux kernel

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.