I. Scheduling Learning Method
1. Scheduling Policy (depending on which process uses CPU) 2. Scheduling time (when) 3. Scheduling steps (how to schedule)
Ii. Linux scheduling policy (in Linux, each process has its own scheduling policy, and the scheduling policy of the entire system is not unique ):
1. sched_normal (sched_other): Common time-sharing process
2. sched_fifo: Real-time process of time slice Rotation
3. sched_rr: Real-time process of time slice Rotation
4. sched_batch: Batch Processing Process
5. sched_idle: the process that can be scheduled only when the system is idle
Iii. scheduling class (the preceding five scheduling policies can be divided into two categories)
1. CFs scheduling class, completely fair scheduler, fair scheduling class (implemented in kernel/sched_fair.c), used for the following Scheduling Policies: sched_normal, sched_batch, sched_idle
2. Real-time scheduling class (implemented in kernel/sched_rt.c) for sched_rr and sched_fifo policies
4. Scheduling time (in Linux, the scheduling function is schedule (), so the scheduling time is to discuss when the schedule function is called)
1. There are two scheduling methods:
(1) active: in the kernel, the process directly calls schedule (). When the process needs to wait for resources and temporarily stop running, it will put the status in the suspended (sleep), and actively request scheduling to let out the CPU
Eg:
Current-> state = task_interruptible;
Schdule ();
(2) passive (preemption)
I. User preemption (in this case, the kernel space is returned to the user space ):
= Return the user space from the system call
= Return the user space from the interrupt handler
Ii. kernel preemption: higher-priority processes/Threads can seize low-priority processes/threads running in the kernel space
= Kernel preemption is not allowed in some special cases
-- The kernel is performing interrupt processing.
-- The kernel is processing the bottom half (bottom half of the interrupt) of the interrupt context.
-- The process holds the spin lock, writelock/readlock, and read/write locks. (A deadlock may occur if the process is preemptible)
-- The kernel is executing the scheduler.
= To ensure that Linux will not be preemptible in the preceding circumstances, the preempt_count variable is used in the preemptible kernel, which is called the kernel preemptible count. This variable is set in the thread_info structure of the process. When the kernel enters the preceding states, the preempt_count variable is added with 1, indicating that the kernel cannot be preemptible. When the kernel exits from the preceding states, the variable preempt_count is reduced by one and can be preemptible for judgment and scheduling. (Preempt indicates preemption.) In short, preempt_count> 0 cannot be preemptible. preempt_count
= 0 preemption can occur
= When kernel preemption occurs:
-- The interrupt handler is completed, before returning to the kernel space
-- When the kernel code is preemptible again, such as unlocking and soft interruption.
2. Scheduling flag, tif_need_resched: indicates whether the kernel needs to re-execute the scheduling.
(1) This flag is set when a process consumes its event slices.
(2) This flag is also set when a process with a higher priority enters the executable state.
5. Scheduling steps (the so-called scheduling steps are actually the workflow of the Schedule function)
1. Clear the currently running processes
2. Select the next process to run. (pic_next_task Analysis)
3. Set the running environment of the new process
4. process context switch