You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下SCHED_OTHER调度线程优先级调整问题求助

问题描述
  • 线程采用默认调度策略SCHED_OTHER,优先级固定为0,无法通过sched_param.param.sched_priority修改优先级:
    • 无法使用系统调用pthread_setschedparam;
    • 调用pthread_setschedprio(std::thread::native_handle(), -1)对线程优先级无影响,已通过getpriority(PRIO_PROCESS, tid)验证,疑问:默认调度器能否使用pthread_setschedprio?
  • 查阅sched.7手册后,了解到需通过调整nice值修改线程动态优先级,但尝试以下方法均失败:
    • 调用nice(19)无效果,推测该调用为进程级nice值修改,已排除;
    • 调用setpriority(PRIO_PROCESS, id, 19)始终返回-1,errno为ESRCH(无此进程),但手册显示该调用可修改线程优先级;
    • 调用syscall(SYS_sched_setattr, id, &attr, flags),配置sched_attr为SCHED_OTHER策略、sched_nice=6,无效果。
需求

不改变调度策略的前提下,降低进程中高CPU占用线程的优先级,确保同优先级其他线程获得CPU时间。

解决方案

通过传入gettid返回的线程ID调用setpriority修改线程优先级,示例代码:

setpriority(PRIO_PROCESS, static_cast<pid_t>(syscall(SYS_gettid)), 19);

内容的提问来源于stack exchange,提问作者Rose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:39:52