This week to prepare for the seminar of the OS Course to study the UNIX process scheduling, from the information on the web and the paper I looked at a lot of System V, so I take system V as an example.
One thing to note is that the first version of System V was released in 1983, and the most successful version of Sytem v Release 4 (1988) is a bit distant from the present, and the *nix of various branches evolved to the present and 80 generations should have changed a lot, But their design ideas are worth studying. UNIX Startup process
When the bootstrap bootstrap is completed, the system control is transferred to the UNIX kernel, the UNIX OS officially starts the work kernel creates process No. 0 process No. 0, creates the 1th process init, and gives control to the INIT init query for the number of terminals currently in existence, creating a new management process for each terminal After the management process waits for the user to log in, the system initiates a shell process user input command for each user, creating a new process by the shell process the UNIX process context Structure proc: resident Memory, which records the basic information of the process. User structure: Does not reside in memory, in the process executes Si cho into the memory, records the memory the private information and the data. Body Segment: Program code and constants, shared areas that can be accessed by multiple processes. Data segment: Private information about the process, the area accessed by the user-state process. System stack: The information reserved area used by the process to complete subroutine nesting and interrupt processing when the system state executes, can only be used by the system State process. User stack: The information retention area that a process uses to complete subroutine nesting and interrupt processing when user state executes. No. 0 Process Three major tasks are created by the core program, in the initialization of the completion of the 1th process of the establishment in the future management, responsible for the process of scheduling and distribution in the future management, responsible for the process of internal and external storage and Exchange UNIX process state transition diagram
user-State and system-State switching
PSW: Processor State Register: reflects how the current execution process is being accessed, such as the state in which the process is executed, the priority state of the interrupt, how the processor is executed before entering an interrupt, or falling into an instruction, and so on. Process Scheduler (switch program for the NO. 0 process) analyze and adjudicate processes that participate in the competitive CPU and already have execution conditions for the selected process the transfer of various States in the running of the management process completes the Exchange scheduling algorithm between the internal and external memory of the process in the system
Dynamic priority multilevel Feedback cyclic scheduling (Round Robin with multilevel Feedback) when a time slice ends, the system calculates the priority calculation for all processes and then looks at the processes that have priority higher than the current process and is in "Memory Ready," and selects them Set the scheduled flag for the selected process when the next round of dispatch starts, the dispatch has set a high priority dispatch flag with the highest priority process, allowing it to start running in the processor priority Count
UNIX is based on the priority number to determine the process scheduling priority.
The priority number is the Char p_pri field in the proc structure of the process, with a value range of 0-127, with a lower priority number and higher precedence.
The priority number between 0-49 is the priority of the system State process, and the process priority in the user state is between 50-127. Calculation Formula
P_pri = p_cpu/2 + puser + p_nice + Nzero Puser and Nzero are the thresholds for basic user priorities, which are system presets, respectively, 25 and P_CPU indicate that the process last consumed CPU time, and for the current process, each clock was interrupted (note is each time slice) This value is added 1 (maximum value 80). If the clock interruption period is 16.6ms, then the right 60 clock interrupts in one second the p_cpu of the newly created process is 0 p_nice is a priority offset value that the user can set through the system call, and the default is 20. A superuser can set it between 0 and 39, and the average user can only increase the value
After a time slice is finished, the system divides the p_cpu of each process by 2, which is called attenuation.
After attenuation, the system recalculates the P_PRI of each process
Analysis of this formula, the System runtime Puser and Nzero is constant, and p_nice is generally a constant, so the process priority depends on the P_CPU. For those processes that take up a lot of CPU time, its priority is reduced after it is larger than those that have recently taken up a shorter CPU, so its priority will be smaller, and in turn, those processes that do not consume CPU will have higher priority, so they will be executed in the next dispatch. This is a kind of negative feedback regulation.
The priority number 0-49 is the kernel-reserved priority number, which is assigned a system priority (0-49) when the process sleeps (blocking), so that these processes wake up (such as IO completion) to be processed in a timely manner, because they are smaller than the number of user priorities. question of timing of dispatch
On the timing of scheduling, I am more puzzled, I have a Chinese textbook and a courseware on the internet said that the scheduling time has the following two: The first case: when the process of automatic abandonment of the processor (such as waiting for I/O, or run the end), a process scheduling In the second case, when the process is transferred from the system state to the user state, the system schedules the schedule once, with the intention of having the High-priority dispatch flag set and the process ready in memory to have the opportunity to preempt the execution state.
However, there is no mention of the "timing of dispatch" in the foreign language data found.
If the above two scheduling time, so it may not be scheduled at the end of the time, because only the priority calculation, then assume that a user-state process in the implementation of a large number of calculations (or dead loops), neither actively discard the processor, nor from the system state to the user state of the conversion, Then the processor has been occupied by it.
Process No. 0 is always a system state, if the No. 0 process after the end of the time slice, calculate the priority number and set the tag, so that the previous user process to continue to execute, whether it is a "system-state to user-state conversion." If it is, then there is the timing of scheduling, the timing of this arrangement is effective.
Although there is this doubt, but the main idea of the scheduling I have understood, perhaps later in the study of these questions will be solved.
Reference http://vega.cs.kent.edu/~mikhail/classes/os.s99/l8advancedscheduling. PDF http://ettc.sysu.edu.cn/2005wlkc/caozuoxitong/book/chapter5/lesson3/lesson3.1.htm Zhang Hongguang, Li Fu Cai "Unix Operating system Tutorial", Machinery Industry Press
Original link: http://blog.dyzeng.org/2016/03/17/Unix-scheduling/