Understanding Linux Planning Using pthreads

through a discussion of another problem, see Debugging a strange error, which depends on the selected scheduler , I came across some questions about scheduling my threads. I am on Linux 2.6.x working with root privileges and using pthreads to do side-effects in a critical urgent application written in C / C ++.

I will try to give some short, boiled, fragments to explain my question:

Basically, I do somewhere in the beginning:

struct sched_param sp; memset(&sp, 0, sizeof(sched_param)); sp.sched_priority = 99; sched_setscheduler(getpid(), SCHED_RR, &sp); 

I understand that this is the code that switches my program to using the RR-Scheduler, which runs on max. a priority.

When starting 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 code that switches the thread that RR-Scheduler will run using the priority specified in "priority". Will this work equivalently if main does not switch the scheduler?

What I do not understand if it is necessary to call this code basically? (The main function does nothing but launch everything, and then blocks keyboard input). Where can I find the exact documentation of how this works. I don't think manpages do a good job explaining the background.

Thanks in advance.

+6
source share
2 answers

Linux by default uses ntpl (Native POSIX Thread Library) , which treats the thread as a light-weigth process, therefore a thread schedule scheduler with other processes.

On FreeBSD, you have an โ€œoriginalโ€ pthread implementation that allows you to specify a thread scheduling policy, but threads are not scheduled as the default process (unless PTHREAD_SCOPE_SYSTEM is specified)

So, in your example, your thread is planned as a standard process with a high priority, so it will compete with all other processes with the same priority as your main process.

If your critical moment in your flow, avoid giving high priority to your main process, it will make another process compete with your material in real time.

I found a comparison between PThreads and NTPL here .

+3
source

Implementation of NPTL - 1: 1 model; a thread in user space and a process in kernel space, which is an LWP call. The kernel-scheduled LWP has a PTHREAD_SCOPE_SYSTEM content area.

0
source

Source: https://habr.com/ru/post/888891/


All Articles