man-pages/man2/getpriority.2.html
2021-03-31 01:06:50 +01:00

355 lines
8.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETPRIORITY</TITLE>
</HEAD><BODY>
<H1>GETPRIORITY</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getpriority, setpriority - get/set program scheduling priority
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/sys/resource.h">sys/resource.h</A>&gt;</B>
<P>
<B>int getpriority(int </B><I>which</I><B>, id_t </B><I>who</I><B>);</B>
<BR>
<B>int setpriority(int </B><I>which</I><B>, id_t </B><I>who</I><B>, int </B><I>prio</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The scheduling priority of the process, process group, or user, as
indicated by
<I>which</I>
and
<I>who</I>
is obtained with the
<B>getpriority</B>()
call and set with the
<B>setpriority</B>()
call.
The process attribute dealt with by these system calls is
the same attribute (also known as the &quot;nice&quot; value) that is dealt with by
<B><A HREF="/cgi-bin/man/man2html?2+nice">nice</A></B>(2).
<P>
The value
<I>which</I>
is one of
<B>PRIO_PROCESS</B>,
<B>PRIO_PGRP</B>,
or
<B>PRIO_USER</B>,
and
<I>who</I>
is interpreted relative to
<I>which</I>
(a process identifier for
<B>PRIO_PROCESS</B>,
process group
identifier for
<B>PRIO_PGRP</B>,
and a user ID for
<B>PRIO_USER</B>).
A zero value for
<I>who</I>
denotes (respectively) the calling process, the process group of the
calling process, or the real user ID of the calling process.
<P>
The
<I>prio</I>
argument is a value in the range -20 to 19 (but see NOTES below).
with -20 being the highest priority and 19 being the lowest priority.
Attempts to set a priority outside this range
are silently clamped to the range.
The default priority is 0;
lower values give a process a higher scheduling priority.
<P>
The
<B>getpriority</B>()
call returns the highest priority (lowest numerical value)
enjoyed by any of the specified processes.
The
<B>setpriority</B>()
call sets the priorities of all of the specified processes
to the specified value.
<P>
Traditionally, only a privileged process could lower the nice value
(i.e., set a higher priority).
However, since Linux 2.6.12, an unprivileged process can decrease
the nice value of a target process that has a suitable
<B>RLIMIT_NICE</B>
soft limit; see
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2)
for details.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>getpriority</B>()
returns the calling thread's nice value, which may be a negative number.
On error, it returns -1 and sets
<I>errno</I>
to indicate the cause of the error.
Since a successful call to
<B>getpriority</B>()
can legitimately return the value -1, it is necessary
to clear the external variable
<I>errno</I>
prior to the
call, then check it afterward to determine
if -1 is an error or a legitimate value.
<P>
<B>setpriority</B>()
returns 0 on success.
On error, it returns -1 and sets
<I>errno</I>
to indicate the cause of the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EINVAL</B>
<DD>
<I>which</I>
was not one of
<B>PRIO_PROCESS</B>,
<B>PRIO_PGRP</B>,
or
<B>PRIO_USER</B>.
<DT id="2"><B>ESRCH</B>
<DD>
No process was located using the
<I>which</I>
and
<I>who</I>
values specified.
</DL>
<P>
In addition to the errors indicated above,
<B>setpriority</B>()
may fail if:
<DL COMPACT>
<DT id="3"><B>EACCES</B>
<DD>
The caller attempted to set a lower nice value
(i.e., a higher process priority), but did not
have the required privilege (on Linux: did not have the
<B>CAP_SYS_NICE</B>
capability).
<DT id="4"><B>EPERM</B>
<DD>
A process was located, but its effective user ID did not match
either the effective or the real user ID of the caller,
and was not privileged (on Linux: did not have the
<B>CAP_SYS_NICE</B>
capability).
But see NOTES below.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008,
SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD).
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
For further details on the nice value, see
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7).
<P>
<I>Note</I>:
the addition of the &quot;autogroup&quot; feature in Linux 2.6.38 means that
the nice value no longer has its traditional effect in many circumstances.
For details, see
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7).
<P>
A child created by
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
inherits its parent's nice value.
The nice value is preserved across
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<P>
The details on the condition for
<B>EPERM</B>
depend on the system.
The above description is what POSIX.1-2001 says, and seems to be followed on
all System&nbsp;V-like systems.
Linux kernels before 2.6.12 required the real or
effective user ID of the caller to match
the real user of the process <I>who</I> (instead of its effective user ID).
Linux 2.6.12 and later require
the effective user ID of the caller to match
the real or effective user ID of the process <I>who</I>.
All BSD-like systems (SunOS 4.1.3, Ultrix 4.2,
4.3BSD, FreeBSD 4.3, OpenBSD-2.5, ...) behave in the same
manner as Linux 2.6.12 and later.
<P>
Including
<I>&lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</I>
is not required these days, but increases portability.
(Indeed,
<I>&lt;<A HREF="file:///usr/include/sys/resource.h">sys/resource.h</A>&gt;</I>
defines the
<I>rusage</I>
structure with fields of type
<I>struct timeval</I>
defined in
<I>&lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</I>.)
<A NAME="lbAI">&nbsp;</A>
<H3>C library/kernel differences</H3>
Within the kernel, nice values are actually represented
using the range 40..1
(since negative numbers are error codes) and these are the values
employed by the
<B>setpriority</B>()
and
<B>getpriority</B>()
system calls.
The glibc wrapper functions for these system calls handle the
translations between the user-land and kernel representations
of the nice value according to the formula
<I>unice&nbsp;=&nbsp;20&nbsp;-&nbsp;knice</I>.
(Thus, the kernel's 40..1 range corresponds to the
range -20..19 as seen by user space.)
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
According to POSIX, the nice value is a per-process setting.
However, under the current Linux/NPTL implementation of POSIX threads,
the nice value is a per-thread attribute:
different threads in the same process can have different nice values.
Portable applications should avoid relying on the Linux behavior,
which may be made standards conformant in the future.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+nice">nice</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+renice">renice</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7)
<P>
<I>Documentation/scheduler/sched-nice-design.txt</I>
in the Linux kernel source tree (since Linux 2.6.23)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="11"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="12"><A HREF="#lbAI">C library/kernel differences</A><DD>
</DL>
<DT id="13"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="14"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>