Viewing priority of different running processes in Linux, and how to change it ?
In this post, we will look various ways how to view priority of a linux process and how to change it.
1. Viewing Priority of Linux process
a. Using Top command
[root@nglinux ~]# top top - 06:21:36 up 4:19, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 135 total, 1 running, 134 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 899192k total, 415420k used, 483772k free, 70460k buffers Swap: 0k total, 0k used, 0k free, 166572k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 2896 1436 1208 S 0.0 0.2 0:01.22 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 4 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0 5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0 6 root RT 0 0 0 0 S 0.0 0.0 0:00.07 watchdog/0 7 root 20 0 0 0 0 S 0.0 0.0 0:04.74 events/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events/0 9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_long/0 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_power_ef 11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
In the above output, PR column shows the priority.
Sorting top command Output
We can press letter “O”(to select field for sorting) and then select “I”(for nice value) and can have processes sorted as per their nice value.
top - 06:25:26 up 4:22, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 135 total, 1 running, 134 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 899192k total, 415544k used, 483648k free, 70484k buffers Swap: 0k total, 0k used, 0k free, 166572k cached NI PID USER PR VIRT SHR S RES %CPU %MEM TIME+ COMMAND -20 1498 root 0 0 0 S 0 0.0 0.0 0:00.00 fcoethread/0 -20 1501 root 0 0 0 S 0 0.0 0.0 0:00.00 bnx2fc_l2_threa -20 1502 root 0 0 0 S 0 0.0 0.0 0:00.00 bnx2fc_thread/0 -11 2359 gdm 9 99.5m 3104 S 4016 0.0 0.4 0:01.76 pulseaudio -4 413 root 16 3020 372 S 1280 0.0 0.1 0:00.23 udevd -4 1385 root 16 13012 612 S 820 0.0 0.1 0:00.01 auditd -2 2140 root 18 3544 580 S 2028 0.0 0.2 0:00.00 udevd -2 2141 root 18 3544 580 S 2032 0.0 0.2 0:00.00 udevd 0 1 root 20 2896 1208 S 1436 0.0 0.2 0:01.22 init 0 2 root 20 0 0 S 0 0.0 0.0 0:00.00 kthreadd 0 3 root RT 0 0 S 0 0.0 0.0 0:00.00 migration/0 0 4 root 20 0 0 S 0 0.0 0.0 0:00.02 ksoftirqd/0 0 5 root RT 0 0 S 0 0.0 0.0 0:00.00 stopper/0
The column NI refers to nice value of a process. NI value ranges from -20(Highest priority) to 20(lowest priority) on most Unix/Linux like operating systems.
-20 20 Highest priority Lowest priority process process
b. Using ps command
[root@nglinux ~]# ps -eo uid,pid,ppid,pri,ni,cmd UID PID PPID PRI NI CMD 0 1 0 19 0 /sbin/init 0 2 0 19 0 [kthreadd] 0 3 2 139 - [migration/0] 0 4 2 19 0 [ksoftirqd/0] 0 5 2 139 - [stopper/0] 0 6 2 139 - [watchdog/0] 0 7 2 19 0 [events/0] 0 8 2 19 0 [events/0] 0 9 2 19 0 [events_long/0]
In the above output, we can see PRI and NI values to determine the priority of a process in Linux.
Sorting ps command output
ps command supports an option “–sort” to sort the command output by any field/column.
[root@nglinux c_programs]# ps --sort=pri -lu root F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 724 - ? 00:00:01 init 1 S 0 2 0 0 80 0 - 0 - ? 00:00:00 kthreadd 1 S 0 3 2 0 -40 - - 0 - ? 00:00:00 migration/0 1 S 0 4 2 0 80 0 - 0 - ? 00:00:00 ksoftirqd/0 1 S 0 5 2 0 -40 - - 0 - ? 00:00:00 stopper/0 1 S 0 6 2 0 -40 - - 0 - ? 00:00:00 watchdog/0 1 S 0 7 2 0 80 0 - 0 - ? 00:00:25 events/0 1 S 0 8 2 0 80 0 - 0 - ? 00:00:00 events/0 1 S 0 9 2 0 80 0 - 0 - ? 00:00:00 events_long/0 1 S 0 10 2 0 80 0 - 0 - ? 00:00:00 events_power_ef 1 S 0 11 2 0 80 0 - 0 - ? 00:00:00 cgroup 1 S 0 12 2 0 80 0 - 0 - ? 00:00:00 khelper 1 S 0 13 2 0 80 0 - 0 - ? 00:00:00 netns 1 S 0 14 2 0 80 0 - 0 - ? 00:00:00 async/mgr
2. Relation between PRI(priority) and NI(nice) Value.
nice value is set in the user space whereas PRI(priority) is the actual priority of a process determined by Linux kernel.
Priorities range from -40 to 99 in which -40(highest priority process) to 60 are for real time processes.
Lower the priority(PRI) value, higher the priority at kernel level.
The default priority of a user process is 80 on a nice value of 0.
And 61(highest priority user program, -19 Nice value) to 99(lowest priority user program, +19 nice value) are for user programs.
Actual Priority=PR+NI PR = 80 + (-20 to +19) i.e. 39 which maps 60 to 99.
Lets have a look on the output below to understand.
Example 1: Default PRI is 80 and nice value is 0 F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 724 - ? 00:00:01 init 1 S 0 2 0 0 80 0 - 0 - ? 00:00:00 kthreadd 1 S 0 3 2 0 -40 - - 0 - ? 00:00:00 migration/0 1 S 0 4 2 0 80 0 - 0 - ? 00:00:00 ksoftirqd/0 1 S 0 5 2 0 -40 - - 0 - ? 00:00:00 stopper/0 Example 2: udevd process with nice value of -2 has PRI value 78 i.e. a high priority process. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 2132 1 0 80 0 - 501 - tty6 00:00:00 mingetty 5 S 0 2140 413 0 78 -2 - 886 - ? 00:00:00 udevd 5 S 0 2141 413 0 78 -2 - 886 - ? 00:00:00 udevd 0 S 0 2163 2119 0 80 0 - 5073 - ? 00:00:00 gdm-simple-slav
3. Start a Process with new priority(nice command)
Lets see how to start a process with low or high priority using nice command.
High priority ### Run top command with high priority. [root@nglinux c_programs]# nice -n -10 top top - 02:32:00 up 1 day, 29 min, 2 users, load average: 0.00, 0.00, 0.00 Tasks: 137 total, 1 running, 136 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.8%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 899192k total, 452280k used, 446912k free, 79256k buffers top - 02:33:07 up 1 day, 30 min, 3 users, load average: 0.00, 0.00, ### Check out the priority in another terminal. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 5016 5013 0 80 0 - 1312 - pts/1 00:00:00 bash 4 S 0 5472 5016 0 70 -10 - 676 - pts/1 00:00:00 top Low Priority ### Start top command with lowest priority +20 [root@nglinux c_programs]# nice -n 20 top top - 02:36:24 up 1 day, 33 min, 3 users, load average: 0.00, 0.00, 0.00 ### Check our priority and nice value in another terminal. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 5476 5473 0 80 0 - 1282 - pts/2 00:00:00 bash 4 S 0 5501 5016 0 99 19 - 676 - pts/1 00:00:00 top
4. Change priority of a Process (renice command)
Now lets have a look how to change priority of a running process or command.
To achieve this, we can use renice command.
NAME renice - alter priority of running processes SYNOPSIS renice [-n] priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...] renice -h | -v
Usage
### Check current priority of a PID/process. [root@nglinux ~]# ps -o pid,uid,pri,ni,cmd 5501 PID UID PRI NI CMD 5501 0 0 19 top ### Increase the priority [root@nglinux ~]# renice -n -15 5501 5501: old priority 19, new priority -15 ### Now check the priority again. [root@nglinux ~]# ps -o pid,uid,pri,ni,cmd 5501 PID UID PRI NI CMD 5501 0 34 -15 top [root@nglinux ~]#
5. Set default nice value for a particular user or group
We can also set the default nice value for a particular user or group in /etc/security/limits.conf file.
Entry Syntax: [username] [hard|soft] priority [nice value]
username hard priority 5 @groupname hard priority 10 ### Make an entry in limits.conf file. [root@nglinux ~]# tail -3 /etc/security/limits.conf #@student - maxlogins 4 * soft core unlimited nglinux hard priority 5 ### On Terminal 1 [nglinux@nglinux ~]$ top ### On Terminal 2 [root@nglinux c_programs]# ps -u nglinux -o uid,pid,pri,ni,cmd UID PID PRI NI CMD 500 5575 14 5 -bash 500 5601 14 5 top