pthread RR and FIFO priority not working - Linux

This is a discussion on pthread RR and FIFO priority not working - Linux ; hi , i am using RH9-2.4.21 , NPTL version 0.29. I am unable to set the policy and priority of any pthread while creating it. after creating the thread , i am able to change its scheduling policy and priority. ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: pthread RR and FIFO priority not working

  1. pthread RR and FIFO priority not working

    hi ,
    i am using RH9-2.4.21 , NPTL version 0.29.
    I am unable to set the policy and priority of any pthread while
    creating it. after creating the thread , i am able to change its
    scheduling policy and priority.
    I am working as root.

    I am writing a very simple program-- just creating a thread, assigning
    it RR scheduling policy and priority =50.

    In the created thread, I am checking its scheduling policy and priority

    (using pthread_getschedparam ) and printing it.

    It should print schedule policy RR and priority 50 ; but instead its
    printing scheduling policy SCHED_OTHER and priority = 0.

    following is my code :

    #include
    #include
    #include
    #include


    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);
    /* setting the Sched policy */
    thread_param.sched_priority = 50;

    pthread_attr_setschedparam(&thread_attr, &thread_param);
    /* setting the Priority */
    pthread_create(&m_thread, &thread_attr, CaptureThread, NULL);
    pthread_join(m_thread,NULL);

    }


    OUTPUT which i am getting :

    # :thread_routine running at OTHER/0

    Please guide me where i am making a mistake.

    how to set the scheduling policy and priority of a thread while
    creating it?

    if I assign the policy and priority inside the created thread, its
    working fine :

    void* CaptureThread()
    {
    int my_policy;
    struct sched_param my_param;
    int status;
    my_param.sched_priority = 50;
    status = pthread_setschedparam (pthread_self (), SCHED_RR,
    &my_param);
    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);


    }

    OUTPUT i am getting in this case :
    # :thread_routine running at RR/50

    where am i going wrong?


    Thanks,
    Jasdeep


  2. Re: pthread RR and FIFO priority not working

    Hello Jasdeep,

    > i am using RH9-2.4.21 , NPTL version 0.29.


    First consider to upgrade your NPTL version. That one is known to have
    some bugs. See:
    https://rhn.redhat.com/errata/RHBA-2003-136.html


    > I am unable to set the policy and priority of any pthread while
    > creating it. after creating the thread , i am able to change its
    > scheduling policy and priority.
    > I am working as root.


    My guess, without reading further: you forgot to set the pthread
    attribute "inheritsched" to PTHREAD_EXPLICIT_SCHED. Otherwise the
    thread inherits the scheduling/policy attribute from the thread calling
    the /pthread_create()/. That's a classical trap when you want to start
    a thread with different scheduling/priority[1]...


    > following is my code :
    >
    > #include
    > #include
    > #include
    > #include
    >




    > 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);
    > /* setting the Sched policy */
    > thread_param.sched_priority = 50;
    >
    > pthread_attr_setschedparam(&thread_attr, &thread_param);


    add the following lines here:
    rc = pthread_attr_setinheritsched(&thread_attr,
    PTHREAD_EXPLICIT_SCHED);
    if (rc!=0) {
    /* deals with the error */
    }

    > /* setting the Priority */
    > pthread_create(&m_thread, &thread_attr, CaptureThread, NULL);
    > pthread_join(m_thread,NULL);
    >
    > }


    Please note that you should check the return value of the Pthreads
    functions. Especially when dealing with policy/priority attributes, you
    should check that the /pthread_create()/ has been successful.

    Cheers,
    Loic.
    --
    [1] I didn't made the mistake the 1st time, as I was following the
    book... But I got trapped by that mistake too a few months later...


+ Reply to Thread