Unable to set scheduling policy using sched_setscheduler() function?

Viewed 911

I am new to OS programming. So, I wrote this code to change the scheduling policy of a background process, whose process ID I supply through a command line argument, but the sched_setscheduler() function fails, giving an error, "Function not implemented"

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

int main(int argc, char* argv[]){
    struct sched_param param;
    param.sched_priority = 80;
    int pid = atoi(argv[1]);
    int policy = sched_getscheduler(pid);
    printf("Current policy: %d\n", policy);
    if(sched_setscheduler(pid, SCHED_FIFO, &param) == -1){
        perror("Scheduler policy cannot be set");
    }
    int new_policy = sched_getscheduler(pid);
    printf("New policy: %d\n", new_policy);
}

Can someone please tell me why this happening? Thanks.

1 Answers

So, I wrote this code to change the scheduling policy of a background process, whose process ID I supply through a command line argument

Here is the problem. sched_setscheduler(2) gets a thread id not a process id. For single thread processes PID and TID coincide, but in multi-threaded processes every thread has its own TID.

The fact that the argument is named pid and is of type pid_t might be misleading and even some old man-pages of sched_setscheduler(2) wrongly talk about processes, but this function is actually about threads.

Related