1GETPRIORITY(2) Linux Programmer's Manual GETPRIORITY(2)
2
3
4
6 getpriority, setpriority - get/set program scheduling priority
7
9 #include <sys/resource.h>
10
11 int getpriority(int which, id_t who);
12 int setpriority(int which, id_t who, int prio);
13
15 The scheduling priority of the process, process group, or user, as in‐
16 dicated by which and who is obtained with the getpriority() call and
17 set with the setpriority() call. The process attribute dealt with by
18 these system calls is the same attribute (also known as the "nice"
19 value) that is dealt with by nice(2).
20
21 The value which is one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER, and
22 who is interpreted relative to which (a process identifier for
23 PRIO_PROCESS, process group identifier for PRIO_PGRP, and a user ID for
24 PRIO_USER). A zero value for who denotes (respectively) the calling
25 process, the process group of the calling process, or the real user ID
26 of the calling process.
27
28 The prio argument is a value in the range -20 to 19 (but see NOTES be‐
29 low), with -20 being the highest priority and 19 being the lowest pri‐
30 ority. Attempts to set a priority outside this range are silently
31 clamped to the range. The default priority is 0; lower values give a
32 process a higher scheduling priority.
33
34 The getpriority() call returns the highest priority (lowest numerical
35 value) enjoyed by any of the specified processes. The setpriority()
36 call sets the priorities of all of the specified processes to the spec‐
37 ified value.
38
39 Traditionally, only a privileged process could lower the nice value
40 (i.e., set a higher priority). However, since Linux 2.6.12, an unpriv‐
41 ileged process can decrease the nice value of a target process that has
42 a suitable RLIMIT_NICE soft limit; see getrlimit(2) for details.
43
45 On success, getpriority() returns the calling thread's nice value,
46 which may be a negative number. On error, it returns -1 and sets errno
47 to indicate the error.
48
49 Since a successful call to getpriority() can legitimately return the
50 value -1, it is necessary to clear errno prior to the call, then check
51 errno afterward to determine if -1 is an error or a legitimate value.
52
53 setpriority() returns 0 on success. On failure, it returns -1 and sets
54 errno to indicate the error.
55
57 EACCES The caller attempted to set a lower nice value (i.e., a higher
58 process priority), but did not have the required privilege (on
59 Linux: did not have the CAP_SYS_NICE capability).
60
61 EINVAL which was not one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER.
62
63 EPERM A process was located, but its effective user ID did not match
64 either the effective or the real user ID of the caller, and was
65 not privileged (on Linux: did not have the CAP_SYS_NICE capabil‐
66 ity). But see NOTES below.
67
68 ESRCH No process was located using the which and who values specified.
69
71 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (these interfaces first ap‐
72 peared in 4.2BSD).
73
75 For further details on the nice value, see sched(7).
76
77 Note: the addition of the "autogroup" feature in Linux 2.6.38 means
78 that the nice value no longer has its traditional effect in many cir‐
79 cumstances. For details, see sched(7).
80
81 A child created by fork(2) inherits its parent's nice value. The nice
82 value is preserved across execve(2).
83
84 The details on the condition for EPERM depend on the system. The above
85 description is what POSIX.1-2001 says, and seems to be followed on all
86 System V-like systems. Linux kernels before 2.6.12 required the real
87 or effective user ID of the caller to match the real user of the
88 process who (instead of its effective user ID). Linux 2.6.12 and later
89 require the effective user ID of the caller to match the real or effec‐
90 tive user ID of the process who. All BSD-like systems (SunOS 4.1.3,
91 Ultrix 4.2, 4.3BSD, FreeBSD 4.3, OpenBSD-2.5, ...) behave in the same
92 manner as Linux 2.6.12 and later.
93
94 C library/kernel differences
95 Within the kernel, nice values are actually represented using the range
96 40..1 (since negative numbers are error codes) and these are the values
97 employed by the setpriority() and getpriority() system calls. The
98 glibc wrapper functions for these system calls handle the translations
99 between the user-land and kernel representations of the nice value ac‐
100 cording to the formula unice = 20 - knice. (Thus, the kernel's 40..1
101 range corresponds to the range -20..19 as seen by user space.)
102
104 According to POSIX, the nice value is a per-process setting. However,
105 under the current Linux/NPTL implementation of POSIX threads, the nice
106 value is a per-thread attribute: different threads in the same process
107 can have different nice values. Portable applications should avoid re‐
108 lying on the Linux behavior, which may be made standards conformant in
109 the future.
110
112 nice(1), renice(1), fork(2), capabilities(7), sched(7)
113
114 Documentation/scheduler/sched-nice-design.txt in the Linux kernel
115 source tree (since Linux 2.6.23)
116
118 This page is part of release 5.13 of the Linux man-pages project. A
119 description of the project, information about reporting bugs, and the
120 latest version of this page, can be found at
121 https://www.kernel.org/doc/man-pages/.
122
123
124
125Linux 2021-08-27 GETPRIORITY(2)