Thread Priorities

Thread Priorities

Post by Christin » Wed, 26 Sep 2007 09:37:54


Hi Everybody,

I am working on a 'SunOS 5.9' system. I want to specify thread
priorities. My application creates 2 threads and I want to specify a
relative thread priority between the three threads in the application
(2 threads created using call to pthread_create() and the main
thread). I tried using the calls pthread_attr_getschedparam() and
pthread_attr_setschedparam(&tattr, ¶m).

// pseudo code ........... start ............
main() {
pthread_attr_init(&tattr);
pthread_attr_getschedparam (&tattr, ¶m);
oldprio = param.sched_priority;
param.sched_priority = newprio; // <--- what shd be the value of
this newprio???
pthread_attr_setschedparam (&tattr, ¶m);

pthread_create(tid1, ....);
pthread_create(tid2, .....);

......
}
// pseudo code ........... end ...........

** My question is whether a numerically higher value indicates a
higher priority in the above pthread calls (value of newprio). If I
understand correctly, setting param.sched_priority changes the thread
priority. What is the valid range of the values for this parameter? I
saw the documentation of pthreads as well as the Sun Multithreaded
Programming Guide but couldn't find a satisfactory answer to my
question.

I would really appreciate if anybody could shed some light on these
topics and refer me to some good documentation which answers my
question.

Thanks and Warm Regards,
Christina.
 
 
 

Thread Priorities

Post by Dave Buten » Thu, 27 Sep 2007 04:24:50

hristina wrote:

pthread_t thread_id;
pthread_attr_t thread_attr;
int thread_policy;
struct sched_param thread_param;
int status, rr_min_priority, rr_max_priority;

pthread_attr_init (&thread_attr);
pthread_attr_setschedpolicy (&thread_attr, SCHED_RR);
pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
rr_min_priority = sched_get_priority_min (SCHED_RR);
rr_max_priority = sched_get_priority_max (SCHED_RR);
thread_param.sched_priority = (rr_min_priority + rr_max_priority)/2;
pthread_attr_setschedparam (&thread_attr, &thread_param);
pthread_attr_setinheritsched (&thread_attr, PTHREAD_EXPLICIT_SCHED);
pthread_create (&thread_id, &thread_attr, thread_routine, NULL);

Note that it's important to setinheritsched -- the default isn't
specified by POSIX, but may be PTHREAD_INHERIT_SCHED, which means that
regardless of any values you set in the attributes object a created
thread will instead INHERIT scheduling attributes from the creating
thread. (In a way it would have been nice to have the implementation set
to 'explicit' when you change a scheduling parameter; but the working
group preferred to avoid side effects, so you need to set everything
yourself.)

POSIX specifies that higher-numbered priority values have precedence
over lower values, but it does NOT specify the range. That's what the
sched_getpriority_* routines are for. The code snippet above computes a
mid-range priority value for the standard SCHED_RR schedulinyour
implementation is with respect to which standard. Contention scope, as
one of the standard's scheduling attributes, should be controlled by the
inheritsched -- but initially wasn't explicitly specified to be.
Therefore, some implementations will use the scope you specify
regardless of inheritsched. Implementations can handle either process,
system, or both.