Linux 如何提高pthreads中的线程优先级?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3649281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:24:35  来源:igfitidea点击:

How to increase thread priority in pthreads?

c++linuxmultithreadingpthreads

提问by Steveng

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.

我在 Linux 中使用 pthread。我想通过设置参数来提高线程优先级sched_param.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?

另外,我想知道相对线程优先级,因为我不想将线程优先级设置得太高而导致操作系统停止。有人可以帮我解决这个问题吗?

采纳答案by levif

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

默认的 Linux 调度策略是SCHED_OTHER,它没有优先级选择,只能nice在策略内部进行调整。

You'll have to change to another scheduling policyusing 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_BATCHas 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 chrttool from util-linuxpackage.
As an example:

为了确定您的机器的功能,您可以使用chrtpackage.json 中的工具 util-linux
举个例子:

$ 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 wasteless time (which I often use):

一种浪费更少时间的方法(我经常使用):

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

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

在保留用户权限的同时,这将make15%(在我的情况下)。

Edit:introducing nice, SCHED_BATCH, SCHED_IDLEand chrttool. For accuracy ! :)

编辑:引入niceSCHED_BATCHSCHED_IDLEchrt工具。为了准确!:)

回答by Potatoswatter

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_max(int policy);

int sched_get_priority_min(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. Don't be surprised if the query tells you that there is no priority higher than the default.

不要指望提高优先级来扼杀机器。事实上,除非您已经使用了 100% 的 CPU 周期,否则不要指望它做任何事情。如果查询告诉您没有比默认值更高的优先级,请不要感到惊讶。

回答by BobDoolittle

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). Your only choice for the non-RT policies is to set 'nice' values e.g. by 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. For current Linux kernels 'nice' has a very strong effect similar to priority, so you can use it pretty much interchangeably. In order to increase how often your thread is scheduled, you want to loweryour 'nice' value. This requires CAP_SYS_NICE capability (typically root, although not necessarily, see http://man7.org/linux/man-pages/man7/capabilities.7.htmland http://man7.org/linux/man-pages/man3/cap_set_proc.3.html).

在今天的内核中,只有实时调度策略允许设置 sched_priority - 对于非 RT 策略(SCHED_OTHER、SCHED_BATCH 和 SCHED_IDLE),它始终为 0。您对非 RT 策略的唯一选择是设置“nice”值,例如通过 setpriority()。然而,通过设置“nice”并没有很好的规范预期的确切行为,至少在理论上它可能因内核版本而异。对于当前的 Linux 内核,'nice' 具有类似于优先级的非常强大的效果,因此您可以互换使用它。为了增加线程的调度频率,您需要降低“nice”值。这需要 CAP_SYS_NICE 功能(通常是 root,但不一定,请参阅http://man7.http://man7.org/linux/man-pages/man3/cap_set_proc.3.html)。

In fact SCHED_BATCH is designed for the oppositecase to what the questioner requested: it is designed for CPU-intensive, long running jobs, that can live with lowerpriority. It tells the scheduler to slightly penalize wake-up priority for the threads.

事实上,SCHED_BATCH 是为提问者所要求的相反情况而设计的:它是为 CPU 密集型、长时间运行的作业而设计的,可以以较低的优先级生存。它告诉调度程序稍微惩罚线程的唤醒优先级。

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. 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. I find it amusing that this is often called out in the "BUGS" section of the man pages. 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. In other words - not a bug.

还要回答之前的评论之一(我还没有足够的声誉来回应评论-对此答案的一些赞成会有所帮助:))。是的,坏消息是 POSIX.1 规范说“好”影响进程,而不是单个线程。好消息是 Linux 线程实现(NPTL 和原始 Linux 线程)打破了规范并允许它影响单个线程。我觉得这很有趣,这经常在手册页的“BUGS”部分中提到。我想说这个错误是在 POSIX.1 规范中,它应该允许这种行为,而不是在不顾规范而被迫提供它的实现中,并且是故意和故意这样做的。换句话说 - 不是错误。

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)。