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