Linux 命令:如何操纵进程优先级
在 Linux 中管理进程是每个优秀的系统管理员都应该熟悉的基本概念。您对这些进程所做的大部分工作都是基本的重复性任务,例如启动和停止进程、搜索进程、终止进程等。
在一些不太常见的情况下,您可能需要根据系统资源分配重新排序您的进程。当这些情况出现时,我们中的许多人会使用我们最喜欢的搜索引擎来找出最有效的方法。现在搜索算法已经将您带到了启用 Sysadmin,让我们为您提供您正在寻找的答案。
我将详细说明如何确定优先级,然后向您展示如何操作下面的这些值。
进程优先级和相关性
Linux 系统运行的进程通常比计算机中的处理单元数量要多。因此,进程调度程序会在单个核心上的进程之间快速切换,从而造成我们正在同时运行多个进程的错觉。
实际上,每个进程都分配有一个进程调度策略。在此策略中,有一个 40 分制的量表用于衡量进程的优先级。优先级是描述进程相对优先级的度量单位。在讨论优先级时,请注意,量表为 -20(最高优先级)到 19(最低优先级),并且进程从父进程继承其优先级(通常为 0)。
[ 您可能还会喜欢: Linux 命令基础:7 个进程管理命令 ]
现在,如果进程的 niceness 级别较低(低于零),则它放弃 CPU 使用的可能性较小(不太好,不是吗?)。另一方面,具有较高 niceness 值(超过零)的进程将更有可能放弃 CPU 使用。
记住这一点的一个简单方法是,如果某人(或某个进程)非常友善,他们就更愿意与他人分享。如果他们不太友善,他们就会更倾向于保护属于他们的东西(在本例中是 CPU 资源)。
此外,如果资源充足,即使是优先级较高的进程也会使用所有可用的 CPU 时间。只有在带宽不足时,进程才会让出资源。
优先报告
现在我们已经了解了有关进程优先级的所有背景信息,让我们看看在终端上查看这些信息的一些方法。
The quickest, most basic way to view niceness information is to use the top
command. It displays this information by default in the fourth column (left to right) and therefore doesn't require any additional options or flags.
The other and slightly more involved method is to use the ps
command with a generous helping of formatting options. The following command will display processes with their process IDs, name, niceness level, and scheduling class (sorted descending):
[tcarrigan@localhost ~]$ ps axo pid,comm,nice,cls --sort=-nice
Now in real time:
Manipulating existing niceness
We now know how to view an existing process's niceness, but what if that nice level doesn't suit our needs? How can we change those values to get the most out of our system? Well, if you have a particular process that is eating up resources that you need elsewhere, you can lower the nice level of that specific process. To do this, we will use the renice
command.
Note: only the root user may reduce a niceness level (increase priority). Unprivileged users may increase nice levels but cannot decrease them without root permissions.
Take a look at this sleep 500
process. It has a PID of 23990 and a default nice level of 0.
[tcarrigan@localhost ~]$ ps -o pid,comm,nice 23990
PID COMMAND NI
23990 sleep 0
If I wanted to make that process less of a priority, I would use the following renice
command:
To make the process more important (lowering the nice level), you would use the same command syntax as a privileged user.
[ Free cheat sheet: Kubernetes glossary ]
One last thing
Okay, before we wrap up, there is one more trick I wanted to include here. If you are going to start a process that usually has a default nice level that is undesirable, you can actually set that parameter as you spawn the process. We will use the sleep
command again. However, this time, we will create it with the nice
command (which defaults the process to nice level 10).
You can set a specific nice level by using the same command with the following syntax:
[tcarrigan@localhost ~]$ nice -n 19 sleep 500 &
The above command sets the background sleep 500
to a nice level of 19.
These commands were a ton of fun to learn and show off. In the future, when I run into issues with system resources or hungry processes, I can modify the resources allocated to them.