Preemptive scheduling preemptive scheduling linux

linux preemptive scheduling

Why scheduling will happen?
 
Because the cpu is limited, and a lot of processes on the operating system, the operating system needs to balance the running time of each process
For example, some running time has been a long process, and has occupied the cpu for a long time, and this time the operating system to be fair
Will be replaced by a process needs to run.
 
for example
 
The company has only one used to access drinking water, there are many people lined up, someone take over a glass of water and took a glass of water, a lot of glasses of water a connection, this time the company's personnel to come, this man away
A person then replaced the water, but this time the boss came to pick up the water, this time to pick the next person is the owner of the water, rather than the back of the queue of people, because of what? Because he is the boss, so tough
 
This is inside the dispenser is cpu
Personnel is the operating system
Take water and other employees is a process
 
The boss of the corresponding operating system in real-time process
Ordinary employees corresponding to the normal process of the operating system
 
Priority real-time processes operating system which is higher priority than the ordinary process,
So the operating system in the next selection process will give priority to real-time process queue inside the process
Real-time process unless there is no process, and this time come the ordinary process
 
 
Operating system like personnel, requires a fair scheduling and allocation of resources, of course, there are many company bosses, ah, ah, general manager of the leadership ah
These people have the privilege, when all assigned resources to give priority to these bosses leaders, this is normal, it is the boss who let others
 
 
 
Here's what preemptive scheduling
 
Preemptive scheduling under what circumstances it will happen?
The most common phenomenon is that you run the process too long, is to switch to another process when the
 
However, the operating system how to count the running time of it?
 
Concept of computer clocks, each over a period of time, the computer will notify the operating system, tells the operating system, some time has passed, you go and see, currently running processes running time is not too long, this time operating system this will engage in a process.
 
In the operating system has a variable ideal running time for each process, then there corresponds to a virtual runtime and the actual running time and weight (priority).
 
These three have to do with it?
 
Virtual runtime vruntime + = actual running time delta_exec * NICE_0_LOAD / weight (priority)
 
You can see by this formula out to the virtual runtime high-priority process is considered less, to the virtual runtime low-priority process is considered much, but when the operating system selects the next process or when selecting virtual runtime minimal process, so that the priority there is manifested.
 
When a process is running, the virtual running time of this process will increase, when the process is not running, the virtual time does not increase
In fact, this will involve the concept scheduler.
 
Here we just say absolutely fair scheduling process for the common strategy
 
This strategy when selecting the next process, is how to select it?
He is to select all the ordinary processes of the current process run time at least, this should be well understood, because at least you have taken in the cpu time, so to be fair, we should choose you this process running on the cpu.
 
Maintain the operating system in a red-black tree, red-black tree is a balanced binary tree, that is a lot of red-black tree hanging above process, the process is the leftmost run minimal time course, all operating systems, select the next a process that will select the left-most red-black tree process.
 
static void
check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
{
unsigned long ideal_runtime, delta_exec;
struct sched_entity *se;
s64 participate;
 
 
ideal_runtime = sched_slice(cfs_rq, curr);
delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
if (delta_exec > ideal_runtime) {
resched_curr(rq_of(cfs_rq));
return;
}
......
se = __pick_first_entity(cfs_rq);
delta = curr->vruntime - se->vruntime;
if (delta < 0)
return;
if (delta > ideal_runtime)
resched_curr(rq_of(cfs_rq));
}
 
The above code is the Linux source code
 
There is a concept of time in the operating system, that is, the actual time in a scheduling period, this process should run (ideal_runtime)
The actual time sum_exec_runtime refers to the process of the implementation of total, prev_sum_exec_runtime refers to the actual time of the last time the process is scheduled already occupied.
 
Every time when scheduling a new process will put its se-> prev_sum_exec_runtime = se-> sum_exec_runtime, so sum_exec_runtime-prev_sum_exec_runtime
The schedule is occupied by the actual time. If this time is greater than ideal_runtime, it should be seized.
 
In addition to this condition, but also remove the red-black tree in the smallest process through _pick_first_entity. If vruntime the current process (virtual runtime) is greater than the red-black tree vruntime smallest process, and the difference is greater than ideal_time, it should
He is seized
 
When they find this process should be seized, not directly to him Tixia Qu, but playing a label TIF_NEED_RESCHED in this process, this process can be marked to seize
 
There is also a preemption scenario may happen is that when a process to be awakened dormant
This time if this is the wake-up process a higher priority than the currently running processes, you should also be seized, is also playing a label TIF_NEED_RESCHED on the currently running process
 
Seize the opportunity
 
1. The user state to seize the opportunity
When a system call returns the process from user mode to kernel mode, when the process determines if there TIF_NEED_RESCHED tag, the preemption.
 
2. seize the opportunity kernel mode
Performed in kernel mode, the timing preempted generally occurs preempt_enable () in.
preempt_disable () Close preemption
In the implementation of kernel mode, some operations can not be interrupted, before carrying out all these operations, always first call preempt_disable () closed seize, again, when opened, is calling preempt_enable () when
It is an opportunity to seize the kernel mode code.
 
In the case of kernel mode will encounter interrupted when the interrupt returns, the return is still the core attitude. This is also a time to seize the opportunity to perform.
 
We can look at this picture to understand

Guess you like

Origin www.cnblogs.com/Leo_wl/p/10993731.html