RLIMIT_RTPRIO Realtime Scheduling Priority Ceiling

 

36.3 — RLIMIT_RTPRIO
Realtime Scheduling Priority Ceiling (Linux 2.6.12+) | EmbeddedPathashala

What is RLIMIT_RTPRIO?

RLIMIT_RTPRIO is a Linux-specific limit (since kernel 2.6.12) that sets a ceiling on the realtime scheduling priority that a process may set for itself using sched_setscheduler() and sched_setparam(). Realtime priorities on Linux range from 1 (lowest) to 99 (highest).

Without RLIMIT_RTPRIO, unprivileged processes would need CAP_SYS_NICE to use realtime scheduling at all. With RLIMIT_RTPRIO set to a nonzero value, an unprivileged process can use realtime scheduling up to the ceiling set by the limit.

How It Works

The soft RLIMIT_RTPRIO value directly sets the ceiling: a process may set its realtime priority to any value from 0 up to rlim_cur. Attempting to set a priority above the ceiling fails with EPERM.

RLIMIT_RTPRIO soft Allowed RT priorities
0 None (cannot use RT scheduling without CAP_SYS_NICE)
10 SCHED_FIFO or SCHED_RR with priority 1–10
99 SCHED_FIFO or SCHED_RR with priority 1–99 (full range)

Code Example — RLIMIT_RTPRIO

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sched.h>
#include <sys/resource.h>

int main(void)
{
    struct rlimit rl;
    struct sched_param sp;

    getrlimit(RLIMIT_RTPRIO, &rl);
    printf("RLIMIT_RTPRIO: soft=%lld hard=%lld\n",
           (long long)rl.rlim_cur, (long long)rl.rlim_max);

    /* Allow RT priority up to 10 */
    rl.rlim_cur = 10;
    rl.rlim_max = 10;
    setrlimit(RLIMIT_RTPRIO, &rl);

    /* Try to set SCHED_FIFO priority 5 (within limit) */
    sp.sched_priority = 5;
    if (sched_setscheduler(0, SCHED_FIFO, &sp) == -1) {
        if (errno == EPERM)
            printf("EPERM: cannot set RT (need root or higher limit)\n");
        else
            perror("sched_setscheduler");
    } else {
        printf("RT priority 5 set (SCHED_FIFO)\n");
        /* Restore normal scheduling */
        sp.sched_priority = 0;
        sched_setscheduler(0, SCHED_OTHER, &sp);
    }

    /* Try priority 11 — above ceiling — should fail */
    sp.sched_priority = 11;
    if (sched_setscheduler(0, SCHED_FIFO, &sp) == -1 && errno == EPERM)
        printf("EPERM: priority 11 exceeds RLIMIT_RTPRIO ceiling of 10\n");

    return 0;
}
/* Compile: gcc -o rtprio_demo rtprio_demo.c
   Note: on most systems, this still requires root unless
   RLIMIT_RTPRIO was set by a privileged parent. */

Interview Questions

Q1. What does RLIMIT_RTPRIO control and since which kernel version?

It sets a ceiling on the realtime scheduling priority a process can set for itself via sched_setscheduler() and sched_setparam(). Available since Linux 2.6.12. Linux-specific, not in SUSv3.

Q2. If RLIMIT_RTPRIO is 0, can the process use realtime scheduling?

No. With rlim_cur=0, the process cannot use any realtime scheduling policy (SCHED_FIFO, SCHED_RR) without CAP_SYS_NICE. It can only use non-realtime policies (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH).

Leave a Reply

Your email address will not be published. Required fields are marked *