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