The kernel on the micro, the CPU running time into a number of points, and then arranged to each process rotation, resulting in the macro of all processes as if at the same time executing. Dual-core CPU, in fact, up to only two processes at the same time, everyone in the top, Vmstat command to see the running process, not really in possession of the CPU ha.
Therefore, some well-designed high-performance processes, such as nginx, are actually a few CPUs, with several working processes, the reason is here. For example, your server has 8 CPUs, then the Nginx worker should be only 8, when you more than 8, the kernel may put more than one nginx worker process to 1 runqueue, what happens? Is on this CPU, will be more evenly distributed time to these nginx workers, each worker process after running a time slice, the kernel needs to do a process switch, the running process context is saved. Assuming that the kernel allocated time slice is 100ms, do process switching time is 5ms, then the process performance is still very obvious, and you configure the worker, the more old-aged women and girls decline more severe.
Of course, this is related to the design of Nginx. Nginx is an event-driven all-asynchronous process, the design of its own almost no blocking and interruption, nginx designers hope that each nginx worker can monopolize the CPU almost all the time slice, this is the number of Nginx worker configuration basis.
Of course, the actual running process, most of the time is not nginx this want to monopolize the whole CPU of the process, many processes, such as VI, it is in a lot of time is waiting for user input, then VI waiting for the IO interrupt, is not occupy the time slice, the kernel faces a variety of processes, It takes a skill to allocate CPU time slices.
The kernel allocates the time slice to have the strategy and the tendency. In other words, the kernel is eccentric, it likes the IO consumption process, because this kind of process if not timely response, users will be very uncomfortable, so it always subconsciously allocate more CPU time to this kind of process. CPU consumption process kernel is not very concerned about. Does that make sense? Too, CPU consumption slow a bit of user perception, electrical signals and biological signals operating speed gap is huge. Although the kernel allocates as many time slices as possible to the IO-consuming process, the IO consumption process often sleeps, giving it no time slices at all. Is that reasonable?
So how does the kernel implement this eccentricity? This is achieved by dynamically adjusting the priority of the process and assigning different lengths of CPU time. First say how the kernel determines the length of the time slice.
For each process, there is an integral type Static_prio that represents the static priority set by the user, which corresponds to the nice value in the kernel. Look at the Static_prio members in the process description structure.
What is the nice value? In fact, the priority is another representation of the user process, and the value range for Nice is 20 to +19,-20 with the highest priority and +19 minimum. As I said in the previous article, there are 140 kernel priorities, and how does the nice priority that a user can set correspond to these 140 priorities? Look at the code:
As you can see, Max_prio is 140, which is the kernel-defined maximum priority.
And Max_user_prio is 40, meaning that the normal process specifies a priority level of up to 40, as we said earlier-20 to +19.
The nice value is-20 is the highest, which corresponds to how much static_prio? Nice_to_prio (0) is 120,nice_to_prio (-20) is 100.
When the process has just been forked out by its parent process, the remaining time slices of its parent process are split evenly. After the time slice is executed, the time slice is redistributed according to its initial priority, the lowest priority is +19, the minimum time slice 5ms is assigned, the priority is 0 o'clock 100ms, and the priority is 20 when the maximum time slice is 800ms. Let's see how the kernel calculates the length of the time slices, so let's look at the task_timeslice time slice calculation function:
Linux kernel scheduling algorithm (2) How to allocate--cpu time slices