6

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, &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?

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.

Community
  • 1
  • 1
user761451
  • 103
  • 1
  • 1
  • 7
  • Another Question is, what happens, if separate multithreaded processes run in parallel? How will the processor time be distributed between those? – user761451 May 24 '11 at 11:04

2 Answers2

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.

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.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • So it has no mentionable effect to switch the main-task to the RR-scheduler on a Linux-system, assuming the main-task does not do anything useful, and it is absolutely sufficient to switch only the threads? Not even with respect to running multiple multithreaded programs? – user761451 May 24 '11 at 13:23
  • On a NTPL implementation, yes, your thread is considered as any other process in the system, no priority inheritance available ;) – Cédric Julien May 24 '11 at 13:32
  • 1
    `getconf GNU_LIBPTHREAD_VERSION` tells about the threading implementation. – user761451 May 24 '11 at 13:51
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.

wli
  • 1