I was using the "top " command and I came across the nice value. I am aware of the priorities and how a nice value indicates the priority of a process. But I can't seem to figure out what the term "niced" or "un-niced" indicates.
1 Answers
Those terms simply refer to whether a process's default nice value has been changed. A "niced" process is one that has been run with the nice command (or whose niceness has been changed by renice) and an "un-niced" process is one that hasn't been run with nice. The default nice values for regular processes (those which haven't been run with nice command or whose niceness hasn't subsequently been changed by renice PID) is 0. So, "un-niced" processes are those with a nice value of 0 and "niced" processes are those with a nice value != 0.
A nice value does not indicate the priority of the process, it indicates the niceness of the process. You can see both values in the output of top:
$ top -b -n1 | head
top - 15:23:10 up 20:59, 1 user, load average: 1.41, 1.75, 1.88
Tasks: 222 total, 1 running, 221 sleeping, 0 stopped, 0 zombie
%Cpu(s): 14.2 us, 7.9 sy, 0.0 ni, 76.6 id, 0.6 wa, 0.0 hi, 0.6 si, 0.0 st
GiB Mem : 7.791 total, 3.082 free, 3.862 used, 0.846 buff/cache
GiB Swap: 16.000 total, 15.793 free, 0.207 used. 3.485 avail Mem
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
1591 terdon 20 0 1605.1m 362.3m 18.8 4.5 285:46.34 S chromium
1754 terdon 20 0 790.8m 184.5m 18.8 2.3 405:54.21 S chromium
1186 terdon 20 0 1992.8m 459.6m 12.5 5.8 119:31.59 S cinnamon
^^ ^^
| |---------> niceness
|-------------> priority
If I now set a nice value for one of those chromium processes, you'll see the difference:
$ renice 10 1591
1591 (process ID) old priority 0, new priority 10
$ top -b -n1 | head
top - 15:24:56 up 21:01, 1 user, load average: 0.89, 1.48, 1.77
Tasks: 225 total, 1 running, 224 sleeping, 0 stopped, 0 zombie
%Cpu(s): 14.2 us, 7.9 sy, 0.0 ni, 76.6 id, 0.6 wa, 0.0 hi, 0.6 si, 0.0 st
GiB Mem : 7.791 total, 3.033 free, 3.908 used, 0.849 buff/cache
GiB Swap: 16.000 total, 15.793 free, 0.207 used. 3.439 avail Mem
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
1591 terdon 30 10 1605.1m 362.6m 23.5 4.5 286:10.40 S chromium
1754 terdon 20 0 790.8m 184.5m 18.8 2.3 405:54.21 S chromium
1186 terdon 20 0 1992.8m 459.6m 12.5 5.8 119:31.59 S cinnamon
The nice value of a process is used to determine its priority, but the actual priority of a process (the PR column) is not the same as its niceness. Put simply, niceness values determine how "nice" a process is to the other processes of your system. If it is very nice (high nice values), it will be "polite" and allow other processes to take precedence and use more CPU time (in other words, it will have a low priority). If it is not nice, it will try to get as much CPU time as possible for itself (so it will have a high priority).
To make things even weirder, priority values range from -20 (the highest) to +20 (the lowest) and niceness values range from 19 (the highest niceness, so the lowest priority) to -20 (the lowest niceness, so the highest priority).
I don't know the exact mechanism by which a niceness value is converted into a priority. The details seem to depend on the kernel version and specific implementation (see the "Notes" section here). However, as a crude approximation, you can think of it as:
Priority = DefaultPriority + Niceness
To illustrate, I'll launch 3 instances of a script called foo.sh (which just runs a sleep command) with different niceness values:
foo.sh & # default
nice -n 10 foo.sh
nice -n 15 foo.sh
sudo nice -n -10 foo.sh
The last one uses sudo because only root can start processes with negative (high) priority. Now, let's see what their priorities are:
$ top -b -n1 | grep foo.sh
21958 terdon 20 0 13.3m 2.7m 0.0 0.0 0:00.00 S foo.sh
22148 terdon 30 10 13.3m 2.7m 0.0 0.0 0:00.00 S foo.sh
22181 terdon 35 15 13.3m 2.7m 0.0 0.0 0:00.00 S foo.sh
23480 root 10 -10 13.3m 2.6m 0.0 0.0 0:00.00 S foo.sh
As you can see above, the priority is equal to the sum of the niceness value and 20, the default priority.
- 104,119