Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread sched_get_priority_min/max implementation for SCHED_OTHER/SCHED_FIFO

I'm trying to write a semi-portable threading class for a project I'm working on, and while trying to set the priority of a thread, I came across a conundrum 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));
}

In OpenBSD this would 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

In Ubuntu it produces this:

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 in regards to the SCHED_OTHER priority. According to all of the documentation I've been able to muster (sched.h, google, SO, SE), my understanding is that one should try and retrieve the process priority of the current process and assign priority based on that. This is fine, except when retrieving the 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 the min/max values on a Ubuntu (or other linux systems I've come across), my values would be 0-0, which won't work for trying to set a valid priority range. However, I do get valid values for SCHED_OTHER on other unix-like systems (OpenBSD, etc).

Since my main concern is only trying to get a valid range of priorities values (to ensure the user doesn't input too high or low a value), should I not even bother with the current process' priority and only get the min/max values for SCHED_FF or SCHED_RR since those values seem to give me valid ranges for the various linux/unix systems I've encountered, or am I missing the point completely?

Thanks in advance and please let me know if I'm unclear or misstating something.

EDIT 1: please note that my main concern is with a 'portable' way to get valid ranges more than the process' priority itself..thanks!

like image 340
txtechhelp Avatar asked Oct 08 '22 19:10

txtechhelp


1 Answers

The range of priorities you get in Ubuntu (as well as any other Linux-based OS) for SCHED_OTHER is [0,0] which is a valid range. This means all of these non-realtime processes have the same 'priority' and their only difference is in their 'nice' value.

See the nice system call for more info. I am not sure how OpenBSD's scheduler works, but nice() syscall is in POSIX and it should supported in UNIX systems.

like image 109
Shayan Pooya Avatar answered Oct 12 '22 20:10

Shayan Pooya