Embedded Linux >> Thread priority scheduling across linux kernels.

by jyoti.wagholikar » Sun, 29 Aug 2004 20:55:10 GMT

Hi,

I have come across some strange behaviour of
priority base scheduling of threads across linux
kernels.

The program below shows that main priority is
raised to max = 99. Another thread with priority =30
is created.

void main()
{
struct sched_param schedparam, getparam,
taskschedparam;
int policy, status;
pthread_attr_t attribs;

pthread_t id =pthread_self();

/* Raising main priority to max =99 */
schedparam.__sched_priority
=sched_get_priority_max( SCHED_FIFO );
pthread_setschedparam(id, SCHED_FIFO, &schedparam);
pthread_getschedparam(id,&policy , &getparam);
printf("\n main : priority = %d, policy = %d",
getparam.__sched_priority, policy);

/* First Assign default attributes for the thread */
pthread_attr_init(&attribs);

/*Set stack size as specified by user*/
attribs.__stacksize = 10000;

/*set scheduling policy*/
pthread_attr_setschedpolicy(&attribs, SCHED_FIFO);


/*Set task priority*/
taskschedparam.__sched_priority = 30;
pthread_attr_setschedparam(&attribs, &taskschedparam);

status = pthread_create(&firstTask, &attribs,
task_fun1, (void*)1 );
printf("\n main :first task = %d,firstTask);
fflush(stdout);

sleep(10000);
}
void *task_fun1 ( void *param)
{
struct sched_param schedparam, getparam ;
int policy;

pthread_getschedparam(pthread_self(),&policy ,
&getparam);
printf("\n task_fun1: priority = %d, policy = %d",
getparam.__sched_priority, policy);fflush(stdout);
printf("\n FIRST TASK = %x", firstTask);
fflush(stdout);
}

Ouputs:
Redhat: 7.2 :[CORRECT OUTPUT]
main : priority = 99, policy = 1
pthread_create status = 0
main :first task = 1026
Sleeping for 1000 sec
task_fun1: priority = 30, policy = 1
FIRST TASK = 402


Redhat: 9.0
main : priority = 99, policy = 1
task_fun1: priority = 0, policy = 0
FIRST TASK = 40838cc0
pthread_create status = 0
main :first task = 1082363072
Sleeping for 1000 sec

Just wondering if there is any inconsistency in the
priority scheduling across linux version: linux
2.4.20-8(Redhat 9.0) linux 2.4.7(Redhat 7.2).

Has anyone come across this problem earlier? Any
solution to overcome it?

Your input will be helpful.

thanks and regards,
-Jyoti


Embedded Linux >> Thread priority scheduling across linux kernels.

by Dan Kegel » Mon, 30 Aug 2004 14:13:58 GMT





Sure, there have been lots of changes. Threads in particular
changed; Red Hat 9 uses NPTL, which is a totally different
interface than the traditional LinuxThreads. Also
I seem to recall that new processes/threads start up
much sooner on recent kernels.

So what's the problem? You haven't explained why the
new behavior is better or worse for you.
Basically, if you want your program to run across
many versions of Linux, you're going to have to
accept some variations.
- Dan



Similar Threads

1. Thread priority scheduling across linux kernels RH9.0 and RH 7.2. - Linux

2. Thread Priority ?? in RealTime Scheduling(pTreads)

   Hi everybody,

                     Could you help me please.....
 Field       :  i am tried to set a thread priority with SCHED_FIFO and
SCHED_RR.
Suitutaion : My application program create three threads, one with
SCHED_FIFO ,another with SCHED_FIFO
                and last SCHED_OTHER..... all three threads are in
infinite loop.
                i use to set priorities for each thread 99**,1,0
repectively.(success return in pthread_setschedparam)

                                        ** Note : pthread higher value
has higher priority)
                i used to print microseconds repeatedly in
threads......... but all threads executing same time......
 Question:
                what will be the execution flow of the above
suitution.....
                         will first thread (has higher priority) will
entirely occupies the CPU or give CPU to othres ,if yes  how ???

3. set thread priority of threads produced with kernel_thread() in kernel mode - Embedded Linux

4. Scheduling a higher priority thread returning from clock_nanosleep()

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

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

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);
}


7. reliable way to set a kernel thread priority

8. Setting kernel thread priority