tags:

views:

453

answers:

1

On an embedded system (Linux kernel 2.6.28 on ARM processor using glibc 2.6.1) I am running an application consisting of multiple threads. I would like one of those threads to get more CPU time than the others.

One option for setting priorities seems to be to use pthread_setschedparam with SCHED_RR (or SCHED_FIFO), however this gives the thread quite too much CPU (unless it sleeps, it eats away all CPU from its fellow threads which are still using SCHED_OTHER).

Another option would be setting the nice level of the thread. However while this would be exactly what I was looking for (the thread just gets a bit more CPU but can't starve the other threads), I can't get it to work correctly. According to the man pages "threads do not share a common nice value", which I interpret as that I can set individual nice values to different threads in the same process.

Here's my code:

pid_t tid;
tid = syscall(SYS_gettid);

int ret = setpriority(PRIO_PROCESS, tid, priority);

However it seems that tid is the same among all threads. Also setting individual nice-levels for thread will still return the latest set nice level in all threads when calling getpriority. From the performance this is consistent (i.e. run thread A and thread B with the same task, then set A to -19 and B to 19 and both will finish roughly the same time).

Yet, when running thread A, then setting it to -19 and then running B and setting it to +19 will have B run at full power (finishing way ahead of A).

So it seems like a thread will use the nice level set at the time it is run with no way of changing it afterwards.

I've googled like crazy for more information or any kind of confirmation, but all I found so far is rather vague. Does anybody here have any description available on how nice levels for threads are handled on Linux (2.6.28, glibc 2.6.1)? And how I possibly could change the nice level of an already running thread?

A: 

Errm, once I posted this question I suddenly realized my mistake - I set the nice level by calling the function from the main thread, so of course they all have the same thread ID (as it is executed in the context of the main thread). DOH!!!!

Using the correct tid everything works fine.

Now how do I delete this question?

I think it is fine to leave it as is, Steven. I guess people would have already closed it or downvote it.
yves Baumes
Btw you can't delete your own question , as I see you don't have enough reps from now. See the faq.
yves Baumes