Wednesday, January 10, 2018

How To Change The Priority Of A Process In Linux

https://www.ostechnix.com/change-priority-process-linux

In Unix ecosystem, a process is a running program. So, any
program running on your Linux box is a process. One or more processes
can be running on your Linux box at a time. We can display the
information about active processes using ps command. By
default, ps command only shows current user’s processes. You can use
“ps -ef” (without quotes, of course) command to display all processes.
To view the user of a process, we use “ps -u”. Hope you got a basic idea
about Linux processes. Let us now come to the point. This tutorial
addresses how to change the priority of a Process in Linux using nice and renice commands..

As you might know, by default, Linux kernel considers all processes
equally important and allocates the same amount of CPU time for each
process. Sometimes, you might want to increase or decrease the priority
of certain processes to utilize more CPU time. This is where the nice
and renice commands comes in help. Nice command is used to run a process
with an user defined priority whereas renice command is used to change
the priority of a running process. Generally, nice and renice commands
are used to change the priority than the default priority of a process.

As you see in the above output, the niceness value is listed under the column heading “NI”. So, it is clear that all processes gets the default priority value of zero.

Start a process with nice utility

If you start a process with nice command without any arguments, it gets the default value of 10. Here 10 is the niceness value or priority. Niceness values range from -20 (most favorable to the process) to 19
(least favorable to the process). To put this simply, the negative
values (Eg. -20) gives higher priority to a process and positive values
(Eg. 19) gives lower priority.
Now, let us stop the running program cmus with command:

kill -9 13829

Here, 13829 is the PID of cmus program. You will see this value under heading “PID” in the above output.
Now, run cmus program with ‘nice’ utility.

See, now cmus process got the default niceness value of 10, which means its priority has been changed.

Start a process with nice utility with lower priority

Also, you can start the same process with specific niceness value, for example 15, like below.
Kill the process with with kill command:

kill -9 13957

Again, start the process with niceness value of 15:

nice -15 cmus &

Note: Here, do not confuse – (hyphen) with minus. We use –
(hyphen) to pass a value. Hence, to assign a positive value, we use
-15. Likewise, to assign a negative value, we use –15 (i.e double
hyphen).
Let us check the niceness value using ps -al command:

Start a Process with Nice utility with higher Priority

You already know that negative niceness value represents higher priority. Please note that regular
users are not allowed to start a process with higher priority. You need
to be root user to launch any process with higher priority.
So, to start a process using nice utility with lower priority, for example -15, run the following command:

Change The Priority Of A Running Process With Renice command

What we have seen so far is we launched a process with an user
defined priority. Now, we are going to change the priority of a running
process using renice command.
Let us see the running processes using command:

To change it to negative niceness value (to assign higher priority), run the following command as root user:

# renice -n -20 -p 14749

You can even change the priorities of all running group belongs to a particular group like below.

# renice -n -20 -g ostechnix

The above command changes the priority of all running processes which are belongs to a group called ostechnix.
And also, you can change the priority of running process owned by particular user, for example sk, using command:

# renice -n -20 -u sk

For more options, I suggest you to refer the man pages of nice and renice utilities.

man nice

man renice

Conclusion

I hope you got a basic idea about what is a Linux process, and how to
change its priority using nice and renice utilities. If you find this
guide useful, please share it on your social and professional networks
and support OSTechNix. Go and nice your programs. I will be soon here
with another interesting and useful guide.
Cheers!