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.
Scheduling. You use the Pthreads scheduling features to set up a policy that determines which thread the system first selects to run when CPU cycles become available, and how long each thread can run once it is given the CPU.
The scheduling policy can either be SCHED_FIFO or SCHED_RR. FIFO is a first come first serve policy. RR is a round robin policy that might preempt threads.
SCHED_OTHER is the standard Linux time-sharing scheduler that is intended for all threads that do not require the special real-time mechanisms. The thread to run is chosen from the static priority 0 list based on a dynamic priority that is determined only inside this list.
POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With