UN*Xoids

nice is the name of a system call to change the scheduling "priority" of a running process. It is also the name of a program which uses this system call to exec some program with a different "priority".

nice(), the system call

nice(priority) sets the "priority" of the current process to priority. This is a value between -20 and +19; low values of priority run faster (this is distinct from what most people would consider normal usage). The default priority is 0.

Successive calls to nice will set a new priority, relative to the old one. However, only root may decrease priority. So any process which you, the lowly user, can create without help from the superuser will have priority≥0. And after calling nice(5), say, a call to nice(2) will change your "priority" to 7, not to 2.

A related system call setpriority lets you set niceness of other processes, process groups or "users" (i.e. all processes running with a given UID).

nice, the command

nice [-priority] PROGRAM ARG1 ... runs the given program with the given arguments, at the given priority. It works by calling the system call nice(priority), then exec'ing the given program. So all restrictions on priority are transferred from the system call to the command. If priority isn't given, the default priority of +10 is used.

Note the priority is preceded by the usual "-" option sign. So "-10" is the (default) "priority" of +10; to set a "priority" of -10 (and you have to be root to do that), you use "--10"...

There is also a renice command (any adjective can be verbed?), which uses setpriority to change the priority of (other) already running processes.

"Priority" controls how much CPU time a process will receive. Processes eligible to run are selected to run by the scheduler based on how long they've been waiting to run; the mechanism takes the "priority" into account to give the desired effect.

Example

To see the effects of nice in the comfort of your own $HOME, try this:

[ariels@HumptyDumpty tmp]$ ./infinite_loop & ./infinite_loop &
[5] 25234
[6] 25235
[ariels@HumptyDumpty tmp]$ top -i -b -n 1 -p 25234 -p 25235
...
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
25234 ariels    18   0  1100 1100   956 R    49.5  0.4   0:06 infinite_loop
25235 ariels    19   0  1100 1100   956 R    50.4  0.4   0:06 infinite_loop
By default, both processes receive (roughly) the same amount of CPU time: about 50% each. But if we nice one of our infinite loops:
[ariels@HumptyDumpty tmp]$ ./infinite_loop & nice ./infinite_loop &
[7] 25237
]8] 25238
[ariels@HumptyDumpty tmp]$ top -i -b -n 1 -p 25237 -p 25238
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
25237 ariels    20   0  1100 1100   956 R    66.4  0.4   0:08 infinite_loop
25238 ariels    18  10  1100 1100   956 R N  31.8  0.4   0:04 infinite_loop
The first process is getting twice as much CPU time than the second. We can increase the discrepancy further:
[ariels@HumptyDumpty tmp]$ ./infinite_loop & nice -15 ./infinite_loop &
[11] 25264
[12] 25265
[ariels@HumptyDumpty tmp]$ top -i -b -n 1 -p 25264 -p 25265
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
25264 ariels    20   0  1100 1100   956 R    74.7  0.4   0:12 infinite_loop
25265 ariels    19  15  1100 1100   956 R N  25.2  0.4   0:04 infinite_loop

Trying to nice a process with a negative "priority" will fail, of course -- ariels isn't root:

[ariels@HumptyDumpty tmp]$ nice --10 sh
nice: cannot set priority: Permission denied
So I can't get a "priority" better than 0. And if I open a shell with worse "priority", it can't return to priority 0. That priority may be my birthright, but it's not the shell's:
[ariels@HumptyDumpty tmp]$ nice bash
# Our new shell is now running at "priority" 10.
[ariels@HumptyDumpty tmp]$ ./infinite_loop & nice -5 ./infinite_loop &
[1] 25332
[2] 25333
ariels@HumptyDumpty tmp$ top -i -b -n 1 -p 25332 -p 25333
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
25332 ariels    20  10  1100 1100   956 R N  58.9  0.4   0:13 infinite_loop
25333 ariels    20  15  1100 1100   956 R N  41.1  0.4   0:08 infinite_loop
The second infinite_loop's "priority" is definitely worse than the first's. Since the first was at "priority" 10, the second is at "priority" 10+5=15.