Linux menu

Wednesday, September 17, 2014

Nice and Renice Command Usage For Process Priority In Linux

On a Linux machine, there are hundred's of processes, that are continuously  running for some or the other tasks. Linux Kernel does a fantastic job in mediating between these processes and allotting CPU to these processes.
An interesting question arises when you read that line "kernel allocates CPU to all the process".
  • Computer is made to do whatever task is given, So how will kernel come to know about a particular process, which i want to be on top of the list on priority?

The answer to that question is , "You can infact ask the kernel to put a particular process of yours, on higher priority than others".
This can be easily achieved with the help of a command called "Nice" in Linux.

 What Does Nice Command In Linux Do?

 With the help of Nice command in Linux you can set process priority. If you give a process a higher priority, then Kernel will allocate more cpu time to that process.
By default when a programe is launched in Linux, it gets launched with the priority of '0'. However you can change the priority of your programes by either of the following methods.
  1. You can launch a programe with your required priority.
  2. Or you can also change the priority of an already running process.

How can i see priority of a processes running in Linux?

You can see the process priority by running ps command with "-al" option as shown below.
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0  4075  4057  15   0   4664  1504 -      Ss+  pts/0      0:00 -bash
4     0  4481     1  15   0   1656   436 -      Ss+  tty2       0:00 /sbin/minge
tty tty2
4     0  4485     1  15   0   1656   436 -      Ss+  tty3       0:00 /sbin/minge
tty tty3
4     0  4488     1  19   0   1656   452 -      Ss+  tty4       0:00 /sbin/minge
tty tty4
The column that starts with "NI" shows the nice value(priority of the process). You can clearly see that most of them has got a '0' priority.
You can also see the process priority(nice value) using "top" command.Below shown is an exampleoutput of top command.
PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 8018 root      15   0  2192  908  716 R  2.0  0.2   0:00.01 top
    1 root      15   0  2064  624  536 S  0.0  0.1   0:03.13 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      10  -5     0    0    0 S  0.0  0.0   0:00.24 events/0
    6 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khelper
    7 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kthread
   10 root      10  -5     0    0    0 S  0.0  0.0   0:02.73 kblockd/0
In the top command output shown above nice value(process priority) can be seen from the "NI" column.

Understanding Nice Command in Linux

The syntax of nice command in linux is quite simple.Help for nice command can be listed by the following method.
[root@myvm1 ~]# nice --help
Usage: nice [OPTION] [COMMAND [ARG]...]
Run COMMAND with an adjusted niceness, which affects process scheduling.
With no COMMAND, print the current niceness.  Nicenesses range from
-20 (most favorable scheduling) to 19 (least favorable).
 
  -n, --adjustment=N   add integer N to the niceness (default 10)
      --help     display this help and exit
      --version  output version information and exit
 
NOTE: your shell may have its own version of nice, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.
 
Report bugs to <bug-coreutils@gnu.org>.
-n and --adjustment can be used to set the priority of your process.You can also use -<required number> to set the process priority to that value.
For example
nice -10 <command name> Will set a process with the priority of "10".
Running nice command with no options will give you the priority with which the current shell is running.
[root@myvm1 ~]# nice
0
[root@myvm1 ~]#

What is the range of nice priority values?

Process priority values range from -20 to 19.
A process with the nice value of -20 is considered to be on top of the priority. And a process with nice value of 19 is considered to be low on the priority list.
Understand the above line very carefully, a nice value of numerically higher number is low on priority and a nice value of numerically low number is higher on priority.
running a process with nice command with no options will set that process's priority to 10.(so you can considerably increase your process priority without any options.)
Normal users can only decrease their process priority. However root user can increase/decrease all process's priority.
Lets clear a confusion that majorly exists in terms of using nice.
nice -10 <command name>  and nice -n 10 <command name>   will do the same thing.(both the above commands will make the process priority to the value 10).
A major misconception about nice command is that nice -10 <command name> will run that process with -10(a higher priority).
In order to assign -10 priority to a command then you should run it as shown below.
nice --10 <command name>
Make a note of the double --, the first one is the command option(-), and the second one is the numerical -(minus).
Note: You can only use nice command to change the process priority when you launch it. You cannot use nice command to change the priority of an already running process.

 Changing the priority of an already running process in linux

In order to change the priority of an already running process you can use "renice" command.
Suppose you see that your machine is running slow due to some lengthy process(which is not a required process at the moment), then you can reduce the priority of that process with the help of renice command. And also increase the priority of your required process at the moment.
renice command is very much similar to the nice command( interms of options in priority), however it does differ slightly in terms of parameters. You can give username,groupname etc in renice command as a parameter.
Some examples of renice command is as follows:
renice -4 -p 3423 (this will set the priority of process id no 3423 to -4, which will inturn increase its priority over others)
renice 13 -p 3564 -u sarath (this will set the priority of the process id 3564 to 13, and all the process owned by user "sarath" to the priority of 13)
renice 14 -u sarath,satish -g custom (this will set all process owned by "sarath","satish" and also the group "custom" to 14)
 Hope this post was helpful in understanding the usage of nice and renice command in Linux.

No comments: