Although the Linux process scheduler is particularly important, but it is merely a space exists in the kernel function of it, is not a mystery. Linux scheduler is called Schedule () function, the high frequency of this function is called by it to decide whether to switch to the process, and if you want to switch, then switch to which process and so on. Let's look at what you want to execute the scheduler, we call this situation a scheduling opportunity.
Linux scheduling opportunity are:
1, the process state transition moment: the process terminates, the process to sleep;
2,current time slice is run out (current-> count = 0) (current-> counter = 0);
3, the device driver
4, the process from interrupts, exceptions and system calls to return to the state when a user;
Timing 1, the process to be called sleep () or exit () functions such as state transition, these functions will take the initiative to call the scheduler process scheduling procedures;
Timing 2, since the process time slice is updated by the timer interrupt, and therefore, the timing of this case and 4 are the same.
Timing 3, when the device driver to perform long and repetitive tasks, direct calls to the scheduler. In each iteration cycle, the driver checks the value of all need_resched, if necessary, call the scheduler schedule () initiative to give up CPU.
Timing 4, as mentioned above, whether it is from the interrupt, exception or system call returns, eventually calling ret_from_sys_call (), this is detected by the flag scheduling function, if necessary, call the call the dispatcher. Why, then, the system call returns from the dispatcher to call it? This of course is from the viewpoint of efficiency. System call returns from kernel mode means that you want to leave and return to the user mode, and converting the state to take some time, therefore, before returning to user mode, the system is all done things in the kernel mode processing.
For the timing of the implementation of direct scheduling procedures, we do not discuss, because later we will describe the work process scheduler. Earlier we discussed the clock interrupt, know the important role of the clock interrupt, http: //Ubuntuone.cn/ Here we take a brief look at each clock interrupt occurs, the core work to be done, the first time to have the most frequent scheduling a general understanding, and then discuss in detail the specific work process scheduler.
Each clock interrupt (timer interrupt) occurs by three functions work together to complete the selection and switching processes, which are: schedule (), do_timer () and ret_form_sys_call (). Let's explain these three functions:
schedule (): process scheduling function, which does the selection process (scheduling);
do_timer (): let's call it a clock function, which is called the clock interrupt service routine, a major component of the clock interrupt service routine, the frequency of the function is called is the clock interrupt frequency that is 100 times per second ( referred to 100 Hz or 100Hz);
ret_from_sys_call (): Returns the system call function. When a system call or interrupt is completed, the function is called for to deal with some finishing touches, such as signal processing, and so on core tasks.
These three functions is how to coordinate the work?
We saw earlier, the clock interrupt is an interrupt service routine, which is a major component of the clock function do_timer (), by the completion of this function to update the system time, the update process time slice, etc., the process of the updated time slice counter as mainly based on the schedule.
Interrupt the clock returns to the calling function ret_from_sys_call (), We have already discussed this function, there is in this function the following lines:
cmpl $ 0, _need_resched
call SYMBOL_NAME (schedule)
These lines meaning is clear: detect need_resched flag. If this flag is non-zero, then call the scheduler to reschedule at the schedule () selection process. Scheduler schedule () will select the next process should run in the run queue according to specific criteria. When returning from the scheduler, if found to have scheduling flag is set, then they call the scheduler until dispatch flag is 0, then, when you return from the scheduler selected by the RESTORE_ALL recovery process environment, to return to the selected given process user space, so that get to run.
These are the most frequent clock interrupt scheduling opportunity. The main purpose of this discussion is to give the reader the opportunity to have a general understanding 4.
Finally, note that the system function call returns ret_from_sys_call () system call is from, exception and interrupt function usually returns to call functions, but does not have to call, for those who want to frequently interrupt response and request to be treated as soon as possible signal, http: //www.britepic.org/ to reduce system overhead, does not call ret_from_sys_call after processing () (because obviously, the return from the interrupt handler to user space must be interrupted that process, without having to re-select), and they are made to work as little as possible, because the frequency response is too high.
Linux and other UNIX scheduler scheduler different, especially in the "nice level" priority treatment and priority scheduling (priority high process to run first) is different from Linux using a round-robin scheduling (Round Robing ), but at the same time ensuring high-priority processes run both fast, time and length (both sooner and longer). The standard UNIX scheduler are used in a multi-stage process queue. Most implementations are used in the two priority queues: a standard queue and a real ( "real time") queue. Under normal circumstances, if the real time queue process is not blocked, they must be executed before the standard queue process, and each queue, "nice level" high process to be executed.
Overall, Linux scheduling sequence in the interactive process in terms of performance is very good, of course, this is at the expense of part of the "throughput" for the price.