简体   繁体   English

如何在pthreads中增加线程优先级?

[英]How to increase thread priority in pthreads?

I am using pthread in Linux. 我在Linux中使用pthread。 I would like to increase the thread priority by setting the parameters sched_param.priority . 我想通过设置参数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. 默认的Linux调度策略是SCHED_OTHER ,它没有优先级选择,但在策略内部调整nice

You'll have to change to another scheduling policy using function pthread_setschedparam (see also man sched_setscheduler ) 您必须使用函数pthread_setschedparam更改为另一个调度策略 (另请参阅man sched_setscheduler

'Normal' scheduling policies: (from sched_setscheduler(2) ) '正常'调度策略:(来自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. 在您的情况下,您可以使用SCHED_BATCH因为这不需要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. 这就是您需要root权限才能执行此类操作的原因。

Just to be sure of what your machine is capable of, you can use chrt tool from util-linux package. 只是为了确定您的机器能够做什么,您可以使用util-linux软件包中的chrt工具。
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). 在保持用户权限的同时,这make推动15%(在我的情况下)。

Edit: introducing nice , SCHED_BATCH , SCHED_IDLE and chrt tool. 编辑:引入niceSCHED_BATCHSCHED_IDLEchrt工具。 For accuracy ! 为了准确! :) :)

The current answer from levif (recommending SCHED_BATCH) is not correct for the current NPTL thread implementation on Linux (you can check which implementation your kernel has by running 'getconf GNU_LIBPTHREAD_VERSION'). levif(推荐SCHED_BATCH)的当前答案对于Linux上的当前NPTL线程实现是不正确的(您可以通过运行'getconf GNU_LIBPTHREAD_VERSION'来检查内核的实现)。

In today's kernel only the Real Time Scheduling policies allow setting of sched_priority - it is always 0 for the non-RT policies (SCHED_OTHER, SCHED_BATCH, and SCHED_IDLE). 在今天的内核中,只有实时调度策略允许设置sched_priority - 对于非RT策略(SCHED_OTHER,SCHED_BATCH和SCHED_IDLE),它始终为0。 Your only choice for the non-RT policies is to set 'nice' values eg by setpriority(). 您对非RT策略的唯一选择是设置'nice'值,例如通过setpriority()。 There are not good specifications of the exact behavior to expect by setting 'nice' however, and at least in theory it could vary from kernel version to kernel version. 然而,通过设置'nice'并没有很好的期望确切的行为,并且至少在理论上它可以从内核版本到内核版本不同。 For current Linux kernels 'nice' has a very strong effect similar to priority, so you can use it pretty much interchangeably. 对于当前的Linux内核,“nice”具有类似于优先级的非常强大的效果,因此您可以互换使用它。 In order to increase how often your thread is scheduled, you want to lower your 'nice' value. 为了增加线程的安排频率,您希望降低 “漂亮”值。 This requires CAP_SYS_NICE capability (typically root, although not necessarily, see http://man7.org/linux/man-pages/man7/capabilities.7.html and http://man7.org/linux/man-pages/man3/cap_set_proc.3.html ). 这需要CAP_SYS_NICE功能(通常是root,但不一定,请参阅http://man7.org/linux/man-pages/man7/capabilities.7.htmlhttp://man7.org/linux/man-pages/man3 /cap_set_proc.3.html )。

In fact SCHED_BATCH is designed for the opposite case to what the questioner requested: it is designed for CPU-intensive, long running jobs, that can live with lower priority. 事实上,SCHED_BATCH的设计与提问者要求的情况相反 :它专为CPU密集型,长时间运行的作业而设计,可以以较低的优先级生活。 It tells the scheduler to slightly penalize wake-up priority for the threads. 它告诉调度程序略微惩罚线程的唤醒优先级。

Also to answer one of the earlier comments (I don't yet have sufficient reputation to comment in response - some upvotes for this answer would help :) ). 也回答之前的评论之一(我还没有足够的声誉来回应评论 - 这个答案的一些赞成将有助于:))。 Yes the bad news is that the POSIX.1 specification says that 'nice' affects processes, not individual threads. 是坏消息是POSIX.1规范说'nice'会影响进程,而不会影响单个线程。 The good news is that the Linux thread implementations (both NPTL and the original Linux Threads) break the specification and allow it to affect individual threads. 好消息是Linux线程实现(NPTL和原始Linux线程)打破了规范并允许它影响单个线程。 I find it amusing that this is often called out in the "BUGS" section of the man pages. 我觉得很有趣的是,这通常在手册页的“BUGS”部分中提到。 I'd say the bug was in the POSIX.1 specification, which should have allowed this behavior, not in the implementations which were compelled to provide it in spite of the specification and did so knowingly and deliberately. 我说这个bug是在POSIX.1规范中的,它应该允许这种行为,而不是在那些被迫提供它的实现中,尽管有规范并且故意和故意这样做。 In other words - not a bug. 换句话说 - 不是错误。

Most of this is detailed on the sched(7) man page (which for some reason is not delivered on my Fedora 20 system): http://man7.org/linux/man-pages/man7/sched.7.html 其中大部分内容详见sched(7)手册页(出于某种原因未在我的Fedora 20系统上提供): http//man7.org/linux/man-pages/man7/sched.7.html

If you really wanted to affect sched_priority you could look at the Real Time policies such as SCHED_RR). 如果您真的想影响sched_priority,可以查看实时策略,例如SCHED_RR)。

POSIX defines a query, so you can ask OS for the valid range of priorities. POSIX定义了一个查询,因此您可以向操作系统询问有效的优先级范围。

int sched_get_priority_max(int policy);

int sched_get_priority_min(int policy);

Don't expect raised priority to choke the machine. 不要指望提高优先级来阻塞机器。 In fact, don't expect it to do anything unless you're already using 100% of CPU cycles. 事实上,除非你已经使用了100%的CPU周期,否则不要指望它做任何事情。 Don't be surprised if the query tells you that there is no priority higher than the default. 如果查询告诉您没有优先级高于默认值,请不要感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM