How to get permission for pthread_setschedparam

混江龙づ霸主 提交于 2020-01-15 11:09:06

问题


I need to run a C++ program at real-time priority on a BeagleBone with Angstrom Linux version 3.2.18. The program calls pthread_setschedparam to attempt to elevate its priority. When logged in as root, I get an "Operation not permitted" error. However, if I first elevate to superuser "su" at the shell prompt, the call to pthread_setschedparam is successful.

How can I achieve the same result without running "su" first? I need to run this program as a service that starts automatically at boot time.


回答1:


In the manpage for pthread_setschedparam (I assume that's what you meant), it states under Notes:

For a description of the permissions required to, and the effect of, changing a thread's scheduling policy and priority, and details of the permitted ranges for priorities in each scheduling policy, see sched_setscheduler(2).

In that manpage, it's detailed under Privileges and resource limits:

In Linux kernels before 2.6.12, only privileged (CAP_SYS_NICE) processes can set a nonzero static priority (i.e., set a real-time scheduling policy). The only change that an unprivileged process can make is to set the SCHED_OTHER policy, and this can only be done if the effective user ID of the caller of sched_setscheduler() matches the real or effective user ID of the target process (i.e., the process specified by pid) whose policy is being changed.

Since Linux 2.6.12, the RLIMIT_RTPRIO resource limit defines a ceiling on an unprivileged process's static priority for the SCHED_RR and SCHED_FIFO policies. The rules for changing scheduling policy and priority are as follows:

  • If an unprivileged process has a nonzero RLIMIT_RTPRIO soft limit, then it can change its scheduling policy and priority, subject to the restriction that the priority cannot be set to a value higher than the maximum of its current priority and its RLIMIT_RTPRIO soft limit.

  • If the RLIMIT_RTPRIO soft limit is 0, then the only permitted changes are to lower the priority, or to switch to a non-real-time policy.

  • Subject to the same rules, another unprivileged process can also make these changes, as long as the effective user ID of the process making the change matches the real or effective user ID of the target process.

  • Special rules apply for the SCHED_IDLE: an unprivileged process operating under this policy cannot change its policy, regardless of the value of its RLIMIT_RTPRIO resource limit.

Privileged (CAP_SYS_NICE) processes ignore the RLIMIT_RTPRIO limit; as with older kernels, they can make arbitrary changes to scheduling policy and priority. See getrlimit(2) for further information on RLIMIT_RTPRIO.



来源:https://stackoverflow.com/questions/14971910/how-to-get-permission-for-pthread-setschedparam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!