【问题标题】:Unable to set scheduling policy using sched_setscheduler() function?无法使用 sched_setscheduler() 函数设置调度策略?
【发布时间】:2018-05-02 10:56:54
【问题描述】:

我是操作系统编程的新手。因此,我编写了这段代码来更改后台进程的调度策略,我通过命令行参数提供了该后台进程的进程 ID,但是 sched_setscheduler() 函数失败,并给出错误,“函数未实现”

#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);
}

谁能告诉我为什么会这样?谢谢。

【问题讨论】:

    标签: process operating-system scheduling job-scheduling


    【解决方案1】:

    因此,我编写了这段代码来更改后台进程的调度策略,我通过命令行参数提供其进程 ID

    这就是问题所在。 sched_setscheduler(2) 得到一个 thread id 而不是 process id。对于单线程进程PIDTID 重合,但在多线程进程中每个线程都有自己的TID

    参数名为pid 并且类型为pid_t 的事实可能具有误导性,甚至sched_setscheduler(2) 的一些旧手册页错误地谈论了进程,但这个函数实际上是关于线程的。

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 2023-04-05
      • 2015-04-24
      • 2019-11-26
      相关资源
      最近更新 更多