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