I am trying to write a semi-portable thread class for a project that I am working on, and trying to prioritize a thread, I came across a puzzle in the pthread world.
Given the following code:
#include <stdio.h> #include <unistd.h> #include <sched.h> main() { printf("Valid priority range for SCHED_OTHER: %d - %d\n", sched_get_priority_min(SCHED_OTHER), sched_get_priority_max(SCHED_OTHER)); printf("Valid priority range for SCHED_FIFO: %d - %d\n", sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO)); printf("Valid priority range for SCHED_RR: %d - %d\n", sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR)); }
On OpenBSD, this will print the following:
Valid priority range for SCHED_OTHER: 0 - 31 Valid priority range for SCHED_FIFO: 0 - 31 Valid priority range for SCHED_RR: 0 - 31
On Ubuntu, this produces the following:
Valid priority range for SCHED_OTHER: 0 - 0 Valid priority range for SCHED_FIFO: 1 - 99 Valid priority range for SCHED_RR: 1 - 99
My question is more about SCHED_OTHER
priority. According to all the documentation that I was able to collect (sched.h, google, SO, SE), I understand that you need to try and get the process priority of the current process and assign priority based on this. This is normal, except when getting min / max values for SCHED_OTHER
. Since scheduling is system-dependent, if I get the priority of the current process as SCHED_OTHER
and try to get min / max values on Ubuntu (or other Linux systems that I came across), my values will be 0-0, which will not work to set the correct range priorities. However, I get valid values for SCHED_OTHER
on other unix-like systems (OpenBSD, etc.).
Since my main concern is only to get a valid range of priority values (to ensure that the user does not enter too high or low value), I should not even worry about the priority of the current process and get only min / max for SCHED_FF
or SCHED_RR
, as these values give me valid ranges for the various linux / unix systems that I came across, or am I completely losing the point?
Thanks in advance, and please let me know if I did not understand or something did not understand.
EDIT 1 : Please note that my main problem is the “portable” way of getting valid ranges more than the process priority itself. Thanks!