When the Linux kernel is looking for a new process to run on the CPU, you must only consider processes in the runable state (that is, processes in the task_running State ), because scanning the entire process chain table is quite inefficient, a two-way circular chain table is introduced, also called runqueue ).
The running queue contains all processes that can be run in the system. It is a bidirectional cyclic queue.
The queue is maintained by the run_list linked list of two pointers in the task_struct structure. The queue has two identifiers: An "Empty Process" idle_task and a queue length.
Two special processes are always in the running queue: the current process and the empty process. We have discussed earlier that the current process is the process directed by the cureent pointer, that is, the progress currently running, the current pointer has no significance in the scheduling process (when the scheduling program is executed). Why? Before scheduling, the current process is running. When a scheduling time occurs that triggers process scheduling, the status of the previously running process is unknown, and in most cases it is waiting, so now is meaningless, http://Ubuntuone.cn/until the scheduler selects a process to run, current really points to the current running process; empty process is a special process, only when no process in the system is runable can it be executed. In Linux, it is regarded as the header of the running queue. When the scheduler traverses the running queue, it starts from idle_task and ends with idle_task, during the scheduling process, you can add new processes to the queue and insert the new processes to the end of the queue, the advantage is that it does not affect the queue members to be traversed by the scheduler. It can be seen that idle_task is an important symbol for running the queue.
Another important sign is the length of the queue, that is, the number of processes in the system that are in the running state (task_running), which is expressed by the global integer variable nr_running. It is defined in/kernel/fork. C as follows:
Int nr_running = 1;
If nr_running is 0, it indicates that there are only empty processes in the queue. NOTE: If nr_running is 0, the current and empty processes in the system are the same process. However, Linux will make full use of the CPU to avoid this situation.