在工作中遇到使用 pthread_setschedparam时,返回了EPERM 异常,该异常是说没有权限。
程序运行的环境是,ssh 连接到一台linux环境下,sudo su切换到root用户。这似乎是一个系统bug。详细的过程见:https://github.com/coreos/bugs/issues/410
从该issue中,提供了两种方法:
第一:更改 kernel.sched_rt_runtime_us 值为 -1.
操作:sysctl kernel.sched_rt_runtime_us,先查看当前值
在我的环境下,kernel.sched_rt_runtime_us = 950000
sysctl -w kernel.sched_rt_runtime_us=-1 更改这个配置项。
然后再重新运行你的程序。本人使用此方法有避免EPERM异常
第二:把当前ssh连接进来的shell,添加进 cpu:/ 组下,以保证有权限操作。
原文解释说,systemd默认情况下,把ssh连接添加在一个非RT组下,此时当你需要通过ssh连接的前台,运行FIFO/RR 任务时,并不能正常执行。
操作是:cgclassify -g cpu:/ $$
也添加在service文件的[Service]项下,添加 ControlGroup=cpu:/
原文如下:
what may or may not be very related, systemd defaults put ssh connections into a non-RT cgroup. if you're trying to ssh in and run FIFO/RR tasks, it just won't work. you need to move your shell into the default cpu:/ group which does permit priority scheduling.cgclassify -g cpu:/ $$your systemd unit file can start your service and get priority scheduling if you put ControlGroup=cpu:/ under [Service]
第二种方法,本人没有亲试过,请谨慎选择。