The commands and
allow you to fine-tune how the kernel handles your processes by adjusting their priorities. Read this tutorial to learn how to use it on Linux and Unix-like operating systems like macOS.
It's all a matter of process
Many processes are already running in your Linux or Unix-like computer You start the application you want to use. Most of these processes are important elements of Linux itself or supporting processes for your graphical desktop environment. There is a lot going on behind the scenes. Of course, there are only so many system resources and CPU time. The Linux kernel is the controller for all these processes.
It is the kernel that has to decide which processes are getting attention and resources and which ones have to wait. The kernel constantly juggles processes and priorities to ensure that the computer runs as smoothly as possible and that all processes receive their corresponding share. Some processes are preferred. They are so important to the overall operation of the computer that their needs must come first in front of your browser.
The nice Value
One of the criteria that determines how the kernel handles a process is its good value. Every process has a good value. The nice value is an integer in the range of -1
The trick here is that the higher the nice value, the nicer the process is on the other processes. In other words, a high value tells the kernel that this process is waiting. A negative number is the opposite of being nice. The greater the negative Nice value, the more the process is selfish . It tries to get as much CPU time as possible, regardless of the other processes.
We can use the command
nice to commit the nice value of a process is started and we can use
renice to the nice value of an ongoing process adjust.
The nice command
We can use the command
nice to adjust the nice value for a program when we start it. This allows us to increase or decrease the priority that the kernel gives to the process compared to the other processes.
Let's say a programmer wrote a program called
ackermann . This calculates the Ackerman functions. It is CPU and memory intensive. The programmer can start the program with the following command:
We can use the command
above to display the running program. 19659013] top
We can see the details of the program
above . The nice value is the number in the "NI column". It was set to zero as expected.
Restart it and make it less exhausting this time. We will set a nice value of 15 for the program
ackermann as follows. Type nice, a space, -15, another space, and the name of the program you want to start. In our example, our fictitious programmer uses the command
nice -15 ./ackermann[1965902115commandintheterminalwindow" width="644" height="55" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>
15 "is not negative 15. It is positive fifteen. The "-" is required to tell
nicethat we are passing a parameter. To display a negative number, you must enter two "-" characters.
If we start again
abovewe can observe the behavioral change of
With a good value of 15,
ackermanndoes not consume the most CPU time. GNOME and Rhythmbox both use more. We have forced
Now we do the opposite and give
ackermanna negative nice value. Note the use of two "-" characters. To make an application more selfish and less nice, you must use
sudo. Everyone can make their application more beautiful, but only superusers can make you selfish.sudo nice --10 ./ackermann[196590293nice-10InstructionTerminalwindow"width="644"height="75"src="/pagespeed_static/1JiBnMqyl6Sgif"onload="pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"onerror="thisonerror=null;pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"/>
Let's run above and see what difference that has made.top
ackermannhas a good value of -10. It is back in the top line and consumes more CPU time than before.
The command renice
With we can adjust the nice value of a running process to the command
reniceWe do not have to stop it and restart with
niceWe can set a new value immediately
The command renice [19459005 takes the process ID (PID) of the process as a command line parameter n either extract the process ID from the column "PID" in
aboveor we can find
grepas follows for us. Of course, instead of
daveenter the name of your user and instead of
ackermannthe name of the process you are interested in.ps -eu dave | grep ackermann
Now that we have the PID, we can use it with
renice. We will post
ackermannon a better behavior with a nice value of five. To change the value for a running process, you must use
sudo. Note that parameter
5does not contain "-". You do not need positive numbers and you only need one, not two for negative numbers.sudo renice -n 5 2339
We receive a confirmation that
Renicechanged the beautiful value. It shows us the old value and the new value.
The kernel usually handles priorities and spends CPU time and system resources. However, if you need to do a long, CPU-intensive task and you do not care when it's done, your computer will run a little smoother if you set a higher value for this task. This will be nice for everyone.