SCHED_SETSCHEDULER(2)
π SCHED_SETSCHEDULER(2) π
okay bestie, this is a page from the System Calls Manual and it's literally about telling processes how to share the CPU. it's giving β¨scheduling royaltyβ¨. no cap, let's get into it.
πΈ NAME πΈ
sched_setscheduler, sched_getscheduler β set/get scheduling policy and scheduler parameters
π LIBRARY π
Standard C Library (libc, -lc) β yes babes, the classic one π«Ά
β¨ SYNOPSIS β¨
#include <sched.h>
int
sched_setscheduler(pid_t pid, int policy,
const struct sched_param *param);
int
sched_getscheduler(pid_t pid);
π DESCRIPTION π
so sched_setscheduler() is the system call that sets the scheduling policy AND the scheduling parameters of the process you point at with pid. you're setting the policy to whatever policy says, and the parameters to whatever is in the sched_param structure that param points to. π§
the sched_priority member inside that param structure? it has to be an integer that sits within the inclusive priority range for whatever scheduling policy you passed as policy. no wandering outside the range, bestie, that's not the vibe.
in this implementation, if pid is negative, the system call just fails. periodt. π
if a process matching pid actually exists, and the calling process has permission, then boom β the scheduling policy and scheduling parameters get set for the process whose process ID equals pid. slay.
if pid is zero? then the scheduling policy and parameters are set for the calling process. it's main character energy, but for yourself. π
in this implementation, the rules about when a process can affect another process's scheduling parameters are laid out in IEEE Std 1003.1b-1993 (βPOSIX.1β) and it's a write-style operation.
the scheduling policies live in <sched.h>:
SCHED_FIFOβ first-in-first-out fixed priority scheduling, with no round robin scheduling. it's giving strict queue energy.SCHED_OTHERβ the standard time sharing scheduler. the chill default one πSCHED_RRβ round-robin scheduling across processes that share the same priority. everyone gets a turn, iconic.
the sched_param structure is defined in <sched.h>:
struct sched_param {
int sched_priority; /* scheduling priority */
};
now, sched_getscheduler() is the read-only girlie of the pair β it returns the scheduling policy of the process specified by pid. π
if a process specified by pid exists and the calling process has permission, then the scheduling parameters for the process whose process ID equals pid get returned.
in this implementation, the rules about when a process can obtain another process's scheduling parameters are detailed in IEEE Std 1003.1b-1993 (βPOSIX.1β) as a read-style operation.
if pid is zero, the scheduling parameters for the calling process get returned. and heads up β in this implementation, sched_getscheduler will fail if pid is negative. nope, no negative pid shenanigans. π
π RETURN VALUES π
if everything goes great? you get 0. it's the little win we love to see. π₯³
if it does not? you get -1, and the global variable errno gets set to tell you exactly what went wrong.
π₯ ERRORS π₯
on failure, errno gets set to the matching value:
ENOSYSβ the system is not configured to support this functionality. it literally cannot, bestie πEPERMβ the requesting process doesn't have permission, as detailed in IEEE Std 1003.1b-1993 (βPOSIX.1β). permission denied, respectfully.ESRCHβ no process can be found corresponding to the one specified bypid. they're not here, they ghosted πEINVALβ the value of thepolicyargument is invalid, or one or more of the parameters insideparamis outside the valid range for the scheduling policy you specified. wrong input, wrong vibe.
π¦ SEE ALSO π¦
sched_get_priority_max(2)sched_get_priority_min(2)sched_getparam(2)sched_rr_get_interval(2)sched_setparam(2)sched_yield(2)
go read them, they're all part of the same little scheduling friend group π«Ά
π STANDARDS π
the sched_setscheduler() and sched_getscheduler() system calls conform to IEEE Std 1003.1b-1993 (βPOSIX.1β). certified, standardized, iconic. β
and that's the whole page, bestie!! this one's from Debian, dated March 12, 1998 β yes, a vintage queen, older than most of us and still running the scheduling game πβ¨πΈ
π¦ 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) 1998 HD Associates, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms