linux设置进程优先级
The nice
and renice
commands let you fine-tune how the kernel treats your processes by adjusting their priorities. Read this tutorial to learn how to use them in Linux and Unix-like operating systems such as macOS.
nice
和renice
命令使您可以通过调整优先级来微调内核如何对待进程。 阅读本教程,了解如何在Linux和类似macOS的Unix操作系统中使用它们。
这都是过程的问题 (It’s All a Matter of Process)
Inside your Linux or Unix-like computer, there will be many processes running even before you launch the application you want to use. The majority of these processes are vital elements of Linux itself or supporting processes for your graphical desktop environment. There’s a lot going on behind the scenes. Of course, there are only so many system resources and CPU time to go around. The Linux kernel is the controller for all of these processes.
在Linux或类似Unix的计算机中,甚至在启动要使用的应用程序之前,就会有许多进程在运行。 这些过程中的大多数是Linux本身或图形桌面环境支持过程的重要元素。 幕后发生了很多事情。 当然,只有这么多的系统资源和CPU时间需要花费。 Linux内核是所有这些过程的控制器。
It is the kernel that has to decide which processes get attention and resources right now, and which ones have to wait. The kernel is continually juggling processes and priorities to ensure that the computer runs as smoothly as it can and that all processes get their appropriate share. Some processes get preferential treatment. They are so important to the general operation of the computer that their needs have to come first ahead of, say, your browser.
内核必须决定当前哪些进程获得关注和资源,哪些进程必须等待。 内核会不断处理进程和优先级,以确保计算机尽可能平稳地运行,并确保所有进程都能获得适当的份额。 一些过程得到优惠待遇。 它们对于计算机的常规操作非常重要,因此必须首先满足您的需求,例如您的浏览器。
不错的价值 (The nice Value)
One of the criteria used to determine how the kernel treats a process is the nice value. Every process has a nice value. The nice value is an integer in the range of -19 to 20. All standard processes are launched with a nice value of zero.
好的值是用来确定内核如何处理进程的标准之一。 每个过程都有很好的价值。 nice值是介于-19到20之间的整数。所有标准进程均以0的尼斯值启动。
The trick here is that the higher the nice value, the nicer your process is being to the other processes. In other words, a high nice value tells the kernel that this process is happy to wait. A negative number is the opposite of being nice. The larger the negative nice value, the more selfish the process is. It is trying to get as much CPU time as it can, with no regard for the other processes.
这里的技巧是,nice值越高,您的过程对其他过程的影响就越好。 换句话说,高nice值告诉内核该过程很高兴等待。 负数与友善相反。 负好值越大,该过程越自私 。 它正在尝试获取尽可能多的CPU时间,而不考虑其他进程。
We can use the nice
command to set the nice value when a process is launched and we can use renice
to adjust the nice value of a running process.
我们可以使用nice
命令来设置 启动进程时的nice值,也可以使用renice
来调整 正在运行的进程的nice值。
漂亮的命令 (The nice Command)
We can use the nice
command to adjust the nice value for a program as we launch it. This allows us to increase or decrease the priority given to the process by the kernel, relative to the other processes.
启动程序时,可以使用nice
命令调整程序的nice值。 相对于其他进程,这允许我们增加或减少内核赋予进程的优先级。
Let’s suppose a programmer has written a program called ackermann
. This calculates Ackerman functions. It is CPU and memory intensive. The programmer can launch the program with the following command:
假设程序员编写了一个名为ackermann
的程序。 这将计算Ackerman函数 。 它占用大量CPU和内存。 程序员可以使用以下命令启动程序:
./ackermann
We can use the top
command to view the running program.
我们可以使用top
命令查看正在运行的程序。
top
We can see the details of the ackermann
program in top
. The nice value is the figure in the “NI column”. It set to zero as we’d expect.
我们可以在top
看到ackermann
程序的详细信息。 好的值是“ NI列”中的数字。 正如我们期望的那样,它设置为零。
Let’s restart it and this time make it less demanding. We’ll set a nice value of 15 for the ackermann
program as follows. Type nice, a space, -15, another space, and then the name of the program you wish to launch. In our example, our fictitious programmer is using ./ackermann
.
让我们重新启动它,这次使它的要求降低了。 我们将为ackermann
程序设置一个很好的值为15,如下所示。 键入nice,一个空格,-15,另一个空格,然后输入您要启动的程序的名称。 在我们的示例中,我们的虚拟程序员正在使用./ackermann
。
nice -15 ./ackermann
Take careful note, the “-15” is not negative fifteen. It is positive fifteen. The “-” is required to tell nice
we’re passing in a parameter. To indicate a negative number you must type two “-” characters.
请注意,“-15”不是负数15。 现在是十五岁。 该“ - ”需要告诉nice
,我们正在传递的参数。 要表示一个负数,您必须输入两个“-”字符。
If we now start top
again, we can see the change in the behavior of ackermann
.
如果我们现在开始top
再次,我们可以看到的行为的变化ackermann
。
top
With a nice value of 15, ackermann
is not consuming the most CPU time. GNOME and Rhythmbox are both using more. We’ve reined ackermann
in a bit.
ackermann
值为15时,它不会消耗最多的CPU时间。 GNOME和Rhythmbox都在使用更多。 我们稍微控制了ackermann
。
Now let’s do the reverse and give ackermann
a negative nice value. Note the use of two “-” characters. To make an application more selfish and less nice, you must use sudo
. Anyone can make their application more nice, but only superusers can make one more selfish.
现在,让我们做相反的事情,并为ackermann
赋予负的不错的价值。 请注意使用两个“-”字符。 要使应用程序变得更自私和不太友好,必须使用sudo
。 任何人都可以使他们的应用程序更加美观,但是只有超级用户才能使自私。
sudo nice --10 ./ackermann
Let’s run top and see what difference that has made.
让我们来看看有什么不同。
top
This time ackermann
has a nice value of -10. It is back on the top line and consuming more CPU time than before.
这次ackermann
的值为-10。 它重新回到了第一位,并且比以前消耗更多的CPU时间。
renice命令 (The renice Command)
The renice
command lets us adjust the nice value of a running process. We don’t need to stop it and relaunch it with nice
. We can set a new value on-the-fly.
renice
命令使我们可以调整正在运行的进程的精确值。 我们不需要停止它并以nice
重新启动它。 我们可以即时设置新值。
The renice
command takes the process ID, or PID, of the process as a command line parameter. We can either extract the process ID from the “PID” column in top
, or we can use ps
and grep
to find it for us, as follows. Obviously, you’ll type the name of your user instead of dave
and the name of the process you’re interested in instead of ackermann
.
renice
命令将进程的进程ID或PID作为命令行参数。 我们可以从top
的“ PID”列中提取进程ID,也可以使用ps
和grep
为我们找到它,如下所示。 显然,您将输入用户名而不是dave
,而不是ackermann
来输入您感兴趣的进程的名称。
ps -eu dave | grep ackermann
Now that we have the PID we can use this with renice
. We’re going to set ackermann
back to a nicer behavior with a nice value of five. To change the nice value for a running process you must use sudo
. Note that there is no “-” on the 5
parameter. You don’t need one for positive numbers and you only need one, not two, for negative numbers.
现在我们有了PID,可以将它与renice
一起使用。 我们将把ackermann
设置为一个更好的行为,其值为5。 要更改正在运行的进程的值,必须使用sudo
。 请注意,参数5
上没有“-”。 对于正数,您不需要一个,对于负数,您只需要一个,而不是两个。
sudo renice -n 5 2339
We get confirmation that renice
has changed the nice value. It shows us the old value and the new value.
我们确认renice
改变了不错的价格。 它向我们展示了旧价值和新价值。
The kernel usually does a great job of handling priorities and handing out CPU time and systems resources. But if you have a long, CPU intensive task to run and you don’t care when it concludes, it’ll make your computer run a bit smoother if you set a higher nice value for that task. That will be nicer for everyone.
内核通常在处理优先级以及分发CPU时间和系统资源方面做得很好。 但是,如果您要执行很长的CPU密集型任务,并且不在乎何时结束,那么如果为该任务设置较高的值,它将使您的计算机运行起来更加流畅。 这对每个人都会更好。
linux设置进程优先级