gpt4 book ai didi

process - 无法使用 sched_setscheduler() 函数设置调度策略?

转载 作者:行者123 更新时间:2023-12-02 21:14:55 34 4
gpt4 key购买 nike

我是操作系统编程新手。因此,我编写了这段代码来更改后台进程的调度策略,我通过命令行参数提供了其进程 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);
}

有人可以告诉我为什么会发生这种情况吗?谢谢。

最佳答案

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

问题就在这里。 sched_setscheduler(2)获取线程ID而不是进程ID。对于单线程进程,PIDTID 一致,但在多线程进程中,每个线程都有自己的 TID

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

关于process - 无法使用 sched_setscheduler() 函数设置调度策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371413/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com