如何增加线程优先级在pthreads? [英] How to increase thread priority in pthreads?

查看:390
本文介绍了如何增加线程优先级在pthreads?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux中使用pthread。我想通过设置参数 sched_pa​​ram.priority 来增加线程优先级。但是,我从网上找不到很多关于线程优先级的范围的信息,或者关于线程优先级的描述。



此外,喜欢了解相对线程优先级,因为我不想将线程优先级设置得太高,导致操作系统停止。

解决方案

默认的Linux调度策略是 SCHED_OTHER ,它们没有优先选择,而是在策略内调整的 nice 级别。



使用函数 pthread_setschedparam 更改为另一个计划策略(另请参阅 man sched_setscheduler

'正常'计划政策:(来自 sched_setscheduler(2)


$ b b

  SCHED_OTHER标准循环分时策略; 
SCHED_BATCH用于批处理样式的进程执行;和
SCHED_IDLE用于运行非常低优先级的后台作业。

实时时间安排策略:

  SCHED_FIFO先进先出策略;和
SCHED_RR一个循环策略。

在您的情况下,您可以使用 SCHED_BATCH 因为这不需要root权限。



警告:实时调度策略的错误使用可能会挂起您的系统。这就是为什么你需要root权限来做这种操作。



只要确定机器的功能,您可以使用 chrt 工具从
util-linux 包。
例如:

  $ chrt -m 
SCHED_OTHER最小/最大优先级:0/0
SCHED_FIFO最小/最大优先级:1/99
SCHED_RR最小/最大优先级:1/99
SCHED_BATCH最小/最大优先级:0/0
SCHED_IDLE最小/最大优先级:0 / 0

一种方式可以减少时间(我经常使用)

  alias batchmake ='time chrt --batch 0 make --silent'

在保留用户权限的同时,这会将 make 增加15%(在我的例子中)。



编辑:介绍 nice SCHED_BATCH SCHED_IDLE chrt 工具。准确度! :)


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?

解决方案

The default Linux scheduling policy is SCHED_OTHER, which have no priority choice but a nice level to tweak inside the policy.

You'll have to change to another scheduling policy using function pthread_setschedparam (see also man sched_setscheduler)

'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.

Real-time scheduling policies:

   SCHED_FIFO    a first-in, first-out policy; and
   SCHED_RR      a round-robin policy.

In your case maybe you can use SCHED_BATCH as this does not require root privileges.

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.

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'

While staying with user privileges, this propels the make by 15% (in my case).

Edit: introducing nice, SCHED_BATCH, SCHED_IDLE and chrt tool. For accuracy ! :)

这篇关于如何增加线程优先级在pthreads?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆