Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I am using RH9-2.4.21 , NPTL version 0.29 I am trying to assign realtime scheduling to the pthreads but facing a strange problem. I am unable to assign the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Location
    Bangalore (INDIA)
    Posts
    3

    problem in assigning real time scheduling to pthread in linux


    Hi,
    I am using RH9-2.4.21 , NPTL version 0.29
    I am trying to assign realtime scheduling to the pthreads but facing a strange problem.
    I am unable to assign the scheduling policy and priority to a thread while creating it.
    after the creation i am able to change its policy and priority (using pthread_setschedparam).
    I am working as root .

    I even tried with PTHREAD_EXPLICIT_SCHED ,( although this is the default behaviour), but of no use

    following is the code :

    #include<stdio.h>
    #include<pthread.h>
    #include<errno.h>
    #include<sched.h>

    void* CaptureThread()
    {
    int my_policy;
    struct sched_param my_param;
    int status;

    status = pthread_getschedparam (pthread_self (), &my_policy, &my_param);
    printf ("# :thread_routine running at %s/%d\n",
    (my_policy == SCHED_FIFO ? "FIFO"
    : (my_policy == SCHED_RR ? "RR"
    : (my_policy == SCHED_OTHER ? "OTHER"
    : "unknown"))),
    my_param.sched_priority);

    }

    pthread_t m_thread;

    int main(void)
    {

    pthread_attr_t thread_attr;
    struct sched_param thread_param;
    pthread_attr_init(&thread_attr);

    pthread_attr_setschedpolicy(&thread_attr, SCHED_RR);
    thread_param.sched_priority = 50;
    pthread_attr_setschedparam(&thread_attr, &thread_param);
    pthread_create(&m_thread, &thread_attr, CaptureThread, NULL);
    pthread_join(m_thread,NULL);

    }

    OUTPUT which i got on running this code :
    # :thread_routine running at OTHER/0

    here i am just trying to assign scheduling policy as RR and priority 50 and creating a pthread.
    Inside the thread function i am printing the sched Policy and priority.
    Ideally it should print policy = RR and priority = 50 ; but instead its printing
    policy = OTHER and priority = 0

    where am i making mistake?

    Thanks

  2. #2
    Just Joined!
    Join Date
    Feb 2007
    Posts
    4

    try this (no error checks)

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <sched.h>


    void *thread_routine (void *arg)
    {
    int my_policy;
    struct sched_param my_param;


    pthread_getschedparam (pthread_self (), &my_policy, &my_param);

    printf ("thread_routine running at %s/%d\n",
    (my_policy == SCHED_FIFO ? "FIFO"
    : (my_policy == SCHED_RR ? "RR"
    : (my_policy == SCHED_OTHER ? "OTHER"
    : "unknown"))),
    my_param.sched_priority);

    return NULL;
    }

    int main (int argc, char *argv[])
    {
    pthread_t thread_id;
    pthread_attr_t thread_attr;
    int thread_policy;
    struct sched_param thread_param;
    int rr_min_priority, rr_max_priority;

    pthread_attr_init (&thread_attr);
    pthread_attr_getschedpolicy (&thread_attr, &thread_policy);
    pthread_attr_getschedparam (&thread_attr, &thread_param);
    printf (
    "Default policy is %s, priority is %d\n",
    (thread_policy == SCHED_FIFO ? "FIFO"
    : (thread_policy == SCHED_RR ? "RR"
    : (thread_policy == SCHED_OTHER ? "OTHER"
    : "unknown"))),
    thread_param.sched_priority);

    pthread_attr_setschedpolicy (&thread_attr, SCHED_RR);
    rr_min_priority = sched_get_priority_min (SCHED_RR);
    rr_max_priority = sched_get_priority_max (SCHED_RR);
    thread_param.sched_priority =
    (rr_min_priority + rr_max_priority)/2;
    printf (
    "SCHED_RR priority range is %d to %d: using %d\n",
    rr_min_priority,
    rr_max_priority,
    thread_param.sched_priority);
    pthread_attr_setschedparam (&thread_attr, &thread_param);

    printf (
    "Creating thread at RR/%d\n",
    thread_param.sched_priority);
    pthread_attr_setinheritsched (&thread_attr, PTHREAD_EXPLICIT_SCHED);
    pthread_create (&thread_id, &thread_attr, thread_routine, NULL);
    pthread_join (thread_id, NULL);
    return 0;
    }

  3. #3
    Just Joined!
    Join Date
    Feb 2007
    Posts
    4

    and in short

    so you basically forgot to add this line before pthread_create
    pthread_attr_setinheritsched (&thread_attr, PTHREAD_EXPLICIT_SCHED);

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2008
    Posts
    1

    confuse about thread preemption

    Hi..

    i think we can simulate preemption by using 2 different priority thread, am i true? I just try these scenario :
    1. Create Thread A with priority 99 , SCHED_FIFO..
    2. Create Thread B with priority 4 , SCHED_FIFO

    Thread A started and doing some busy work.. I guest that Thread B wouldn't start until thread A finished, but I get thread B can run before Thread A finished (It just like a common task switching).. I'm sure my 2 thread has right priority.

    Can anyone help me?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •