涉及pthread时了解Linux调度

时间:2021-07-26 02:13:18

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

通过讨论另一个问题,请参阅调试奇怪的错误,这取决于所选的调度程序,我遇到了一些关于我的线程调度的问题。我在Linux 2.6.x上,使用root权限运行并使用pthreads在用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.

我理解这是切换我的程序以使用RR-Scheduler的代码,运行时最多。优先。

When starting a pthread, I do:

在启动pthread时,我会这样做:

sched_param param;
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_getschedparam(&attr, &param);
param.sched_priority = priority;
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setschedparam(&attr, &param);

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?

我理解这一点,是使用'priority'中给出的优先级将要启动的线程切换到RR-Scheduler的代码。如果main不会切换调度程序,那是否会等效地工作?

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.

我不明白的是,如果有必要在main中调用该代码? (主要功能除了启动所有功能,然后阻止键盘输入外没有任何作用。)我在哪里可以找到有关其工作原理的精确文档。我不认为联机帮助页在解释背景方面做得很好。

Thanks in advance.

提前致谢。

2 个解决方案

#1


3  

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.

默认情况下,Linux使用ntpl(本机POSIX线程库)实现,该实现将线程视为轻量级进程,因此调度程序将线程与其他进程一起调度。

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)

在FreeBSD上,你有“原始”的pthread实现,它允许你指定线程调度策略,但线程没有被安排为默认的进程(除非设置了PTHREAD_SCOPE_SYSTEM参数)

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.

我在这里找到了PThreads和NTPL之间的比较。

#2


0  

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.

NPTL实现是1:1模型;用户空间中的线程和内核空间中的一个进程,即调用LWP。内核调度的LWP的内容范围为PTHREAD_SCOPE_SYSTEM。

#1


3  

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.

默认情况下,Linux使用ntpl(本机POSIX线程库)实现,该实现将线程视为轻量级进程,因此调度程序将线程与其他进程一起调度。

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)

在FreeBSD上,你有“原始”的pthread实现,它允许你指定线程调度策略,但线程没有被安排为默认的进程(除非设置了PTHREAD_SCOPE_SYSTEM参数)

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.

我在这里找到了PThreads和NTPL之间的比较。

#2


0  

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.

NPTL实现是1:1模型;用户空间中的线程和内核空间中的一个进程,即调用LWP。内核调度的LWP的内容范围为PTHREAD_SCOPE_SYSTEM。