Reprint Please specify the Source:Jiq ' s technical Blog
Data:
Each processor maintains a running queue, the primary field.
Each run queue has two priority queues, one active (time slice not finished ) , and the other is out of date ( time slice is finished ) . Each queue has a bitmap that is used to quickly find the highest priority in the current queue.
The Schedule () function performs the following steps:
(1) The current highest priority is found in the array of bitmaps in the active priority queue;
(2) Follow this priority value to find the priority group and then the process queue at that location ( with the same priority ) Remove the first one above;
(3) If the process is not currently running, do a task switch ( call the context_switch () function ) .
When to call Schedule () function?
(1) Current process hibernation ( blocked ) (Active) : Mark yourself as dormant, remove yourself from the executable queue into the wait queue, and then call Schedule () function;
(2) End of current process (active) : Perform do_exit () the last step of the function is to invoke the Schedule () function;
(3) be preempted (passive) : This is the most complex part, mainly by a "global flag" need_resched completed.
In both cases, this flag will be set:
( I When a process time slice is exhausted Scheduler_tick () function settings;
( II when a high-priority task enters the ready state try_to_wake_up () function settings;
Although the flag is set, but does not really start the scheduler, when to start it?
( I ) User preemption:
1 performing system calls ( implemented with interrupts ) when you return to user space Check need_resched flag to enable the scheduler if it is set:
2 Execute Interrupt Handler return to User space time Check need_resched flag, if set to enable the scheduler;
( II ) kernel preemption:
Note: Add a flag Preempt_count represents the currently running task (current) The number of locks held.
1 Interrupt handler return to kernel space check if need_resched flag set, and preempt_count flag value is 0
2 Current Process code to execute a release lock will check Preempt_count If the value is 0 ( without holding the lock ) and set the need_resched flag, the scheduler is enabled;
3 Cores Show Call Schedule () function;
Note: You can use preempt_disable ()/preempt_enable ( ) function to disallow and allow kernel preemption.
Linux Process scheduling policy:
Multi-tasking, preemptive.
Based on priority scheduling, time slice rotation.
Dynamic priority, dynamic time slice.
or say:
Priority high first run, low after run, the same priority of the rotation schedule.
Scheduler always chooses the time slice is not exhausted and the highest priority process runs !!!
How is the dynamic priority calculated?
Two priority levels per task:
(1) a static priority. As the initial priority + as the basis for calculating the dynamic priority level;
(2) a dynamic priority. Its value = static priority + interactivity strength ( calculated based on task sleep time );
because we need to dynamically improve the interactive I/O the priority of the consumed task, I/O The consumption-type task performs high frequency, short execution time, long sleep time and high interactivity intensity, so it leads to high dynamic priority.
How is a dynamic time slice calculated?
the higher the priority, the longer the time slice, Nice value ( Priority series value ) the smaller, the higher the frequency of execution!!!
(e.g. I/O consumption type )
Visible priority and time slice length are proportional!!!
So we just need to scale the priority to a certain scale, according to the time slice of the value range, is the time slice value.
How do you ensure that highly interactive tasks, such as I/O consumption, are performed more frequently?
in the Schedler_tick () in the function:
(1) The current task time slice minus one operation;
(2) If it is reduced to zero, it is judged that if there is a "hunger process" in a task that is not interactive or an expired array, the current task is added to the expired array;
Otherwise join the active array ( This guarantees a higher frequency of execution ) .
This means that each clock cycle is checked once, and the interactive task is then put back into the active array, allowing it to be executed again as soon as possible.
Linux Kernel (iii) process scheduling