Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the process priority of Linux PR and NI

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand the process priorities of Linux PR and NI", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the process priorities of Linux PR and NI.

In JDK, threads have priority, 0-10 10 means the highest priority, while in Linux, the lowest value means the highest priority, and the one with high priority takes up more time in CPU.

There are PR and NI in Linux. You can see it in the result of top command. For example, figure 1 below is the result of top, and you can see the column values of NI and PR:

Figure 1

Let's first figure out the difference between PR (priority) and NI (nice) values.

Nice value

This is a value that reflects the priority status of the process, ranging from-20 to 19, that is, a total of 40 values, with the lowest value and the highest priority, and the default value is 0.

Although the nice value is not a priority value, it can affect the process priority.

The process with lower nice value has stronger ability to preempt CPU, while the process with higher nice value has less ability to preempt CPU.

On the original O1 scheduled Linux, the nice value is also called static priority. Once set, it will not change unless the value is modified manually by renice. On the O1 scheduled Linux, the value will change, so it is also called dynamic priority.

On linux, renice 10 pid, so that the priority of that process in pid can only be set to a higher value when setting the value to 10, for example, the previous value is 5, and the subsequent value is set to 10 is OK, but setting to-10 is not allowed, and an error without permission will be reported unless you use root users.

Real-time operating system

The real-time operating system needs to ensure that the relevant real-time processes respond in a short time, without long delay, and require minimum interrupt delay and process switching delay. For such requirements, the general process scheduling algorithms, whether O1 or CFS, are unable to meet, so when the kernel is designed, the real-time process is mapped to 100priorities, these priorities are higher than the normal process priority (nice value), and the real-time process scheduling algorithm is also different, they use a simpler scheduling algorithm to reduce scheduling overhead.

Since real-time processes, whether O1 or CFS scheduling, are not satisfied, they are assigned a value priority of 0-100 and a simple scheduling algorithm is used to reduce overhead.

The distinction between real-time processes and non-real-time processes is distinguished by priority. 0-99 are real-time processes, while 100-139 are non-real-time processes. NICE's-20'19 corresponds to 100 'rt',. If you see' rt','in the PR column, it means that the task / process is a realtime process, that is, a real-time process, and sometimes you will see that the value of PR is not a numerical value, but rt.

As shown in the following List-1, in the policy options part, the system gives five scheduling policies to the process. These five scheduling policies are for the two processes, real-time processes: SCHED_FIFO, SCHED_RR, and for non-real-time processes: SCHED_OTHER, SCHED_OTHER, SCHED_IDLE.

List-1

Mjduan@dmj:/mnt/sdb1/doc/minedoc$ chrt-- helpShow or change the real-time scheduling attributes of a process.Set policy: chrt [options] [...] Chrt [options]-- pid Get policy: chrt [options]-p Policy options:-b,-- batch set policy to SCHED_BATCH-d,-- deadline set policy to SCHED_DEADLINE-f,-- fifo set policy to SCHED_FIFO-I,-- idle set policy to SCHED_IDLE-o,-- other set policy to SCHED_OTHER-r -- rr set policy to SCHED_RR (default) Scheduling options:-R,-- reset-on-fork set SCHED_RESET_ON_FORK for FIFO or RR-T,-- sched-runtime runtime parameter for DEADLINE-P,-- sched-period period parameter for DEADLINE-D,-- sched-deadline deadline parameter for DEADLINEOther options:-- a,-- all-tasks operate on all the tasks (threads) for a given pid-m -- max show min and max valid priorities-p,-- pid operate on existing given pid-v,-- verbose display status information-h,-- help display this help-V,-- version display versionFor more details see chrt (1).

The overall scheduling strategy of the system:

If there is a real-time process to be executed in the system, then execute the real-time process

The execution of non-real-time processes will not be scheduled until the real-time process exits or actively gives up the CPU.

Sched_fifo: is one of the scheduling strategies for real-time processes. Using the FIFO strategy, in the case of the same priority, who enters the queue first is scheduled first.

Sched_rr: is one of the real-time process scheduling strategies, using time slicing strategy. The default is 100ms. This strategy is simple and suitable for real-time process with small latency.

What is more troublesome on Linux is the non-real-time scheduling strategy. Many processes on linux are non-real-time processes, and their main scheduling algorithms are O1 and CFS scheduling strategies.

O1 scheduling strategy

It was introduced in kernel version 2.6 and was replaced by CFS by 2.6.63.

Named O1 because the time complexity of its algorithm is O1, using the idea of time slicing, the time of cpu is divided into small segments, and each process takes up a period of time slicing. For multi-core, such time slicing can be done for each cpu. How do you deal with priority: time slicing with high priority takes up more and vice versa.

CFS completely fair scheduling

O1 has poor support for multi-core and multi-CPU, and poor performance. In addition, the linux kernel needs to add the function of cgroup, so it is replaced with CFS,Linux enabling CFS after 2.6.23 as a scheduling method for general priority (SCHED_OTHER) processes.

CFS scheduling strategy will collect virtual time, and then build a red-black tree red-block tree. The virtual time is the key of the red-black tree, so the shorter the virtual time will be on the far left of the red-black tree, and the time complexity of the query is Log (N).

Basically, a virtual time is generated according to the priority and execution time of the current process, which is true for each process. Finally, a red-black tree is built. The process on the left in the red-black tree indicates that the virtual time value is small, indicating that the virtual time value is small most of the time, while the process on the right indicates that the virtual time is large, when a process with small virtual time is occupying cpu. If a process with large virtual time becomes runnable at this time, then the process with large virtual time will preempt cpu, that is, the system always schedules processes with relatively small virtual time.

Figure 2

In the case of multi-core cpu, how does CFS optimize performance? each cpu maintains a scheduling queue to avoid contending locking problems due to the use of global queues. However, after one queue per cpu, load imbalance may occur, so the system needs to balance each cpu queue regularly.

In the following List-2, use the chrt command to check that the priority of the process is 0, and the scheduling algorithm is CFS

List-2

Mjduan@dmj:/mnt/sdb1/doc/minedoc$ jps26960 Main12642 Jps27090 Main890 RemoteJdbcServer31980 Launchermjduan@dmj:/mnt/sdb1/doc/minedoc$ chrt-p 27090pid 27090s current scheduling policy: SCHED_OTHERpid 27090s current scheduling priority: 0mjduanchangdmjVue MNT bank sdb1qdocdock $chrt-p 890pid 890s current scheduling policy: SCHED_OTHERpid 890s current scheduling priority: 0 add

If the number of processes to be scheduled is n, the average CPU time taken by each process is sched_latency_ns/n. Obviously, the actual CPU time consumed by each process decreases as n increases. But it is impossible to make it smaller infinitely in implementation, so the value of sched_min_granularity_ns also limits the minimum execution time period that each process can get.

List-3

Mjduan@dmj:/mnt/sdb1/doc/minedoc$ cat / proc/sys/kernel/sched_latency_ns 300000mjduanchangdmjvl.mntUnix Db1 cat / proc/sys/kernel/sched_min_granularity_ns 300000 Note:

For normal processes, PR=nice+20, so by default, the created process nice value is 0, while the PR value is 20, for example, the default PR for java applications is 20. 0.

For rt processes, that is, real-time processes, from the perspective of PR=-1- users, sometimes when you see a process with a PR value of-51, then its actual priority is 50, and sometimes when you see that the value of PR is rt rather than a numeric value, the corresponding priority is 99.

At this point, I believe you have a deeper understanding of "how to understand the process priorities of Linux PR and NI". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report