1.Linux is how the process is organized.
* Process Concept: Process is a computer program on a set of data on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure. In the early process design-oriented computer architecture, the process is the basic execution entity of the program, and in the contemporary thread-oriented computer architecture, the process is the container of the thread. A program is a description of the instruction, the data and its organization, and the process is the entity of the program
* Below is a diagram that shows a program from writing to the final running process:
* process is a dynamic process, and since the process is generated, the process will disappear, that is, death. Each process is produced by its parent process, and if the child process ends, the parent process reclaims its resources. If the parent process of the child process is terminated first, the child process enters the orphan state, which is reclaimed by the system's 1th process.
The shell command Ps-la can view the current system's processes, for example:
2. How the process state is converted.
* The Linux process states are:
Task_running: Ready state or run state, process ready to run, but not necessarily in possession of CPU, corresponding to the process state of R
Task_interruptible: Sleep state, but the process is in a light sleep, can respond to signals, usually the process of active sleep into the state, corresponding to the process state s
Task_uninterruptible: Sleep state, deep sleep, no response signal, typical scenario is the process gets the semaphore block, corresponding to the process status D
Task_zombie: Zombie state, the process has exited or ended, but the parent process is not yet known, there is no recycle state, corresponding to the process state Z
task_stoped: Stop, Debug state, corresponding process state t
* process Scheduling time:
Process scheduling can cause process state transitions, which can be triggered by the following conditions, when the process terminates or the process sleeps and the active exit or sleep releases the CPU;
The process of light sleep is awakened by the CFS scheduler, the deep sleep process is awakened by the release of semaphores, locks, etc., and the process receives semaphores, etc. there is also one of the most common interrupts, exceptions.
3. How the process is scheduled.
* meaning of process scheduling
To implement multi-processes, process scheduling is essential. Process scheduling is the process of task_running state scheduling, the need to schedule the process must be operational state. The scheduler is responsible for deciding which process to run, when to run, and how long it will run. The process Scheduler is the kernel subsystem that allocates a limited amount of processor time resources between the running state processes.
* management of process scheduling
The operating system requires a snap-in that is responsible for scheduling processes, which the management unit decides who should use the CPU at the next moment, which is the process scheduler that acts as a snap-in. The task of the process scheduler is to reasonably allocate CPU time to the running process. The following describes the classification of the process, to better describe how the process scheduling is a pattern.
I/o-bound: The majority of the situation is that the CPU is waiting for I/O (hard disk/memory) read/write, usually spend a lot of time waiting for I/O operations to complete, when the CPU loading is not high.
Cpu-bound: Computationally intensive, similar to the previous intensive, this type requires a lot of CPU time to operate, for the I/O read/write in a very short time can be completed, most of the situation is CPU Loading 100%, For example, a program that calculates the PI to 1000 digits below the decimal point, in the course of execution, most of the time used in trigonometric and open-root calculations, is a procedure belonging to the Cpu-bound.
Interactive Process : As the name implies, this type needs to interact with the user frequently, it may take a long time to wait for the user to enter the operation, and requires a short time to respond, form the application, shell and so on.
Batch Process : This type does not need to interact with the user, usually runs in the background, does not require immediate response, typical examples are: program compiling, scientific calculation and so on.
real-time process : Response time is short, there is real-time demand, will not be low-priority interruption, such as video or audio playback, mechanical control.
*
The process provides two priorities, one is the normal process priority, and the second is the real-time priority. The former is suitable for sched_normal scheduling strategy, the latter can choose Sched_fifo or SCHED_RR scheduling strategy. At any time, real-time processes are prioritized higher than normal processes , and real-time processes are only preempted by higher-level real-time processes, which are scheduled in terms of FIFO (one-time-chance) or RR (multiple rotation) rules.
Scheduling of real-time processes
Real-time processes, with only static priority, because the kernel no longer adjusts its static priority based on factors such as hibernation, which ranges between 0~max_rt_prio-1. The default Max_rt_prio configuration is 100, that is, the default real-time priority range is 0~99. The nice value, in effect, is the process of prioritizing within the max_rt_prio~max_rt_prio+40 range.
Unlike normal processes, when a system is scheduled, a process with a high real-time priority is always preceded by a low-priority process. Real-time processes that know that real-time priorities are high cannot be executed. Real-time processes are always considered active. If there are several real-time processes with the same priority, the system chooses the process in the order in which the process appears on the queue. Assuming that the current CPU is running real-time process A with a priority of a, and a real-time process B with a priority of B enters the operational state, then as long as b<a, the system will interrupt the execution of a, and priority B should be executed until B is unable to execute (regardless of the real-time process of A/b).
Real-time processes of different scheduling strategies are comparable only at the same priority level:
1. For FIFO processes, it means that only the current process is completed before it is executed by another process. This shows quite overbearing.
2. For the RR process. Once the time slice is consumed, the process is placed at the end of the queue, and other processes of the same priority are run, and the process continues to execute if there are no other processes of the same priority level.
, for real-time processes, high-priority processes are the big uncle. It executes until it is impossible to execute, before it turns to the low-priority process. The hierarchy is quite rigid.
Non-real-time process scheduling
Linux is scheduled for normal processes, based on the dynamic priority. The dynamic priority is adjusted by the static priority (Static_prio). Under Linux, the static priority is invisible to the user and hidden in the kernel. The kernel provides the user with an interface that can affect the static priority, which is the nice value, and the relationship is as follows:
Static_prio=max_rt_prio +nice+ 20
The range of nice values is -20~19, and thus the static priority range is between 100~139. The larger the nice value, the greater the Static_prio, and the lower the final process priority.
Ps-el command execution results: NI column shows the nice value of each process, and the PRI is the priority of the process (if the real-time process is a static priority, if it is a non-real-time process, which is the dynamic priority)
And the process of time slices is completely dependent on Static_prio customization, see, from the "deep understanding of the Linux kernel",
As we have said earlier, when the system is scheduled, other factors will be taken into account, which will result in the calculation of something called the dynamic priority of the process, according to which the scheduling is implemented. Because you should consider not only the static priority, but also the properties of the process. For example, if the process is an interactive process, it can be appropriately prioritized to make the interface more responsive and thus give users a better experience. Linux2.6 has been greatly improved in this respect. Linux2.6 that interactive processes can be judged from an measurement such as average sleep time. The more sleep the process has in the past, the more likely it is to be part of the interactive process. When the system is dispatched, more rewards (bonus) are given to the process so that the process has more opportunities to execute. Awards (bonus) range from 0 to 10.
Process execution is scheduled strictly in the order of the dynamic priority level. A process with a high dynamic priority goes into a non-operational state, or the time slice is consumed before the process executes with a lower dynamic priority. The calculation of dynamic priority takes two factors into account: static priority, and the average sleep time of the process is bonus. The calculation formula is as follows
Dynamic_prio = max (min (Static_prio-bonus + 5, 139))
At the time of dispatch, Linux2.6 used a small trick, which is the idea of the classical space-changing time in the algorithm, so that the optimal process can be completed in the time of O (1).
* Scheduler Class
*Linux process state machine
5 Scheduler classes are implemented according to the different scheduling strategies
Stop_sched_class: Occurs when a task is shut down cpu_stop_cpu_callback a task migration,hotplug_cpu between CPUs, no normal process needs to be scheduled.
dl_sched_class: Scheduling real-time process with EDF's earliest priority algorithm, corresponding scheduling strategy is Sched_deadline.
Rt_sched_class: To provide roound-robin algorithm or FIFO algorithm to dispatch real-time process, the specific scheduling policy is specified by the task_struct->policy of the process, the corresponding scheduling strategy is Sched_fifo , SCHED_RR.
Fair_sched_clas: Using the CFS algorithm to dispatch the ordinary non-real-time process, the corresponding scheduling strategy is Sched_normal, Sched_batch.
Idle_sched_class: Using the CFS algorithm to dispatch the idle process, the first pid=0 thread of each cup: Swapper, is a static thread. Scheduling class belongs to: Idel_sched_class, so in PS is not visible. Generally run in the boot process and CPU abnormal time to do dump, the corresponding scheduling strategy for Sched_idle.
* Linux process state machine
4. View of the operating system process model.
* Effectively manage system resources and improve system resource use efficiency
First job: Analysis based on Linux process model