fanotify_mark.2: ERRORS: add missing EBADF error for invalid 'dirfd'
[man-pages.git] / man2 / getpriority.2
blobd6744989d800412ea666852495d0fb2d0395d056
1 .\" Copyright (c) 1980, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\"    must display the following acknowledgement:
15 .\"     This product includes software developed by the University of
16 .\"     California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\"    may be used to endorse or promote products derived from this software
19 .\"    without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\"     @(#)getpriority.2       6.9 (Berkeley) 3/10/91
35 .\"
36 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
37 .\" Modified 1996-07-01 by Andries Brouwer <aeb@cwi.nl>
38 .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
39 .\" Modified 2001-10-21 by Michael Kerrisk <mtk.manpages@gmail.com>
40 .\"    Corrected statement under EPERM to clarify privileges required
41 .\" Modified 2002-06-21 by Michael Kerrisk <mtk.manpages@gmail.com>
42 .\"    Clarified meaning of 0 value for 'who' argument
43 .\" Modified 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
44 .\"
45 .TH GETPRIORITY 2 2021-03-22 "Linux" "Linux Programmer's Manual"
46 .SH NAME
47 getpriority, setpriority \- get/set program scheduling priority
48 .SH SYNOPSIS
49 .nf
50 .B #include <sys/resource.h>
51 .PP
52 .BI "int getpriority(int " which ", id_t " who );
53 .BI "int setpriority(int " which ", id_t " who ", int " prio );
54 .fi
55 .SH DESCRIPTION
56 The scheduling priority of the process, process group, or user, as
57 indicated by
58 .I which
59 and
60 .I who
61 is obtained with the
62 .BR getpriority ()
63 call and set with the
64 .BR setpriority ()
65 call.
66 The process attribute dealt with by these system calls is
67 the same attribute (also known as the "nice" value) that is dealt with by
68 .BR nice (2).
69 .PP
70 The value
71 .I which
72 is one of
73 .BR PRIO_PROCESS ,
74 .BR PRIO_PGRP ,
76 .BR PRIO_USER ,
77 and
78 .I who
79 is interpreted relative to
80 .I which
81 (a process identifier for
82 .BR PRIO_PROCESS ,
83 process group
84 identifier for
85 .BR PRIO_PGRP ,
86 and a user ID for
87 .BR PRIO_USER ).
88 A zero value for
89 .I who
90 denotes (respectively) the calling process, the process group of the
91 calling process, or the real user ID of the calling process.
92 .PP
93 The
94 .I prio
95 argument is a value in the range \-20 to 19 (but see NOTES below),
96 with \-20 being the highest priority and 19 being the lowest priority.
97 Attempts to set a priority outside this range
98 are silently clamped to the range.
99 The default priority is 0;
100 lower values give a process a higher scheduling priority.
103 .BR getpriority ()
104 call returns the highest priority (lowest numerical value)
105 enjoyed by any of the specified processes.
107 .BR setpriority ()
108 call sets the priorities of all of the specified processes
109 to the specified value.
111 Traditionally, only a privileged process could lower the nice value
112 (i.e., set a higher priority).
113 However, since Linux 2.6.12, an unprivileged process can decrease
114 the nice value of a target process that has a suitable
115 .BR RLIMIT_NICE
116 soft limit; see
117 .BR getrlimit (2)
118 for details.
119 .SH RETURN VALUE
120 On success,
121 .BR getpriority ()
122 returns the calling thread's nice value, which may be a negative number.
123 On error, it returns \-1 and sets
124 .I errno
125 to indicate the error.
127 Since a successful call to
128 .BR getpriority ()
129 can legitimately return the value \-1, it is necessary
130 to clear
131 .I errno
132 prior to the
133 call, then check
134 .I errno
135 afterward to determine
136 if \-1 is an error or a legitimate value.
138 .BR setpriority ()
139 returns 0 on success.
140 On failure, it returns \-1 and sets
141 .I errno
142 to indicate the error.
143 .SH ERRORS
145 .B EINVAL
146 .I which
147 was not one of
148 .BR PRIO_PROCESS ,
149 .BR PRIO_PGRP ,
151 .BR PRIO_USER .
153 .B ESRCH
154 No process was located using the
155 .I which
157 .I who
158 values specified.
160 In addition to the errors indicated above,
161 .BR setpriority ()
162 may fail if:
164 .B EACCES
165 The caller attempted to set a lower nice value
166 (i.e., a higher process priority), but did not
167 have the required privilege (on Linux: did not have the
168 .B CAP_SYS_NICE
169 capability).
171 .B EPERM
172 A process was located, but its effective user ID did not match
173 either the effective or the real user ID of the caller,
174 and was not privileged (on Linux: did not have the
175 .B CAP_SYS_NICE
176 capability).
177 But see NOTES below.
178 .SH CONFORMING TO
179 POSIX.1-2001, POSIX.1-2008,
180 SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD).
181 .SH NOTES
182 For further details on the nice value, see
183 .BR sched (7).
185 .IR Note :
186 the addition of the "autogroup" feature in Linux 2.6.38 means that
187 the nice value no longer has its traditional effect in many circumstances.
188 For details, see
189 .BR sched (7).
191 A child created by
192 .BR fork (2)
193 inherits its parent's nice value.
194 The nice value is preserved across
195 .BR execve (2).
197 The details on the condition for
198 .B EPERM
199 depend on the system.
200 The above description is what POSIX.1-2001 says, and seems to be followed on
201 all System\ V-like systems.
202 Linux kernels before 2.6.12 required the real or
203 effective user ID of the caller to match
204 the real user of the process \fIwho\fP (instead of its effective user ID).
205 Linux 2.6.12 and later require
206 the effective user ID of the caller to match
207 the real or effective user ID of the process \fIwho\fP.
208 All BSD-like systems (SunOS 4.1.3, Ultrix 4.2,
209 4.3BSD, FreeBSD 4.3, OpenBSD-2.5, ...) behave in the same
210 manner as Linux 2.6.12 and later.
212 .SS C library/kernel differences
213 Within the kernel, nice values are actually represented
214 using the range 40..1
215 (since negative numbers are error codes) and these are the values
216 employed by the
217 .BR setpriority ()
219 .BR getpriority ()
220 system calls.
221 The glibc wrapper functions for these system calls handle the
222 translations between the user-land and kernel representations
223 of the nice value according to the formula
224 .IR "unice\ =\ 20\ \-\ knice" .
225 (Thus, the kernel's 40..1 range corresponds to the
226 range \-20..19 as seen by user space.)
227 .SH BUGS
228 According to POSIX, the nice value is a per-process setting.
229 However, under the current Linux/NPTL implementation of POSIX threads,
230 the nice value is a per-thread attribute:
231 different threads in the same process can have different nice values.
232 Portable applications should avoid relying on the Linux behavior,
233 which may be made standards conformant in the future.
234 .SH SEE ALSO
235 .BR nice (1),
236 .BR renice (1),
237 .BR fork (2),
238 .BR capabilities (7),
239 .BR sched (7)
241 .I Documentation/scheduler/sched\-nice\-design.txt
242 in the Linux kernel source tree (since Linux 2.6.23)