Three Scheduling Methods for Linux kernel:
1. SCHED_OTHER time-based scheduling policy,
2. SCHED_FIFO real-time scheduling policy, first served
3. SCHED_RR real-time scheduling policy, time slice Rotation
Real-time processes will receive priority calls. Real-time processes determine the scheduling weights based on the real-time priority. Time-based processes use nice and counter values to determine the weights. The smaller the nice value, the larger the counter value, the higher the probability of being scheduled, that is, the process that used the least cpu will receive priority scheduling.
SHCED_RR and SCHED_FIFO are different:
When the time slice of the process using the SHCED_RR policy is used up, the system will re-allocate the time slice and place it at the end of the ready queue. Putting it at the end of the queue ensures fair scheduling for all RR tasks with the same priority.
SCHED_FIFO runs continuously once it occupies the cpu. Run until a higher-priority task arrives or you give up.
If a real-time process with the same priority (the scheduling weights calculated based on the same priority are the same) is ready, the task with the same priority can be run only after the process voluntarily gives up in FIFO. RR allows each task to be executed for a period of time.
Similarities:
RR and FIFO are only used for real-time tasks.
The priority is greater than 0 (1-99) during creation ).
It is executed according to the preemptible priority scheduling algorithm.
Ready real-time tasks immediately seize non-real-time tasks.
When all tasks adopt the linux time-based scheduling policy.
1. Create a task and specify the time-based scheduling policy and the nice value (-20 ~ 19 ).
2. the execution time (counter) on the cpu is determined based on the nice value of each task ).
3. If no resource is waiting, add the task to the ready queue.
4. The scheduler traverses the tasks in the ready queue. By calculating the dynamic priority of each task (counter + 20-nice) result, the scheduler selects the largest one to run, when the time slice is used up (counter is reduced to 0) or the cpu is automatically abandoned, the task will be placed at the end of the ready queue (the time slice is used up) or waiting queue (cpu is abandoned due to waiting for resources).
5. At this time, the scheduler repeats the above calculation process and goes to step 2.
6. When the scheduler finds that the weights calculated by all ready tasks are not greater than 0, repeat Step 1.
When FIFO is used for all tasks,
1. Specify FIFO when creating a process and set the real-time priority rt_priority (1-99 ).
2. If no resource is waiting, add the task to the ready queue.
3. The scheduler traverses the ready queue and calculates the scheduling weight (1000 + rt_priority) based on the real-time priority. The task with the highest selection value uses the cpu, this FIFO task will occupy the cpu until a higher priority task is ready (even if the priority is the same) or voluntarily give up (waiting for resources ).
4. The scheduler finds that a task with a higher priority has arrived (a task with a higher priority may be interrupted or awakened by a timer task, or by a running task ), the scheduler immediately saves all the data of the current cpu register in the current task stack, and re-loads the register data from the stack of the high-priority task to the cpu. At this time, the high-priority task starts to run. Repeat Step 1.
5. If the current task voluntarily gives up cpu usage because it is waiting for resources, the task will be deleted from the ready queue and added to the waiting queue. At this time, repeat Step 1.
When all tasks adopt the RR scheduling policy
1. Specify the scheduling parameter as RR when creating a task, and set the real-time priority and nice value of the task (nice value will be converted to the length of the time slice of the task ).
2. If no resource is waiting, add the task to the ready queue.
3. The scheduler traverses the ready queue and calculates the scheduling weight (1000 + rt_priority) based on the real-time priority. The task with the highest selection value uses the cpu.
4. If the RR task time slice in the ready queue is 0, the time slice of the task is set based on the nice value, and the task is placed at the end of the ready queue. Repeat Step 3.
5. If the current task actively exits the cpu because it is waiting for resources, it is added to the waiting queue. Repeat Step 3.
In the system, there are both time-based scheduling, time slice rotation scheduling, and first-in-first-out scheduling.
1. The RR and FIFO scheduling processes are real-time processes. The time-based scheduling processes are non-real-time processes.
2. When the real-time process is ready, if the current cpu is running a non-real-time process, the real-time process immediately grabs the non-real-time process.
3. Both the RR process and the FIFO process adopt the real-time priority as the scheduling weight standard, and the RR is an extension of the FIFO. In FIFO, if the two processes have the same priority, which of the two processes with the same priority is determined by their position in the queue, this leads to some unfairness (the priority is the same, why keep you running ?), If the Scheduling Policies of two jobs with the same priority are set to RR, the two jobs can be executed cyclically and fairly.