Priority of process in Linux | nice value

The running instance of program is process, and each process needs space in RAM and CPU time to be executed, each process has its priority in which it is executed.

Now observe the below image and see column NI

top

Output:

top command output



The column NI represents nice value of a process. It’s value ranges from -20 to 20(on most unix like operating systems).

  -20                         20
most priority           least priority    
  process                  process

One important thing to note is nice value only controls CPU time assigned to process and not utilisation of memory and I/O devices.

nice and renice command
nice command is used to start a process with specified nice value, which renice command is used to alter priority of running process.

Usage of nice command :
Now let’s assume the case that system has only 1GB of RAM and it’s working really slow, i.e. programs running on it(processes) are not responding quickly, in that case if you want to kill some of the processes, you need to start a terminal, if you start your bash shell normally, it will also produce lag but you can avoid this by starting the bash shell with high priority.

For example:

nice -n -5 bash

First observe output of top without setting nice value of any process in below image

nice value of top is 0

Now start a bash shell with nice value -5, if you see the highlighted line, the top command which is running on bash shell has nice value set to -5

nice value of bash shell is -5

Usage of renice command :
To alter priority of running process, we use renice command.

renice value PID

value is new priority to be assigned
PID is PID of process whose priority is to be changed

One thing to note is you can’t set high priority to any process without having root permissions though any normal user can set high priority to low priority of a process.

We will see one example of how you alter priority of process.

nice value of gnome terminal is 0

You can observe that nice value of process(PID = 2371) is 0, now let’s try to set the new priority of 5 to this process.

renice 5 2371

Output:

2371 (process ID) old priority 0, new priority 5

You can also see this priority using top command(see highlighted line in image).

process 2371 has nice value 5



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.