Go to the previous, next section.

Process Priority

When several processes try to run, their respective priorities determine what share of the CPU each process gets. This section describes how you can read and set the priority of a process. All these functions and macros are declared in `sys/resource.h'.

The range of valid priority values depends on the operating system, but typically it runs from -20 to 20. A lower priority value means the process runs more often. These constants describe the range of priority values:

PRIO_MIN
The smallest valid priority value.

PRIO_MAX
The smallest valid priority value.

Function: int getpriority (int class, int id)

Read the priority of a class of processes; class and id specify which ones (see below).

The return value is the priority value on success, and -1 on failure. The following errno error condition are possible for this function:

ESRCH
The combination of class and id does not match any existing process.

EINVAL
The value of class is not valid.

When the return value is -1, it could indicate failure, or it could be the priority value. The only way to make certain is to set errno = 0 before calling getpriority, then use errno != 0 afterward as the criterion for failure.

Function: int setpriority (int class, int id, int priority)

Read the priority of a class of processes; class and id specify which ones (see below).

The return value is 0 on success and -1 on failure. The following errno error condition are defined for this function:

ESRCH
The combination of class and id does not match any existing process.

EINVAL
The value of class is not valid.

EPERM
You tried to set the priority of some other user's process, and you don't have privileges for that.

EACCES
You tried to lower the priority of a process, and you don't have privileges for that.

The arguments class and id together specify a set of processes you are interested in. These are the possible values for class:

PRIO_PROCESS
Read or set the priority of one process. The argument id is a process ID.

PRIO_PGRP
Read or set the priority of one process group. The argument id is a process group ID.

PRIO_USER
Read or set the priority of one user's processes. The argument id is a user ID.

If the argument id is 0, it stands for the current process, current process group, or the current user, according to class.

Function: int nice (int increment)

Increment the priority of the current process by increment. The return value is not meaningful.

Here is an equivalent definition for nice:

int
nice (int increment)
{
  int old = getpriority (PRIO_PROCESS, 0);
  setpriority (PRIO_PROCESS, 0, old + increment);
}

Go to the previous, next section.