In-Depth Analysis of PR and NI in Linux Top Command

This article is an original work by Teacher Liu from Yunbei Education. Please respect intellectual property rights. When forwarding, please indicate the source. No plagiarism, adaptation, or unauthorized reproduction is allowed.

In the Linux system, the top command is an important tool for monitoring system resources and process status. In the interactive interface of top, PR (Priority) and NI (Nice) are two key parameters related to process scheduling priority. Many users are confused about their meanings and relationships. This article will explain these two parameters in detail and explore how they affect the system’s process scheduling.

1. PR (Priority): Dynamic Priority of Processes

PR represents the process’s real-time priority, which is an important basis for the kernel scheduler when allocating CPU time slices. The range of values and their meanings are as follows:

PR Value Range

Meaning

0 to 39

Normal processes (non-real-time processes), where a lower value indicates a higher priority (determined together with NI).

RT or negative values (e.g. -2)

Real-time processes, with a priority higher than all normal processes.

Normal Processes:By default, most processes belong to this type. The PR value is dynamically calculated from the NI (Nice value), usually being 20 + NI. For example, if NI=0, then PR=20; if NI=-5, then PR=15.

Real-Time Processes:Processes running under SCHED_FIFO or SCHED_RR scheduling policies (such as certain high real-time tasks) show their PR as RT or negative numbers. Real-time processes have a priority higher than all normal processes.

2. NI (Nice): User-Adjustable Priority Offset

NI is the Nice Value of user-mode processes, allowing users to indirectly adjust the priority of processes. The range of values is -20 to 19, with a default value of 0. The key rules are as follows:

The lower the NI, the higher the process priority:Setting NI to -20 allows the process to obtain CPU time more frequently.

The higher the NI, the lower the process priority:Setting it to 19 will make the process more “humble,” reducing its CPU usage.

3. The Relationship Between PR and NI
Normal Processes:PR = 20 + NI For example:
NI=0 → PR=20
NI=-5 → PR=15
NI=10 → PR=30
Real-Time Processes:NI is ineffective

The priority of real-time processes is directly set by the chrt command (range 1~99, with 1 being the lowest and 99 being the highest), at which point PR is displayed as RT or negative (e.g., -2 corresponds to real-time priority 98).

4. How to View and Modify NI and PR
View Process Priority:
top -p [PID]         # View the PR and NI of a specific process in top
Example as follows
# top -p 1895                                                top - 11:27:18 up  1:55,  2 users,  load average: 0.00, 0.00, 0.00Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 stMiB Mem :   1960.7 total,   1230.0 free,    326.5 used,    404.2 buff/cacheMiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   1556.6 avail Mem     PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND       1895 root      20   0  225616   4604   3852 S   0.0   0.2   0:00.43 top
Modify NI (Normal Processes):
# ps -eo pid,ni,pri,cmd | grep 1352 | grep -v grep     1352  0  19 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups   # renice 10 13521352 (process ID) old priority 0, new priority 10    # Modify the running process's NI=10# ps -eo pid,ni,pri,cmd | grep 1352 | grep -v grep   1352  10   9 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

Note:Normal users can only lower their priority (NI ≥ 0), and need sudo privileges to raise their priority (NI < 0).

Set Real-Time Process Priority:

Here, -f indicatesfixed priority scheduling policy, and 99 is the highest priority level.Note that this operation usually requires superuser privileges and should be used with caution to avoid affecting the overall stability of the system.

5. Common Questions and Misconceptions
Why is there no change in PR after modifying NI?
The process may be a real-time process (PR displayed as RT), in which case NI is ineffective.
The priority difference may not be obvious when the system load is low.
Why does PR exceed 39?
The theoretical maximum PR value for normal processes is 39 (NI=19). If you see larger values, it may be due to kernel version differences or different display methods.
Do real-time processes “starve” other processes?
Yes!Real-time processes, if they do not actively release the CPU (such as executing sleep in a loop), may prevent normal processes from running.Use with caution.
6. Summary

PR (Priority):Dynamic priority that determines the kernel scheduling order.

NI (Nice):User-adjustable offset that only affects normal processes.

Real-Time Processes:Set by chrt, with a priority higher than normal processes, but should be used with caution.

Understanding these two parameters can help you manage system resources more efficiently and optimize the performance of critical tasks.If you need to temporarily raise the priority of a certain process, first adjust NI;if you need extreme real-time performance, then consider real-time process strategies.

To learn more related study materials (technical articles and videos), you can search for “Yunbei Education” on WeChat public account or Bilibili to obtain them for free.

Leave a Comment