'sched_setscheduler returns error with no more information

Based on the example here, in the following code, I first get the pid of the program in main(), then retrieve the scheduling policy and then set to another policy.

#include <unistd.h>
#include <sched.h>
#include <stdio.h>

int main()
{
    int pid_num = getpid();
    printf("PID = %d -> ", pid_num);
    
    int policy = sched_getscheduler(pid_num);
    switch(policy) {
        case SCHED_OTHER: printf("SCHED_OTHER -> "); break;
        case SCHED_RR:   printf("SCHED_RR -> "); break;
        case SCHED_FIFO:  printf("SCHED_FIFO -> "); break;
        default:   printf("Unknown... -> ");
    }
    
    struct sched_param sp = { .sched_priority = 50 };
    int ret = sched_setscheduler(pid_num, SCHED_FIFO, &sp);
    if (ret == -1) {
        printf("sched_setscheduler failed!\n");
        return 1;
    }
    
    printf(":)\n");
    return 0;
}

However, according to the output below, it fails and I don't know what else should I do.

$ ./app 
PID = 122637 -> SCHED_OTHER -> sched_setscheduler failed!

Any idea about that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source