Pthread Priority on Non PREEMPT Linux Kernels | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Pthread Priority on Non PREEMPT Linux Kernels

I've recently been exposed to <pthread.h>, which is is a Unix/Linux (POSIX) API for multi-threading. While playing around with the code and trying to prioritize certain threads so that they execute prior to other threads, I realized that my scheduling parameters, although do not return any errors, are being ignored by the the OS. Threads which I've set to the highest priority do not appear to have executed prior to threads with lower priority. I have been previously told that the Linux kernel I am running on has to have the PREEMPT (and possibly the RT) configurations to properly prioritize threads. Is that why my programs are not behaving according to my expectations? int main() { pthread_t tid1, tid2; pthread_attr_t attr; struct sched_param param; pthread_attr_init(&attr); pthread_attr_getschedparam(&attr, &param); param_sched_priority = 99; pthread_attr_setschedpolicy(&attr, &param); pthread_attr_setschedparam(&attr, &param); pthread_create(&tid1, &attr, myFunc1, NULL); pthread_attr_init(&attr); pthread_attr_getschedparam(&attr, &param); param_sched_priority = 1; pthread_attr_setschedpolicy(&attr, &param); pthread_attr_setschedparam(&attr, &param); pthread_create(&tid1, &attr, myFunc2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); }

22nd Mar 2019, 6:07 AM
Fermi
Fermi - avatar
2 Answers
+ 2
Ani Jona 🕊 Sorry for the late reply. The reason I haven't tested your code yet is because the build environment is installed on a machine which I currently have no access to. It will be about two days from now until I can get back to you regarding the output, but I will.
23rd Mar 2019, 8:28 AM
Fermi
Fermi - avatar
+ 1
Ani Jona 🕊 I've run the code above and received this output policy: SCHED_OTHER priority: 0 for both functions! I guess this is what was messing things up for me. Does this mean that my Linux config does not support prioritizing threads? How should I change the policy then?
25th Mar 2019, 12:51 AM
Fermi
Fermi - avatar