RTPRIO(2)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’… rtprio(2) β€” the realtime / idle priority glow-up

okay bestie so you've found yourself in the System Calls Manual, section 2, and the star of the show is rtprio(2). It lives in the Standard C Library (libc, -lc) 🫢


✨ NAME ✨

rtprio, rtprio_thread β€” examine or modify realtime or idle priority πŸ’–


πŸŽ€ LIBRARY πŸŽ€

Standard C Library (libc, -lc)


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/types.h>
#include <sys/rtprio.h>

int
rtprio(int function, pid_t pid, struct rtprio *rtp);

int
rtprio_thread(int function, lwpid_t lwpid, struct rtprio *rtp);

🌸 DESCRIPTION 🌸

So here's the vibe: rtprio() looks up or changes the realtime or idle priority of a process, or of the calling thread. And rtprio_thread() does the same little lookup/change dance but for a thread specifically. Iconic division of labor, no cap. πŸ‘‘

The function argument β€” what are we even doing here

The function argument specifies the operation to be performed:

  • RTP_LOOKUP β€” to lookup the current priority πŸ”
  • RTP_SET β€” to set the priority πŸ› οΈ

The pid argument (for rtprio())

For the rtprio() system call, the pid argument specifies the process to operate on, and 0 means πŸ‘‰ the calling thread.

When pid is non-zero, the system call reports the highest priority in the process, or sets all threads' priority in the process, all depending on the value of the function argument. It's giving "main character of the whole process" energy, honestly.

The lwpid argument (for rtprio_thread())

For the rtprio_thread() system call, the lwpid specifies the thread to operate on, and 0 means πŸ‘‰ the calling thread.

The *rtp argument β€” the struct moment

The *rtp argument is a pointer to a struct rtprio, which is what you use to specify the priority and the priority type. And this little guy has the following form:

struct rtprio {
        u_short type;
        u_short prio;
};

The value of the type field may be:

  • RTP_PRIO_REALTIME β€” for realtime priorities πŸ”₯
  • RTP_PRIO_NORMAL β€” for normal priorities 🌿
  • RTP_PRIO_IDLE β€” for idle priorities 😴

The priority specified by the prio field ranges between 0 and RTP_PRIO_MAX (usually 31). And heads up: 0 is the highest possible priority. Yes, backwards from what your brain wants, we know, it's a whole thing. πŸ’€

Inheritance tea β˜•

Realtime and idle priority is inherited through fork() and exec(). It's literally in the bloodline. 🧬

Scheduling behavior, AKA who preempts whom

  • A realtime thread can only be preempted by a thread of equal or higher priority, or by an interrupt ⚑
  • Idle priority threads will run only when no other real/normal priority thread is runnable πŸ¦‹
  • Higher real/idle priority threads preempt lower real/idle priority threads πŸ‘‘
  • Threads of equal real/idle priority are run round-robin πŸ”„

πŸ’œ RETURN VALUES πŸ’œ

The rtprio() and rtprio_thread() functions return the value 0 if successful β€” slay ✨. Otherwise the value -1 is returned and the global variable errno is set to indicate the error 😭.


πŸ”₯ ERRORS πŸ”₯

The rtprio() and rtprio_thread() system calls will fail if:

  • [EFAULT] β€” The rtp pointer passed to rtprio() or rtprio_thread() was invalid. πŸ™ˆ
  • [EINVAL] β€” The specified prio was out of range. πŸ“
  • [EPERM] β€” The calling thread is not allowed to set the priority. Only root is allowed to change the realtime or idle priority of any thread. Exceptional privileges can be granted through the mac_priority(4) policy and the realtime and idletime user groups. The sysctl(8) variable security.bsd.unprivileged_idprio is deprecated. If set to non-zero, it lets any user change the idle priority of threads they own. 🚨
  • [ESRCH] β€” The specified process or thread was not found or visible. πŸ‘»

🌈 SEE ALSO 🌈

nice(1), ps(1), rtprio(1), setpriority(2), nice(3), mac_priority(4), renice(8), p_cansee(9)


πŸ‘‘ AUTHORS πŸ‘‘

The original author was Henrik Vestergaard Draboel <hvd@terry.ping.dk> πŸ’–. This implementation in FreeBSD was substantially rewritten by David Greenman. And the rtprio_thread() system call was implemented by David Xu. Legends, all of them, fr fr. 🫢


okay that's the whole page bestie, go forth and prioritize responsibly πŸ’…βœ¨

Source: Debian Β· December 8, 2021 Β· RTPRIO(2) β€” mwah, that's the sign-off, ily, bye πŸŽ€

πŸ“¦ where this page comes from