linux设置进程优先级_如何在Linux上很好地设置进程优先级

linux设置进程优先级

linux设置进程优先级

Bash shell on Ubuntu laptop
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

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.

nicerenice命令使您可以通过调整优先级来微调内核如何对待进程。 阅读本教程,了解如何在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
ackermann command in terminal window

We can use the top command to view the running program.

我们可以使用top命令查看正在运行的程序。

top
top running in a terminal

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
nice 15 command in terminal window

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
top running in a terminal

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
nice -10 command in terminal window

Let’s run top and see what difference that has made.

让我们来看看有什么不同。

top
top running in a terminal

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,也可以使用psgrep为我们找到它,如下所示。 显然,您将输入用户名而不是dave ,而不是ackermann来输入您感兴趣的进程的名称。

ps -eu dave | grep ackermann
ps piped through grep in a terminal window

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
renice command running in a terminal window

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密集型任务,并且不在乎何时结束,那么如果为该任务设置较高的值,它将使您的计算机运行起来更加流畅。 这对每个人都会更好。

翻译自: https://www.howtogeek.com/411979/how-to-set-process-priorities-with-the-nice-and-renice-commands-in-linux/

linux设置进程优先级

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值