How to Set Process Priorities With nice and renice on Linux

Bash shell on Ubuntu laptop

The nice and renice commands allow you to refine how the kernel processes your processes by adjusting their priorities. Read this tutorial to learn how to use them in Linux and Unix operating systems such as macOS.

It's all about process

In your Linux or Unix computer, many processes will be running even before launching the application you want to use. Most of these processes are vital parts of Linux itself or support processes for your graphical desktop environment. There is a lot going on behind the scenes. Of course, there are only a few system resources and processor time to go through. The Linux kernel is the controller for all these processes.

It's the core that has to decide which processes are currently attracting attention and resources, and which ones have to wait. The core continually juggles with processes and priorities to ensure the optimal operation of the computer and the proper sharing of all processes. Some processes benefit from preferential treatment. They are so important to the overall operation of the computer that their needs should be prioritized, for example, on your browser.

The beautiful value

One of the criteria used to determine how the kernel processes a process is the value of courtesy. Each process has an interesting value. The courtesy value is an integer between -19 and 20. All standard processes are started with a nice value of zero.

The trick here is that the higher the trading value, the more comfortable your process is with other processes. In other words, a high value value tells the kernel that this process is happy to wait. A negative number is the opposite of being nice. The higher the value of the negative value, the more selfish the process. It tries to get as much CPU time as possible, with no regard to other processes.

We can use the nice command to set the nice value when a process is launched and we can use Renice to adjust the value of Nice of a process in progress.

The beautiful order

We can use the nice command to adjust the nice value of a program when it is launched. This allows us to increase or decrease the priority given to the process by the kernel, compared to other processes.

Suppose a programmer has written a program called ackermann. This calculates Ackerman functions. It requires a lot of CPU and memory resources. The programmer can launch the program with the following command:

./ackermann

ackermann command in the terminal window

We can use the top command to display the program that is running.

top

top running in a terminal

We can see the details of the ackermann program at the top. The interesting value is the number in the "NI column". It is set to zero as we expected.

Let it restart and this time make it less demanding. We will set an interesting value of 15 for the ackermann program as follows. Type nice, a space, -15, another space, and then the name of the program you want to launch. In our example, our fictional programmer uses ./ackermann.

well -15 ./ackermann

nice order 15 in the terminal window

Be careful, the "-15" is not negative. It's fifteen positive. The "-" is needed to kindly indicate that we are going into a parameter. To indicate a negative number, you must type two "-" characters.

If we start now at the top, we can see ackermann's behavior change.

top

top running in a terminal

With a cool value of 15, ackermann does not consume the most CPU time. GNOME and Rhythmbox both use more. We quickly mastered ackermann.

Now let's do the opposite and give ackermann a nice negative value. Note the use of two characters "-". To make an application more selfish and less pleasant, you must use sudo. Everyone can make their application more enjoyable, but only superusers can make one more selfish.

sudo nice –10 ./ackermann

nice -10 command in the terminal window

Let's run upstairs and see what has changed.

top

top running in a terminal

This time, ackermann has a nice value of -10. It comes back on the bottom line and consumes more processor time than before.

The command renice

The renice command allows us to adjust the value value of a running process. We do not need to stop it and restart it with nice. We can set a new value on the fly.

The renice command uses the process ID, or PID, of the process as a command line parameter. We can extract the process ID from the "PID" column at the top or use ps and grep to find it, as follows. Obviously, you type the name of your user instead of dave and the name of the process you are interested in instead of ackermann.

ps -eu dave | Grep Ackermann

ps goes through grep in a terminal window

Now that we have the PID, we can use it with Renice. We will give ackermann a more pleasant behavior with an interesting value of five. To change the transaction value of an ongoing process, you must use sudo. Note that there is no "-" on parameter 5. You do not need a single for positive numbers and you only need one, and not two, for negative numbers.

sudo renice -n 5 2339

renice command running in a terminal window

We get confirmation that Renice has changed the value of the value. This shows us the old value and the new value.

The kernel usually does an excellent job of prioritizing and distributing processor time and system resources. However, if you have to run a long, CPU-intensive task, and you have nothing to do with it at the end of the process, your computer will run a little smoother if you set a higher control value for this task. It will be more enjoyable for everyone.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.