Priority of process in Linux | nice value
Last Updated : 14 Oct, 2024
In Linux, every running program is considered a process. Each process requires space in RAM and CPU time to execute. Processes have assigned priorities, which dictate the order in which they are executed by the CPU. This priority is managed using the nice value, a parameter that determines how much CPU time a process gets. we will discuss how the nice and renice commands are used to manage process priorities. The running instance of the 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 the column.
nice and renice Commands
The nice command is used to start a process with a specified nice value, while the renice command is used to alter priority of the running process.
Nice Command Syntax
nice [OPTION] [COMMAND [ARG]...]
where,
- OPTION: Set the nice value (e.g., -n).
- COMMAND: Command to be executed with the specified priority.
- ARG: Arguments passed to the command.
Renice Command Syntax
renice [PRIORITY] [-p PID]
where,
- PRIORITY: The new priority to be assigned to a process.
- -p PID: The Process ID (PID) of the process you want to modify.
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:- most priority Process 20: - least priority Process
One important thing to note is nice value only controls CPU time assigned to process and not utilization of memory and I/O devices.
Common Options Used with the Commands
Option | Description |
|---|
-n | Set the nice value for a new process. |
|---|
-p | Modify the nice value for a process using its PID. |
|---|
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 0Now 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 -5Usage 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 0You 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 5Conclusion
The nice and renice commands are useful tools for managing process priorities in Linux. By adjusting the nice values, you can control how much CPU time is allocated to processes, which helps in managing system performance. However, keep in mind that normal users can only decrease a process's priority, while increasing the priority (i.e., setting a negative nice value) requires root privileges.
Explore
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting
Linux Administrator System