GETPRIORITY(3POSIX)

πŸ’– GETPRIORITY(3POSIX) β€” bestie edition πŸ’–

okay so this one is from the POSIX Programmer's Manual, and it's the glow-up guide to getpriority and setpriority β€” aka how you check and change a process's nice value. πŸŽ€

🌸 PROLOG

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so bestie, go consult the corresponding Linux manual page for details of Linux behavior), or honestly? the interface may not 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() shall obtain the nice value of a process, a process group, or a user. And setpriority() shall set the nice value of a process, process group, or user to value+{NZERO}. That's the whole vibe.

Which target processes we're talking about? That's decided by the which and who arguments, and the which argument may be one of these three icons:

  • PRIO_PROCESS β€” meaning who is interpreted as a process ID
  • PRIO_PGRP β€” meaning who is interpreted as a process group ID
  • PRIO_USER β€” meaning who is interpreted as an effective user ID

And a 0 value for the who argument specifies the current process, process group, or user. Cute, right? ✨

The nice value set with setpriority() shall be applied to the process. And if the process is multi-threaded, the nice value shall affect all system scope threads in the process. no cap, all of them.

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 of the specified processes to value+{NZERO}. πŸ’«

The default nice value is {NZERO}; lower nice values shall cause more favorable scheduling. (yes bestie, lower = more main character energy in the scheduler.) While the range of valid nice values is [0,{NZERO}*2-1], implementations may enforce more restrictive limits. 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. periodt.

⚠️ Only a process with appropriate privileges can lower its nice value. So like… know your limits, bestie.

Any processes or threads using SCHED_FIFO or SCHED_RR shall be unaffected by a call to setpriority(). This is not considered an error. And a process which subsequently reverts to SCHED_OTHER need not have its priority affected by such a setpriority() call. so don't come for it, it's just how it is. πŸ¦‹

The effect of changing the nice value may vary depending on the process-scheduling algorithm in effect.

😭 CRITICAL VIBE CHECK: Since getpriority() can return the value -1 upon successful completion, it is necessary to set errno to 0 prior to a call to getpriority(). If getpriority() returns the value -1, then errno can be checked to see if an error occurred or if the value is a legitimate nice value. Do NOT skip this step. This is the plot twist of the whole page.

🫢 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 the which and who argument values specified. (ghosted fr fr πŸ‘»)
  • EINVAL β€” The value of the which argument was not recognized, or the value of the who argument 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. (i.e. the vibes-only bonus content ✨)


πŸ“ 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}). The nice value is 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 girl. πŸ’œ


πŸ‘‘ 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>


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 .


okay bestie that's the whole page, you ate it, every flag and error code accounted for πŸ’– this came to you courtesy of IEEE/The Open Group, 2017 β€” so yeah, that's the whole GETPRIORITY(3POSIX) moment. go forth and set your nice values responsibly, mwah 🫢🌸