gpt4 book ai didi

c - 为什么 sched_setscheduler() 如果没有包含在 if block 中则不起作用?

转载 作者:行者123 更新时间:2023-12-02 00:09:07 25 4
gpt4 key购买 nike

以下代码有效并需要“root”身份验证:

struct sched_param param;
param.sched_priority = 99;
if (sched_setscheduler(0, SCHED_FIFO, & param) != 0) {
perror("sched_setscheduler");
exit(EXIT_FAILURE);
}

但是,这个似乎可以工作(没有错误)但没有任何效果并且不需要“root”身份验证:

struct sched_param param;
param.sched_priority = 99;
sched_setscheduler(0, SCHED_FIFO, & param);

为什么?我用 gcc/Ubuntu 13 编译。

最佳答案

很可能 sched_setscheduler 在您的第二个示例中不起作用。您只是忽略了可能不是 0 的返回值。

由于您忽略了返回值,您实际上并不知道它是否有效。

查看 sched_setscheduler 的手册页,您会在 RETURN VALUE 下找到它

RETURN VALUE
On success, sched_setscheduler() returns zero.
On success, sched_getscheduler() returns the policy for the process (a nonnegative integer).
On error, -1 is returned, and errno is set appropriately.

如果返回 -1,则设置 errno 并且 perror 打印一个人类可读的错误字符串。

既然你说 -1 是从第二个例子中返回的,sched_setscheduler 实际上并没有起作用。

关于c - 为什么 sched_setscheduler() 如果没有包含在 if block 中则不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16502054/

25 4 0
文章推荐: r - Extent S4 类,如何使用父类(super class)的构造函数
文章推荐: mathjax - 如何以编程方式将 MathJax LaTeX 格式应用于
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com