了解涉及 pthread 时的 Linux 调度
通过对另一个问题的讨论,参见调试取决于所选调度程序的奇怪错误,我遇到了一些关于我的调度程序的问题线程。我使用的是 Linux 2.6.x,以 root 权限运行,并使用 pthreads 在用 C/C++ 编写的计时关键应用程序中执行并行操作。
我将尝试给出一些简短的、浓缩的片段来解释我的问题:
主要是我在开始的某个地方做了:
struct sched_param sp;
memset(&sp, 0, sizeof(sched_param));
sp.sched_priority = 99;
sched_setscheduler(getpid(), SCHED_RR, &sp);
我理解这是将我的程序切换为使用 RR 调度程序的代码,以最大速度运行。优先事项。
当启动 pthread 时,我这样做:
sched_param param;
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority = priority;
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setschedparam(&attr, ¶m);
我理解这一点,即使用“优先级”中给出的优先级将要启动的线程切换到 RR 调度程序的代码。 如果 main 不切换调度程序,这是否同样有效?
我不明白的是,是否有必要在 main 中调用该代码? (主函数除了启动所有内容然后阻止键盘输入之外什么也不做。) 我在哪里可以找到有关其工作原理的精确文档。我认为手册页在解释背景方面做得不好。
提前致谢。
through the discussion of another problem, see Debugging strange error that depends on the selected scheduler, I ran into some questions about the scheduling of my threads. I am on Linux 2.6.x, running with root-rights and using pthreads to do parallel things in a timing critical application written in C/C++.
I'll try to give some short, boiled down, snippets to explain my question:
In main I somewhere at the beginning do:
struct sched_param sp;
memset(&sp, 0, sizeof(sched_param));
sp.sched_priority = 99;
sched_setscheduler(getpid(), SCHED_RR, &sp);
I understand this to be the code that switches my program to use the RR-Scheduler, running at max. priority.
When starting a pthread, I do:
sched_param param;
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority = priority;
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setschedparam(&attr, ¶m);
I understand this, to be the code that switches the thread that's gonna be started to RR-Scheduler, using the priority given in 'priority'.
Is that going to work equivalently if main would not switch the scheduler?
What I do not understand is, if it is necessary to call that code in main? (The main-function does not do anything than starting everything and then block on keyboard input.)
Where can I find precise documentation of how this works. I don't think the manpages do a good job on explaining the background.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Linux 默认情况下使用 ntpl(本机 POSIX 线程库) 实现,该实现将线程视为轻量级- 权重进程,因此调度程序与其他进程一起调度线程。
在 FreeBSD 上,您拥有“原始”pthread 实现,它允许您指定线程调度策略,但线程默认情况下不被调度为进程(除非设置了 PTHREAD_SCOPE_SYSTEM 参数)
因此,在您的示例中,您的线程被调度为具有高优先级的标准进程,因此它将与具有相同优先级的所有其他进程竞争,您的主要流程也。
如果你的时间关键的东西在你的线程中,避免给你的主进程赋予高优先级,它会减少一个进程与你的实时东西竞争。
我在此处找到了 PThreads 和 NTPL 之间的比较。
Linux by default, uses the ntpl (Native POSIX Thread Library) implementation which considers a thread as a light-weigth process, so the scheduler schedules threads with other processes.
On FreeBSD, you have the "original" pthread implementation that allows you to specify threads scheduling policy but threads are not scheduled as process on default (unless PTHREAD_SCOPE_SYSTEM parameter is set)
So, in your example, your thread is scheduled as a standard process with a high priority so it will be in competition with all others processes with the same level of priority, your main process also.
If your time-critical stuff is in your thread, avoid to give a high priority to your main process, it will make one less process in competition with your real time stuff.
I found a comparison between PThreads and NTPL here.
NPTL实现是1:1模型;用户空间中的线程和内核空间中的进程(称为 LWP)。
由内核调度的 LWP 的内容范围为 PTHREAD_SCOPE_SYSTEM。
The NPTL implementation is 1:1 model; a thread in user space and a process in kernel space which is call LWP.
A LWP scheduled by kernel has the content scope of PTHREAD_SCOPE_SYSTEM.