RLIMIT_NICE Nice Value Ceiling: Ceiling Formula, nice(), sched_setscheduler()

 

36.3 — RLIMIT_NICE
Nice Value Ceiling: Ceiling Formula, nice(), sched_setscheduler() | EmbeddedPathashala

What is RLIMIT_NICE?

RLIMIT_NICE is a Linux-specific limit (since kernel 2.6.12) that sets a ceiling on the nice value a process can set for itself using nice() or sched_setscheduler(). A lower nice value means higher CPU priority. RLIMIT_NICE prevents unprivileged processes from boosting their own priority above an allowed ceiling.

The Ceiling Formula

Nice values on Linux range from -20 (highest priority) to +19 (lowest priority). The RLIMIT_NICE ceiling is calculated using this formula:

ceiling_nice_value = 20 − rlim_cur

Where rlim_cur is the soft RLIMIT_NICE limit. This means:

rlim_cur (RLIMIT_NICE) Ceiling nice value (20 − rlim_cur) Priority effect
0 20 (but nice max is 19, so +19) Lowest priority
1 19 Lowest priority
20 0 Default priority
30 −10 Higher priority
40 −20 Highest priority
Why the inverted formula? Resource limits are expressed as positive numbers (higher = more). But nice values are inverted (lower = more priority). The formula 20 − rlim_cur bridges this: a higher RLIMIT_NICE allows a more negative (higher-priority) nice value.

Code Example — RLIMIT_NICE

#include 
#include 
#include 
#include 
#include <sys/resource.h>

int main(void)
{
    struct rlimit rl;

    /* Show current RLIMIT_NICE */
    getrlimit(RLIMIT_NICE, &rl);
    printf("RLIMIT_NICE: soft=%lld hard=%lld\n",
           (long long)rl.rlim_cur, (long long)rl.rlim_max);

    /* With rlim_cur=0, the ceiling nice value is 20 - 0 = 20
       Since nice max is 19, the process can only set nice to +19 or higher
       i.e. it cannot raise its priority at all */

    /* Set RLIMIT_NICE to 25: ceiling = 20 - 25 = -5
       Process can now set nice values down to -5 */
    rl.rlim_cur = 25;
    rl.rlim_max = 25;
    setrlimit(RLIMIT_NICE, &rl);
    printf("Set RLIMIT_NICE=25, ceiling nice = %d\n", 20 - 25);

    /* Try to set nice to -5 (should now be allowed) */
    if (setpriority(PRIO_PROCESS, 0, -5) == -1) {
        if (errno == EPERM)
            printf("setpriority(-5) denied: EPERM\n");
        else
            perror("setpriority");
    } else {
        printf("nice set to -5 successfully\n");
    }

    /* Try to set nice to -6 (one below ceiling — should fail) */
    if (setpriority(PRIO_PROCESS, 0, -6) == -1) {
        if (errno == EPERM)
            printf("setpriority(-6) denied: EPERM (above ceiling)\n");
    }
    return 0;
}
/* Compile: gcc -o rlimit_nice rlimit_nice.c */

Interview Questions

Q1. What is the formula for the nice value ceiling imposed by RLIMIT_NICE?

ceiling = 20 − rlim_cur. A process can set its nice value to the ceiling or higher (less priority), but not lower (more priority) than the ceiling.

Q2. If RLIMIT_NICE is set to 0, what nice values can the process use?

With rlim_cur=0, the ceiling is 20−0=20. Since the maximum allowed nice value is 19, this means the process can use nice values 19 down to 19 (only the lowest priority). It cannot raise its priority at all.

Q3. Since which kernel version is RLIMIT_NICE available?

Since Linux kernel 2.6.12. It is Linux-specific and not in SUSv3.

Leave a Reply

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