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:46:29



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:51:55

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:dl9tdi$ 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>
<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:dl9tdi$ XXXX@XXXXX.COM "><FONT face=Arial
size=2>news:dl9tdi$ 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>>
===============================================================<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 20:59:35

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:dl9tdi$ 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>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=Arial size=2>      &nb
 
 
 

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:18:57

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:dl9u67$ 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 r.e.ballar » Tue, 15 Nov 2005 22:36:51

Printf uses buffered writes, which means that the threads will each
create their own buffers and only the "write" during the flush of the
printf buffer will be sent. Because your messages are so short, you
will have perhaps 10-20 messages being batched up then flushed from one
thread, then 10-20 messages from the other thread.

you might want to open a pipe and use write calls instead. You can
also use message queues or open the file for write but without lock.
The problem with the file write without lock is that there may be
internal buffers within the application library and kernel module (like
a driver component).

Pipes, udp sockets, and message queues are less likely to be buffered
in this way.

You can also use fflush, fsync, and sync commands as well.
 
 
 

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

Post by Linu » Tue, 15 Nov 2005 23:27:30

After takin' a swig o' grog, Gavin Yu belched out this bit o' wisdom:


No wonder Lucent is in the crapper.

--
Treat yourself to the devices, applications, and services running on the
GNU/Linuxoperating system!
 
 
 

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

Post by John A. Ba » Wed, 16 Nov 2005 03:19:53


How about if he did something graphical using SDL.

Would that be as /instant/ as possible...in terms of some type of response?