getpriority , setpriority 或 nice 子例程

用途

获取或设置 nice 值。

getprioritysetpriority: 标准 C 库 (libc.a)

nice: 标准 C 库 (libc.a)

Berkeley 兼容性库 (libbsd.a)

语法

#include <sys/resource.h>
int getpriority( Which Who)
int Which;
int Who;
int setpriority(WhichWho Priority)
int Which;
int Who;
int Priority;
#include <unistd.h>
int nice( Increment)
int Increment;

描述

进程,进程组或用户的 nice 值,如 哪些 参数是使用 getpriority 子例程获取并使用 setpriority 子例程设置的。

getpriority 子例程返回与任何指定进程相关的最高优先级 nice 值 (最低数字值)。 setpriority 子例程将所有指定进程的 nice 值设置为指定值。 如果指定的值小于 -20 ,则使用值 -20; 如果大于 20 ,则使用值 20。 只有具有 root 用户权限的进程才能降低 nice 值。

nice 子例程按 Increment 参数的值递增 nice 值。

注: 尼斯值仅用于调度策略 SCHED_OTHER,其中它们与最近 CPU 使用率的计算组合在一起以确定优先级值。

To provide upward compatibility with older programs, the 好人 interface, originally found in AT&T System V, is supported.

注: Process priorities in AT&T System V are defined in the range of 0 to 39, rather than -20 to 20 as in BSD, and the 好人 library routine is supported by both. 因此,AIX版本 3 支持两个版本的nice。 The default version behaves like the AT&T System V version, with the 增量 parameter treated as the modifier of a value in the range of 0 to 39 (0 corresponds to -20, 39 corresponds to 9, and priority 20 is not reachable with this interface).

如果需要 BSD 版本的行为,请使用 Berkeley 兼容性库 (libbsd.a) 进行编译。 Increment 参数被视为 -20 到 20 范围内的值的修饰符。

参数

描述
which 指定 PRIO_PROCESSPRIO_PGRPPRIO_USER中的一个。
相对于 哪些 参数 (分别为进程标识,进程组标识和用户标识) 进行解释。 who 参数的零值表示当前进程,进程组或用户。
Priority 指定范围在 -20 到 20 之间的值。 负的 nice 值会导致更有利的调度。
增量 指定要添加到当前进程 nice 值的值。 可以指定负值,尽管超过上限或下限的值会被截断。

返回值

成功完成时, getpriority 子例程返回范围在 -20 到 20 之间的整数。 返回值为-1也可能表示出错,在这种情况下,将设置errno全局变量。

成功完成时, setpriority 子例程返回 0。 否则,将返回-1并设置全局变量errno以指示错误。

成功完成时, nice 子例程将返回新的 nice 值减去 {NZERO}。 否则,返回值为-1,并设置errno全局变量来指示错误。

注:也可以返回-1的值。 在这种情况下,调用进程还应该检查 errno 全局变量。

错误代码

如果下列其中一项为 true ,那么 getprioritysetpriority 子例程将失败:

描述
ESRCH 未使用指定的 WhoWho 参数值找到任何进程。
EINVAL 无法识别 哪个 参数。

除了上面指示的错误外,如果下列其中一个为 true ,那么 setpriority 子例程将失败:

描述
EPERM 找到了进程,但执行 setpriority 子例程的进程的调用者的有效用户标识和实际用户标识都没有 root 用户权限。
EACCES 调用 setpriority 会将进程的优先级更改为低于其当前值的值,并且执行调用的进程的有效用户标识没有 root 用户权限。

如果以下情况成立,那么 nice 子例程不成功:

描述
EPERM Increment 参数为负数,调用进程没有相应的特权。