Home > Operation and Maintenance > Linux Operation and Maintenance > Do processes have priorities in linux?

Do processes have priorities in linux?

青灯夜游
Release: 2023-03-30 13:46:05
Original
1349 people have browsed it

Processes in Linux have priorities. Linux is a multi-user, multi-tasking operating system. There are usually many processes running in the system, but the CPU can only operate one instruction in one clock cycle. Which process should operate first and who should operate last? This needs to be determined by the priority of the process. In the Linux system, there are two parameters that indicate the priority of the process: Pri and Nice; the process priority is the PRI value, not the Nice value, but the Nice value will affect the priority.

Do processes have priorities in linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Linux-Process Priority

Linux is a multi-user, multi-task operating system. There are usually many processes running in the system. process. However, the CPU can only operate one instruction in one clock cycle (the current CPU uses multi-threading and multi-core technology, so it can operate multiple instructions in one clock cycle. However, the number of instructions that can be operated at the same time is far smaller than that in the system. The total number of processes), then the question comes: Who should calculate first and who should calculate last? This needs to be determined by the priority of the process.

  • The order in which the CPU allocates resources is the priority

  • The process with the highest priority has the right to execute first,

  • You can let the process run on the specified CPU to improve the overall performance of the system

In addition, when the CPU is computing data, it does not complete an integrated calculation and then proceed to the next step. The operation of a process first operates on process 1, then on process 2, then on process 3, and then on process 1 until the process task ends. Not only that, due to the existence of process priority, processes are not calculated sequentially, but which process has a higher priority will be calculated more times in one calculation cycle.

It’s hard to understand this way, let’s put it another way. Suppose I now have 4 children (processes) that need to be fed (computed). I prefer child 1 (process 1 has a higher priority), and child 2, child 3, and child 4 are treated equally (process 2, process 3, and process 4 have priority). Now I start feeding. I can't feed child 1 first, and then feed the other children. Instead, I need to feed the food in a loop (all processes loop during CPU operation). Then, when I am feeding (calculation), I will feed the child a mouthful of rice first, and then feed the other children. And in a cycle, feed child 1 two mouthfuls of food first, because I prefer child 1 (high priority), and feed the other children one mouthful of food. This way, child 1 will be fed first (process 1 computes faster) because I like child 1 more.

View process priority

1. Use ps-al to see the priority of the process

Do processes have priorities in linux?

2.PRI and NI

In the Linux system, there are two parameters indicating the priority of the process: Priority and Nice.

[root@localhost ~]# ps -le
F S UID PID PPID C PRI NI ADDR  SZ WCHAN TTY    TIME  CMD
4 S   0   1    0 0  80  0    - 718     -   ? 00:00:01 init
1 S   0   2    0 0  80  0    -   0     -   ? 00:00:00 kthreadd
...省略部分输出...
Copy after login

Among them, PRI stands for Priority and NI stands for Nice. Both values ​​indicate priority. The smaller the value, the higher the priority the process will receive from the CPU. However, the PRI value is dynamically adjusted by the kernel and cannot be modified directly by the user. So we can only affect the PRI value by modifying the NI value and indirectly adjust the process priority.

The relationship between PRI and NI is as follows:

PRI (final value) = PRI (original value) NI

In fact, everyone only needs to remember, We can change the priority of the process by modifying the value of NI. The smaller the NI value, the PRI value of the process will decrease, and the process will be processed by the CPU first; conversely, the larger the NI value, the PRI value of the process will increase, and the process will be processed later by the CPU.

There are several things to note when modifying the NI value:

  • The NI range is -20~19.

  • Ordinary users can adjust the NI value in the range of 0~19, and they can only adjust their own processes.

  • Ordinary users can only increase the NI value, but not lower it. If the original NI value is 0, it can only be adjusted to be greater than 0.

  • Only the root user can set the process NI value to a negative value, and can adjust any user's process.

View the command to view and modify the nice value

1. Use the top command to view and modify the nice value

Enter top-press r, enter the process pid, and enter the nice value. (If it shows that elevation is refused, use sudo to elevate the permissions of the top command).

2. Other concepts

  • Competitiveness: There are many systems, but only a small number of CPUs, or even only one, so there is competition between processes. In order to complete tasks efficiently and compete for related resources more reasonably, priority is given.

  • Independence: When multiple processes run, they need to enjoy various resources exclusively, so they do not interfere with each other during the multi-process process.

  • Parallel: Multiple processes running on multiple CPUs at the same time are called parallel.

  • Concurrency: Multiple processes use process switching on a CPU to advance multiple processes within a period of time, which is called concurrency.

The process priority is the PRI value, not the Nice value, but the Nice value will affect the priority

PRI is still It is easier to understand, that is, the priority of the process, or in layman's terms, the order in which programs are executed by the CPU. The smaller this value, the higher the priority of the process. What about NI? This is the nice value we are talking about, which represents the modified value of the priority at which the process can be executed. As mentioned before, the smaller the PRI value, the faster it will be executed. After adding the nice value, the PRI will become: PRI(new)=PRI(old) nice.

So far, what needs to be emphasized is that the nice value of a process is not the priority of the process. They are not a concept, but the nice value of the process will affect the priority change of the process.

Change the process priority by adjusting the nice value. The nice adjustment range is -20~19.

Influencing factors and principles of process priority and change

Scheduling strategy: RR scheduling and FIFO scheduling processes are real-time processes. Processes scheduled with time-sharing (OTHER) are non-real-time processes.

FIFO (first in, first out) and RR (time slice rotation) are used for real-time processes, and OTHER (time-sharing scheduling) is used for non-real-time processes; real-time processes will preempt ordinary processes; FIFO will cause simultaneous Real-time processes with priority always occupy the CPU, and RR will ensure that real-time processes with the same priority are executed in turn according to time slices.

The priority of real-time processes is higher than that of ordinary processes. Real-time processes use static priority scheduling, and non-real-time processes use dynamic priority scheduling. The dynamic priority of non-real-time processes is adjusted by nice and may be affected by bonus.

Linux processes have two priorities: normal process priority (using SCHED_NORMAL scheduling policy), and real-time process priority (using SCHED_FIFO or SCHED_RR scheduling policy)

Different scheduling strategies Real-time processes are comparable only when they have the same priority. At any time, the priority of a real-time process is higher than that of an ordinary process.

Linux uses static priority scheduling for real-time processes, and for ordinary processes (non-real-time Process), scheduled according to dynamic priority.

The real-time process only has a static priority (between 0~MAX_RT_PRIO-1, the default MAX_RT_PRIO is 100), and the kernel will no longer adjust its static priority based on factors such as sleep;

Each priority level of the real-time process No. 0-99 corresponds to a priority queue (linked list). The corresponding linked list with a higher value is executed first (number 0 is the lowest). The priority can be modified by the chrt and corresponding functions described later;

The static priority of non-real-time processes can be adjusted through the nice value (-20~19): static_prio=MAX_RT_PRIO nice 20.

nice only affects non-real-time processes (static priority is between 100 and 139). The larger the nice value, the larger the static priority value and the lower the priority.

Non-real-time process dynamic priority is calculated based on static priority and bonus: dynamic_prio = max (100, min (static_prio - bonus 5, 139))

bonus reflects the average sleep time of the process (range 0~10), the more sleep time, the more likely it is an interactive process, and the more likely it will not use up the time slice before releasing the CPU every time it is its turn to run.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of Do processes have priorities in linux?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template