GETPRIORITY(2)
π GETPRIORITY π
okay bestie, we're in the System Calls Manual section (that's section 2, iykyk) and today's main character is the getpriority / setpriority duo. they're siblings. they come as a pair. you literally cannot talk about one without the other, it's giving twin energy fr fr β¨
β¨ NAME β¨
getpriority, setpriority β get/set program scheduling priority π
π LIBRARY π
Standard C Library (that's libc, link it with -lc) π«Ά
π¦ SYNOPSIS π¦
okay so first you gotta invite the headers to the party:
#include <sys/time.h>
#include <sys/resource.h>
and then here's the actual function glow-ups:
int
getpriority(int which, int who);
int
setpriority(int which, int who, int prio);
π DESCRIPTION π
So here's the tea. Every process, process group, or user has a scheduling priority β basically how much the scheduler likes them and how fast they get picked. You read that priority with getpriority() and you set it with setpriority(). And you tell it whose priority you mean using which and who together, like a lil buddy system π
The which argument is exactly one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER β no substitutions, no vibes-based guessing, periodt.
And who gets interpreted relative to whatever which you picked:
- a process identifier if
whichisPRIO_PROCESS - a process group identifier if
whichisPRIO_PGRP - a user ID if
whichisPRIO_USER
And if you pass a zero value for who? That's the "me, myself, and I" move β it denotes the current process, process group, or user. slay π
The prio argument is a value in the range -20 to 20. The default priority is 0. And listen closely because this is the part that trips everyone up: lower priorities cause more favorable scheduling. so -20 is like, VIP velvet rope treatment, and 20 is standing outside in the rain π
getpriority() returns the highest priority β which means the lowest numerical value β enjoyed by any of the specified processes. yes that's confusing, yes it's on purpose, we live here now π
setpriority() sets the priorities of all of the specified processes to the specified value.
β οΈ Only the super-user may lower priorities. like, actually lower them. don't try it, bestie, it will not go how you want.
π₯ RETURN VALUES π₯
Here's the gotcha and I need you to internalize it: getpriority() can legitimately return the value -1. Not as an error. As a real answer. So you literally cannot tell "error" from "real value" just by looking at the return π
So the move is: clear the external variable errno before the call, then check it afterward to figure out if that -1 is an error or a legitimate value. That's it. That's the ritual. do it every single time, no cap π
The setpriority() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. nice and simple, we love a straightforward queen π
π ERRORS π
The getpriority() and setpriority() system calls will fail if:
[ESRCH]β No process was located using thewhichandwhovalues specified. you asked for someone and nobody showed up. ghosted fr π«₯[EINVAL]β Thewhichargument was not one ofPRIO_PROCESS,PRIO_PGRP, orPRIO_USER. you made up a category. that's not a thing. π
In addition to the errors indicated above, setpriority() will fail if:
[EPERM]β A process was located, but neither its effective nor real user ID matched the effective user ID of the caller. so the process exists, you just don't get to touch it. not yours, not your business π[EACCES]β A non super-user attempted to lower a process priority. you tried to jump the velvet rope without the super-user wristband. absolutely not. π«
π SEE ALSO π
go read those next, they're all part of the same little priority cinematic universe π¬
π HISTORY π
The getpriority() system call appeared in 4.2BSD. it's old. it's iconic. it has been here longer than most of us and it's still booked and busy β¨
okay that's the whole page bestie!! this one's from Debian, dated June 4, 1993 β yeah, 1993, this manual page is older than the internet's entire personality and it still ate π go forth, clear that errno, and schedule responsibly. mwah π«Άπ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms