0
votes

I have an application running on Linux using SCHED_FIFO via pthread_attr_setschedpolicy.

My understanding is that pthread_attr_setschedparam() should allow setting the thread priority on a thread attributes structure before the thread is launched and Posix requires at least 32 levels are supported.

Indeed while running:

sched_get_priority_max(SCHED_FIFO) = 99
sched_get_priority_min(SCHED_FIFO) = 1

The thread priority is set before pthread_create using:

pthread_attr_setinheritsched(&thread_attr,PTHREAD_EXPLICIT_SCHED))
pthread_attr_setschedparam(&thread_attr)

If I interrogate it

pthread_attr_t thread_attr;
struct sched_param s_param;
int res = pthread_getattr_np(channel_data_p->thread, &thread_attr);
assert(res == 0);
res = pthread_attr_getschedparam (&thread_attr, &s_param);
assert(res == 0);
log_write("thread %d, priority = %d",threadId,s_param.sched_priority);

I see the expected priority. However, I'm not clear how to display the pthreads priority using ps -o, top -H or looking in /proc or how to relate Linux values like PR and NI (if they are the right fields to watch under top -H) back to the posix priority.

I have no intention of messing with the priorities dynamically so this is more of academic interest.

1
When I asked this question there was a bug in the code I was using which forgot to use "pthread_attr_setinheritsched(&thread_attr,PTHREAD_EXPLICIT_SCHED))" meaning the priority had no effect.Bruce Adams

1 Answers

0
votes

The pthreads priority maps directly to the Linux thread/process priority.

This article describes the scheduler quite well (from the book Linux Kernel Development):

http://www.informit.com/articles/article.aspx?p=101760&seqNum=2

As noted here linux schedules threads nowadays and not processes using a 1:1 mapping of threads to processes.