Custom priority value: is a pthread high sched prio positive or negative?
1
vote
1
answer
8560
views
I’m trying to reimplement in a backward-compatible way the RobotC API from C (although some details would better fit or would be easier with C++), and trying to reimplement their multithreading API in some portable way, so I try to use
sched_get_priority_max(2)
and sched_get_priority_min(2)
. But on one hand nice(1)
and sched(7)
say priority is between -20 (highest priority) and 19 (lower priority), on another hand, the former man pages stated that:
> […] numerically higher priority values are scheduled before
> processes with numerically lower priority values. Thus, the value
> returned by sched_get_priority_max() will be greater than the value
> returned by sched_get_priority_min()
which would mean the opposite: positive value being of higher priority and negative of lower priority (it also gives the example of real-time policies where priority is between 1 and 99).
What is going to be returned by sched_get_priority_max
and _min
? what should I use with pthread_attr_setschedparam(3)
?
I have three values defining my custom priority range: the low priority (kLowPriority
, set to 0), the high one (kHighPriority
, set to 255) and the default one (kDefaultPriority
, set to 7). Ideally I suppose the default being 0 for the scheduling policies having a default, kDefaultPriority
/7 should become 0, kHighPriority
/255 the highest priority (19? 99? whatever sched_get_priority_max(3)
returns) or maybe the highest priority that can be assigned if unpriviledged? and then kLowPriority
either 0 ± kDefaultPriority
, either the lowest priority …what would be the more reasonable?
Currently I think I do it this way:
pthread_attr_t attr;
pthread_t thread;
struct sched_param param;
const int policy = sched_getscheduler(0),
sched_high_prio = sched_get_priority_max(policy), // 19,
sched_low_prio = sched_get_priority_min(policy), // -20,
sched_range_prio = sched_high_prio - sched_low_prio;
pthread_attr_init (&attr);
pthread_attr_getinheritsched(&attr, PTHREAD_INHERIT_SCHED);
pthread_attr_getschedparam (&attr, ¶m);
param.sched_priority = -(((nTaskPriority
- kLowPriority) * sched_range_prio
/ kHighPriority) + sched_low_prio
- kDefaultTaskPriority);
PS: I’m not sure if the best place to ask this question about POSIX API is here or stackoverflow , so I’m doing both, please delete my post or ask me to delete if you think it’s at the wrong place.
EDIT: SO post deleted.
Asked by galex-713
(215 rep)
Mar 25, 2018, 03:39 AM
Last activity: Mar 12, 2022, 02:02 PM
Last activity: Mar 12, 2022, 02:02 PM