GETPRIORITY(3P)
π PROLOG π
okay bestie, real talk before we even start: this page is part of the POSIX Programmer's Manual. That means the Linux version of this interface might act differently β so if you're on Linux, go check the matching Linux manual page for the tea. Or, plot twist, the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
getpriority, setpriority β get and set the nice value π
πΈ SYNOPSIS πΈ
#include <sys/resource.h>
int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int value);
π DESCRIPTION π
So getpriority() grabs the nice value of a process, a process group, or a user. And setpriority()? It sets the nice value of a process, process group, or user to value+{NZERO}. Slay.
Which target you're talking to is decided by the which and who arguments. which can be one of these three:
PRIO_PROCESSPRIO_PGRPPRIO_USER
β¦and that tells everyone whether who should be read as a process ID, a process group ID, or an effective user ID, respectively. And if you pass 0 for who? That means the current process, process group, or user. easy. β¨
The nice value you set with setpriority() shall be applied to the process. And if the process is multi-threaded β heads up β the nice value shall affect all system scope threads in the process. all of them. no exceptions. π
If more than one process is specified, getpriority() shall return value {NZERO} less than the lowest nice value pertaining to any of the specified processes, and setpriority() shall set the nice values of all the specified processes to value+{NZERO}.
The default nice value is {NZERO}. Lower nice values shall cause more favorable scheduling (yes, lower = more popular, it's giving main character energy). While the range of valid nice values is [0,{NZERO}*2-1], implementations may enforce more restrictive limits. And if value+{NZERO} is less than the system's lowest supported nice value, setpriority() shall set the nice value to the lowest supported value; if value+{NZERO} is greater than the system's highest supported nice value, setpriority() shall set the nice value to the highest supported value. it clamps, bestie. π
Only a process with appropriate privileges can lower its nice value. sorry. π
Any processes or threads using SCHED_FIFO or SCHED_RR shall be unaffected by a call to setpriority(). And no, that is not considered an error. Also, a process which subsequently reverts to SCHED_OTHER need not have its priority affected by such a setpriority() call.
The effect of changing the nice value may vary depending on the process-scheduling algorithm in effect.
β οΈ Okay this one is IMPORTANT, do not skip: sincegetpriority()can return the value-1upon successful completion, it is necessary to seterrnoto0prior to a call togetpriority(). Then ifgetpriority()returns the value-1, you can checkerrnoto see if an error occurred or if the value is a legitimate nice value. this trips everyone up, fr fr π
π«Ά RETURN VALUE π«Ά
Upon successful completion, getpriority() shall return an integer in the range -{NZERO} to {NZERO}-1. Otherwise, -1 shall be returned and errno set to indicate the error.
Upon successful completion, setpriority() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
π¨ ERRORS π¨
The getpriority() and setpriority() functions shall fail if:
ESRCHβ No process could be located using thewhichandwhoargument values specified.EINVALβ The value of thewhichargument was not recognized, or the value of thewhoargument is not a valid process ID, process group ID, or user ID.
In addition, setpriority() may fail if:
EPERMβ A process was located, but neither the real nor effective user ID of the executing process match the effective user ID of the process whose nice value is being changed.EACCESβ A request was made to change the nice value to a lower numeric value and the current process does not have appropriate privileges.
The following sections are informative. π
π EXAMPLES π
Using getpriority()
The following example returns the current scheduling priority for the process ID returned by the call to getpid().
#include <sys/resource.h>
...
int which = PRIO_PROCESS;
id_t pid;
int ret;
pid = getpid();
ret = getpriority(which, pid);
Using setpriority()
The following example sets the priority for the current process ID to -20.
#include <sys/resource.h>
...
int which = PRIO_PROCESS;
id_t pid;
int priority = -20;
int ret;
pid = getpid();
ret = setpriority(which, pid, priority);
π¦ APPLICATION USAGE π¦
The getpriority() and setpriority() functions work with an offset nice value (nice value -{NZERO}). So the nice value itself lives in the range [0,2*{NZERO} -1], while the return value for getpriority() and the third parameter for setpriority() are in the range [-{NZERO},{NZERO} -1]. two different coordinate systems, same vibe. πΈ
β RATIONALE β
None.
π§ FUTURE DIRECTIONS π§
None.
π SEE ALSO π
nice()sched_get_priority_max()sched_setscheduler()
The Base Definitions volume of POSIX.1β2017, <sys_resource.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π this one's brought to you by IEEE/The Open Group, 2017 β getpriority(3P), POSIX Programmer's Manual. go forth and be nice (or not, depending on your privileges) β¨ππ¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms