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.
- You can launch a programe with your required priority.
- 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.
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.
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.
-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.
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:
Post a Comment