Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Gavin Y » Tue, 15 Nov 2005 20:44:35



Hi,

I wrote one simple program to test the functionality of the policy for
SCHED_RR
and SCHED_FIFO one linux kernel 2.6. I created two threads, one with the
priority
42 and the other with the priority 41.The two threads execute the same
function, which just
prints "printf(....") statements all the time.

In theory, only when the first has finish its execution, the second could
get
the chance to run. But unfortunately,the result is very confusing: the
second
can run even if the first thread is still running.

I complied the program as "gcc test.c -lpthread"
Below is my codes:
===============================================================
#include <pthread.h>
#include <stdio.h>

void* dowork(void* idp)
{
int i;
int *my_id = (int*) idp;

printf("Thread %d is starting up\n", *my_id);
sleep(2);

while(1)
{
printf("Thread %d is doing work. \n", *my_id);
}

}

int main (int argc, char* argv[])
{
int i;
pthread_t threads[2];
int thread_ids[2] = {0, 1};
pthread_attr_t attr_high, attr_low;
struct sched_param param_high, param_low;

pthread_attr_init(&attr_high);
pthread_attr_init(&attr_low);
pthread_attr_setinheritsched(&attr_high, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_high, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_low, PTHREAD_CREATE_JOINABLE);

/* set thread priority 42 and scheduling policy SCHED_RR for high
thread */
param_high.sched_priority = 42;
pthread_attr_setschedparam(&attr_high, ¶m);
pthread_attr_setschedpolicy(&attr_high, SCHED_RR);

pthread_create(&threads[0], &attr_high, dowork, &thread_ids[0]);

/* set thread priority 41 and scheduling policy SCHED_RR for high
thread */
param_high.sched_priority = 41;
pthread_attr_setschedparam(&attr_low, ¶m);
pthread_attr_setschedpolicy(&attr_low, SCHED_RR);

pthread_create(&threads[1], &attr_low, dowork, &thread_ids[1]);

for (i = 0; i < 2; i++)
{
pthread_join(threads[i], NULL);
}

printf ("Main(): Waited on 2 threads. Done.\n");

/* Clean up and exit */
pthread_attr_destroy(&attr_high);
pthread_attr_destroy(&attr_low);
pthread_exit(NULL);
}
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Gavin Y » Tue, 15 Nov 2005 20:52:11

his is a multi-part message in MIME format.


Sorry for my typo in my codes:
====================================================
/* set thread priority 41 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 41;
pthread_attr_setschedparam(&attr_low, ¶m);
pthread_attr_setschedpolicy(&attr_low, SCHED_RR);
......................
===================================================
The right one should be:
param_low.sched_priority = 41;

"Gavin Yu" < XXXX@XXXXX.COM > wrote in message news:dl9ta0$ XXXX@XXXXX.COM ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2800.1523" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2></FONT>
<DIV><FONT face=Arial size=2>Sorry for my typo in my codes:</FONT></DIV>
<DIV><FONT face=Arial
size=2>====================================================</FONT></DIV>
<DIV><FONT face=Arial size=2>     /* set thread priority 41
and scheduling policy SCHED_RR for high thread
*/<BR>        <FONT color=#0000ff>
</FONT><FONT color=#ff0000>param_high.sched_priority =
41;<BR></FONT>        
pthread_attr_setschedparam(&attr_low,
&param);<BR>        
pthread_attr_setschedpolicy(&attr_low,
SCHED_RR);<BR>.....................</FONT></DIV>
<DIV><FONT face=Arial
size=2>===================================================</FONT></DIV>
<DIV><FONT face=Arial size=2>The right one should be:</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2>param_low.sched_priority =
41;<BR></FONT></DIV></DIV>
<DIV><FONT face=Arial size=2>"Gavin Yu" <</FONT><A
href="mailto: XXXX@XXXXX.COM "><FONT face=Arial
size=2> XXXX@XXXXX.COM </FONT></A><FONT face=Arial size=2>> wrote in
message </FONT><A href="news:dl9ta0$ XXXX@XXXXX.COM "><FONT face=Arial
size=2>news:dl9ta0$ XXXX@XXXXX.COM </FONT></A><FONT face=Arial
size=2>...</FONT></DIV><FONT face=Arial size=2>> <BR>> Hi,<BR>>
<BR>> I wrote one simple program to test the functionality of the policy
for<BR>> SCHED_RR<BR>> and SCHED_FIFO one linux kernel 2.6. I created two
threads, one with the<BR>> priority<BR>> 42 and the other with the
priority 41.The two threads execute the same<BR>> function, which
just<BR>> prints "printf(....") statements all the time.<BR>> <BR>> In
theory, only when the first has finish its execution, the second could<BR>>
get<BR>> the chance to run. But unfortunately,the result is very confusing:
the<BR>> second<BR>> can run even if the first thread  is still
running.<BR>> <BR>> I complied the program as "gcc test.c
-lpthread"<BR>> Below is my codes:<BR>>
=============================
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Gavin Y » Tue, 15 Nov 2005 21:00:03

his is a multi-part message in MIME format.


There are too many typo errors in my former mail, I re-copy as below:
==================================================================
#include <pthread.h>
#include <stdio.h>

void* dowork(void* idp)
{
int i;
int *my_id = (int*) idp;

printf("Thread %d is starting up\n", *my_id);
sleep(2);

while(1)
{
printf("Thread %d is doing work. \n", *my_id);
}

}


int main (int argc, char* argv[])
{
int i;
pthread_t threads[2];
int thread_ids[2] = {0, 1};
pthread_attr_t attr_high, attr_low;
struct sched_param param_high, param_low;

pthread_attr_init(&attr_high);
pthread_attr_init(&attr_low);
pthread_attr_setinheritsched(&attr_high, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_high, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_low, PTHREAD_CREATE_JOINABLE);

/* set thread priority 42 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 42;
pthread_attr_setschedparam(&attr_high, ¶m_high);
pthread_attr_setschedpolicy(&attr_high, SCHED_RR);

pthread_create(&threads[0], &attr_high, dowork, &thread_ids[0]);

/* set thread priority 41 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 41;
pthread_attr_setschedparam(&attr_low, ¶m_low);
pthread_attr_setschedpolicy(&attr_low, SCHED_RR);

pthread_create(&threads[1], &attr_low, dowork, &thread_ids[1]);

for (i = 0; i < 2; i++)
{
pthread_join(threads[i], NULL);
}

printf ("Main(): Waited on 2 threads. Done.\n");

/* Clean up and exit */
pthread_attr_destroy(&attr_high);
pthread_attr_destroy(&attr_low);
pthread_exit(NULL);
}
"Gavin Yu" < XXXX@XXXXX.COM > wrote in message news:dl9ta0$ XXXX@XXXXX.COM ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2800.1523" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2></FONT>
<DIV><FONT face=Arial size=2>There are too many typo errors in my former mail, I
re-copy as below:</FONT></DIV>
<DIV><FONT face=Arial
size=2>==================================================================</FONT></DIV>
<DIV><FONT face=Arial size=2>#include <pthread.h><BR>#include
<stdio.h></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>void* dowork(void*
idp)<BR>{<BR>        int
i;<BR>        int *my_id = (int*)
idp;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>       
printf("Thread %d is starting up\n",
*my_id);<BR>        sleep(2);</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=A
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Gavin Y » Tue, 15 Nov 2005 21:19:37

his is a multi-part message in MIME format.


My god, what is wrong for myself???? I have make the same mistake for the second time!!!!!!!!!!!!!!!
Below is the final version for my code:
========================================================================
include <pthread.h>
#include <stdio.h>

void* dowork(void* idp)
{
int i;
int *my_id = (int*) idp;

printf("Thread %d is starting up\n", *my_id);
sleep(2);

while(1)
{
printf("Thread %d is doing work. \n", *my_id);
}

}


int main (int argc, char* argv[])
{
int i;
pthread_t threads[2];
int thread_ids[2] = {0, 1};
pthread_attr_t attr_high, attr_low;
struct sched_param param_high, param_low;

pthread_attr_init(&attr_high);
pthread_attr_init(&attr_low);
pthread_attr_setinheritsched(&attr_high, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_high, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_low, PTHREAD_CREATE_JOINABLE);

/* set thread priority 42 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 42;
pthread_attr_setschedparam(&attr_high, ¶m_high);
pthread_attr_setschedpolicy(&attr_high, SCHED_RR);

pthread_create(&threads[0], &attr_high, dowork, &thread_ids[0]);


/* set thread priority 41 and scheduling policy SCHED_RR for low thread */
param_low.sched_priority = 41;
pthread_attr_setschedparam(&attr_low, ¶m_low);
pthread_attr_setschedpolicy(&attr_low, SCHED_RR);

pthread_create(&threads[1], &attr_low, dowork, &thread_ids[1]);

for (i = 0; i < 2; i++)
{
pthread_join(threads[i], NULL);
}

printf ("Main(): Waited on 2 threads. Done.\n");

/* Clean up and exit */
pthread_attr_destroy(&attr_high);
pthread_attr_destroy(&attr_low);
pthread_exit(NULL);
}

"Gavin Yu" < XXXX@XXXXX.COM > wrote in message news:dl9u73$ XXXX@XXXXX.COM ...
There are too many typo errors in my former mail, I re-copy as below:
==================================================================
#include <pthread.h>
#include <stdio.h>

void* dowork(void* idp)
{
int i;
int *my_id = (int*) idp;

printf("Thread %d is starting up\n", *my_id);
sleep(2);

while(1)
{
printf("Thread %d is doing work. \n", *my_id);
}

}


int main (int argc, char* argv[])
{
int i;
pthread_t threads[2];
int thread_ids[2] = {0, 1};
pthread_attr_t attr_high, attr_low;
struct sched_param param_high, param_low;

pthread_attr_init(&attr_high);
pthread_attr_init(&attr_low);
pthread_attr_setinheritsched(&attr_high, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_high, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_low, PTHREAD_CREATE_JOINABLE);

/* set thread priority 42 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 42;
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Ik5pbHMgTy » Tue, 15 Nov 2005 22:24:36


Check the return value of *all* your calls, so you'll atleast
know if they fail - e.g. because the various attributes arn't
implemented on your system, or you don't have proper permissions
to set them, etc.
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Loic Domai » Wed, 16 Nov 2005 07:05:34

Hello Gavin,


This is not necessarily true, if your box has for instance more than one
processor...

Regards,
Lo.
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Andy MacLe » Wed, 16 Nov 2005 08:35:58


I don't think Linux lets you set SCHED_RR or SCHED_FIFO scheduling or
use realtime priorities unless you're root. Perhaps you're
running as a normal user and your scheduler settings are being ignored?

You can use ps to look at the scheduler settings for the threads of your
program to see if they're what they should be:

ps -p <whatever process ID> -m -o pid,cls,rtprio,command


A.
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Florian Sc » Wed, 23 Nov 2005 08:45:38


Hehm, any good newsreader can copy and paste. anyways. checking return
values of the function calls you make might be a _very good_ idea.

Flo

P.S.: Some version of NPTL ignored attributes set before thread creation.
Namely 0.60

getconf GNU_LIBPTHREAD_VERSION

should show you

--
Palimm Palimm!
http://www.yqcomputer.com/
 
 
 

Thread Priority setting for SCHED_RR and SCHED_FIFO does not work as expected on Linux Kernel 2.6

Post by Florian Sc » Wed, 23 Nov 2005 08:47:04


Not only did libc fail to use the attributes, it also did so without
reporting an error. Which was causing quite a bit of problems during the
"NPTL hell" days of linux audio :)

--
Palimm Palimm!
http://www.yqcomputer.com/