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

From: Gavin Yu (songtaoyu_at_lucent.com)
Date: 11/14/05


Date: Mon, 14 Nov 2005 20:19:37 +0800

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, &param_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, &param_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" <songtaoyu@lucent.com> wrote in message news:dl9u73$fk@netnews.net.lucent.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;
          pthread_attr_setschedparam(&attr_high, &param_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, &param_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" <songtaoyu@lucent.com> wrote in message news:dl9ta0$aac@netnews.net.lucent.com...
>
> 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, &param);
> 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, &param);
> 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);
> }
>
>



Relevant Pages