nice() — Change priority of a process

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <unistd.h>

int nice(int increment);

General description

nice() adds the value of increment to the nice value of the calling process. A process's nice value is a nonnegative number for which a more positive value results in a lower CPU priority.

A maximum nice value of 2*{NZERO}-1 and a minimum value of zero are imposed by the system. Requests for values above or below these limits result in the nice value being set to the corresponding limit. Only a process with appropriate privileges can lower the nice value.

The changing of a process's nice value has the equivalent effect on a process's scheduling priority value, since they both represent the process's relative CPU priority. For example, increasing one's nice value to its maximum value of (2*NZERO)-1 has the equivalent effect of setting one's scheduling priority value to its maximum value (19), and will be reflected on the nice(), getpriority(), and setpriority() functions.

Returned value

If successful, nice() return the new nice value minus (NZERO).

If unsuccessful, nice() returns -1 and sets errno to one of the following values:
Error Code
Description
ENOSYS
The system does not support this function.
EPERM
The value of increment was negative and the calling process does not have the appropriate privileges.

Because nice() can return the value -1 on successful completion, it is necessary to set the external variable errno to 0 before a call to nice(). If nice() returns -1, then errno can be checked to see if an error occurred or if the value is a legitimate nice value.

Related information