如何提高pthreads中的线程优先级? [英] How to increase thread priority in pthreads?
问题描述
我在 Linux 中使用 pthread.我想通过设置参数sched_param.priority
来增加线程优先级.但是,我在网上找不到很多关于我可以设置的线程优先级范围的信息,或者关于线程优先级的描述.
I am using pthread in Linux. I would like to increase the thread priority by setting the parameters sched_param.priority
. However, I could not find much info from the net regarding the range of the thread priority I could set, or about the description of the thread priority.
另外,我想知道相对线程优先级,因为我不想将线程优先级设置得太高而导致操作系统停止.有人可以帮我解决这个问题吗?
Also, I would like to know about the relative thread priority as I would not want to set the thread priority to be too high and resulting the OS to halt. Could someone help me with this?
推荐答案
默认的 Linux 调度策略是 SCHED_OTHER
,它没有优先级选择,只有一个 nice
级别可以调整政策内.
The default Linux scheduling policy is SCHED_OTHER
, which have no priority choice but a nice
level to tweak inside the policy.
您必须使用函数pthread_setschedparam
更改为另一个调度策略(另请参阅man sched_setscheduler
)
You'll have to change to another scheduling policy using function pthread_setschedparam
(see also man sched_setscheduler
)
正常"调度策略:(来自sched_setscheduler(2)
)
'Normal' scheduling policies: (from sched_setscheduler(2)
)
SCHED_OTHER the standard round-robin time-sharing policy;
SCHED_BATCH for "batch" style execution of processes; and
SCHED_IDLE for running very low priority background jobs.
实时调度策略:
SCHED_FIFO a first-in, first-out policy; and
SCHED_RR a round-robin policy.
在您的情况下,您可以使用 SCHED_BATCH
,因为这不需要 root 权限.
In your case maybe you can use SCHED_BATCH
as this does not require root privileges.
警告:错误地使用实时调度策略可能会使您的系统挂起.这就是为什么您需要 root 权限才能执行此类操作.
Warning: wrong usage of real-time scheduling policies may hang your system. That's why you need root privileges to do this kind of operation.
为了确定您的机器的功能,您可以使用 chrt
工具从util-linux
包.
例如:
Just to be sure of what your machine is capable of, you can use chrt
tool from
util-linux
package.
As an example:
$ chrt -m
SCHED_OTHER min/max priority : 0/0
SCHED_FIFO min/max priority : 1/99
SCHED_RR min/max priority : 1/99
SCHED_BATCH min/max priority : 0/0
SCHED_IDLE min/max priority : 0/0
一种浪费时间的方法(我经常使用):
A way to waste less time (which I often use):
alias batchmake='time chrt --batch 0 make --silent'
在保留用户权限的同时,这将 make
推动了 15%(在我的情况下).
While staying with user privileges, this propels the make
by 15% (in my case).
编辑:引入nice
、SCHED_BATCH
、SCHED_IDLE
和chrt
工具.为了准确!:)
introducing nice
, SCHED_BATCH
, SCHED_IDLE
and chrt
tool. For accuracy ! :)
这篇关于如何提高pthreads中的线程优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!