Pthread Priority on Non PREEMPT Linux Kernels | Sololearn: Learn to code for FREE!


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); }

3/22/2019 6:07:51 AM


5 Answers

New Answer


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.


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?