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 by pid. they're not here, they ghosted πŸ’€
  • EINVAL β€” the value of the policy argument is invalid, or one or more of the parameters inside param is outside the valid range for the scheduling policy you specified. wrong input, wrong vibe.

πŸ¦‹ SEE ALSO πŸ¦‹

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