I'm trying to implement a different flavor of the 'nice' command of unix in C. I have seen the definitions of nice() system call and setpriority() call. The nice() call only increments/decrements the priority of the process. If I want to set the priority of a process to a particular value, can't I use the nice() call? Basically, other than how the priority is modified, is there any difference between nice() and setpriority() ?
3 Answers
It's historical. nice()
was introduced long before setpriority()
. For backwards compatibility, the nice
function was retained.

- 25,660
- 5
- 55
- 79
-
so setpriority is an advancement of nice.. thanks, tat cleared my doubt.. i was wondering if the 'priority' referred by nice and setpriority was different (lame, i know :) ) then i'll implement nice with setpriority() itself. – Aswin Parthasarathy Oct 01 '11 at 05:18
-
@AswinParthasarathy It's similar to the history of dup2. In both cases, people realized that more general (or more specific) behaviors were needed, which is why some functions seem to have overlapping behavior – Foo Bah Oct 01 '11 at 05:21
-
@Aswin: actually there are other priorities, which are set via `sched_setscheduler()`. – ninjalj Oct 01 '11 at 10:25
nice
sets your own priority (the niceness of the current process). setpriority
lets you set the niceness of other processes (or process groups or users). Think of it as renice
.
man 3p nice
int nice(int incr);
man 3p setpriority
int setpriority(int which, id_t who, int value);

- 47,994
- 12
- 82
- 119
-
yeah but i can accomplish with setpriority, the same as what I accomplish with nice, right? – Aswin Parthasarathy Oct 01 '11 at 05:13
-
1Yes, but nice *only* affects the current process, where setpriority can affect (potentially) any process. That's the difference. – Chris Eberle Oct 01 '11 at 05:16
-
@Chris the question seems to be focused on why someone would use nice when setpriority/getpriority can be used to replicate the behavior – Foo Bah Oct 01 '11 at 05:17
-
1The name says it all -- because it's nice :D But seriously it's just for convenience. – Chris Eberle Oct 01 '11 at 05:18
nice()
modifies the nice value of the current process relative to its current nice value, so the process doesn't need to know about its starting nice value, it only cares that it should be nicer to the system (e.g: a process launches a child who does some background processing, the child sets itself to be nice).
setpriority()
use case is the user explicitly setting absolute nice values to specific processes.

- 42,493
- 9
- 106
- 148