Does process have priority in linux

06-06-2023

Today, the editor will share with you some relevant knowledge points about whether processes have priority in Linux. , I hope you have gained something after reading this article, let's take a look at it together.

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

Linux-process priority

Linux is a multi-user, multi-tasking operating system, usually running a lot of process. But the CPU can only operate one instruction in one clock cycle (the current CPU adopts multi-threading and multi-core technology, so it can operate multiple instructions in one clock cycle. But the number of instructions operated at the same time is far less than the The total number of processes), then the question comes: who should be calculated first, and who should be calculated later? 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 high priority has the right to execute first,

  • You can let the process run on the specified cpu, improve the overall performance of the system

In addition, when the CPU is computing data, it is not the After one integrated calculation is completed, the next process is calculated. Instead, process 1 is calculated first, then process 2 is calculated, then process 3 is calculated, and then process 1 is calculated 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, let's put it another way. Suppose I now have 4 children (processes) that need to be fed (calculated), I prefer child 1 (process 1 has higher priority), child 2, child 3 and child 4 are treated equally (process 2, process 3 and process 4 same priority). Now I start feeding, I can't feed child 1 first, and then feed other children, but need to feed in a loop (all processes cycle during CPU operation). Then, when I feed (calculate), I will feed the child 1 bite of food first, and thenThen feed the other children. And in a cycle, feed child 1 two bites first, because I prefer child 1 (higher priority), and feed the other children one bite. This way, child 1 gets full first (process 1 runs faster), because I prefer child 1.

View process priority

1. Using ps-al, you can see the priority of the process

< p>

2.PRI and NI

In the Linux system, there are two parameters that indicate the process priority: 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 ...omit some output...

Where PRI stands for Priority and NI stands for Nice. These two values ​​both indicate the priority, and the smaller the value, the higher the priority of the process being processed by 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

Actually, You just need to remember that we can change the priority of the process by modifying the value of NI. The smaller the NI value, the lower the PRI of the process, and the more preferentially the process is processed by the CPU; on the contrary, the larger the NI value, the higher the PRI value of the process, and the later the process is processed by the CPU.

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

  • The range of NI is -20~19.

  • Ordinary users can adjust the NI value from 0 to 19, and they can only adjust their own processes.

  • Ordinary users can only increase the NI value, not decrease 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. (Use sudo to elevate the privileges of the top command if it shows denial of elevation).

2. Other concepts

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

  • Independence: Multi-process operation requires exclusive use of various resources, so it does not interfere with each other during multi-process.

  • Parallel: Multiple processes run in multiple cpus and run at the same time, which is called parallelism.

  • Concurrency: Multiple processes use process switching on one 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 relatively It is easy 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 the value, the higher the priority of the process. What about NI? It is the nice value we are talking about, which represents the modified value of the priority of the process that can be executed. As mentioned earlier, 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 the process is not the priority of the process. They are not a concept, but the nice value of the process will affect the change of the priority of the process.

By adjusting the nice value, change the process priority, the nice adjustment range is -20~19.

Factors and principles of process priority and change

Scheduling strategy: RR scheduling and FIFO scheduling are real-time processes, based on time-sharing The process scheduled ( OTHER ) is a non-real-time process.

FIFO (first in, first out) and RR (time slice rotation) are used for real-time processes, OTHER (time-sharing scheduling) is used for non-real-time processes; real-time processes will preempt ordinary processes; FIFOIt will cause real-time processes of the same priority to always occupy the CPU, and RR will ensure that real-time processes of 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 the SCHED_NORMAL scheduling strategy), and real-time process priority (using the SCHED_FIFO or SCHED_RR scheduling strategy)

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

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

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

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

Static priority of non-real-time process can be adjusted by nice value (-20~19): static_prio=MAX_RT_PRIO + nice + 20.

nice only affects non-real-time processes (the static priority is between 100 and 139), the greater the nice, the greater 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 of the process Time (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 and release the cpu every time it is its turn to run.

The above is all the content of this article on whether processes have priority in linux, thank you for reading! I believe that everyone has gained a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the Yisu cloud industry information channel.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us