Issue with pthread_setschedparam, system hangs

1.2k views Asked by At
static int pthrd_setthread_prio(int thred_prio)
{
    int                        thrd_policy = SCHED_RR;
    struct sched_param         thr_prio;
    int                        res=0;

    thr_prio.sched_priority = thred_prio;

    /* Try setting the thread/process priority via pthread */
    res = pthread_setschedparam(  pthread_self(), 
                                 thrd_policy,
                                 (const struct sched_param*) &thr_prio);

    //pthread_setschedparam has two return values 0 on successs, >0 on failure
    if(res != 0)
    {
       deb_err("pthread_setschedparam failed withe error %d\n",res);
    }

   res = pthread_getschedparam( pthread_self(), 
                                &thrd_policy,
                                &thr_prio);
    if(res < 0)
    {
       deb_err("pthread_getschedparam failed\n");
    }

    printf("Thread policy %s priority %d process id %ld\n", \
                   ( (thrd_policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                     (thrd_policy == SCHED_RR)    ? "SCHED_RR" :
                     (thrd_policy == SCHED_OTHER) ? "SCHED_OTHER" : "???"),   thr_prio.sched_priority,   syscall(SYS_gettid));

    if ( (thr_prio.sched_priority != thred_prio) || (thrd_policy != SCHED_RR) )
    {
       deb_err("Thread priority ==  %d, this should be %d ERROR! using pthread\n",thr_prio.sched_priority ,thred_prio);
       res=-1;
    }

    return (res);
}

With above code used for setting thread priority and creating thread-2 is causing my system to hang at certain point.

I established a telnet session and used "ps -ef" command to check real time priority of thread-2 and see its set correctly. But at later stage its causing system to hang. I confirmed this by removing this particular function and assigned default priority to thread.

Could anyone please let me know if I am missing anything here ?

2

There are 2 answers

3
caf On

You're setting a realtime scheduling policy on your thread. This means that only higher priority realtime tasks can preempt it, so if your thread enters an endless (or just very-long-running) loop where it is consuming CPU and not blocking, it will starve out other non-realtime tasks.

Calculation-heavy threads that don't block should probably not be given a realtime scheduling policy.

0
Murali Marimekala On

I tried increasing the thread priority of parent thread and it solved the issue. In our design, its like thread2 (child thread) transfer data to thread1 (parent thread) by polling continously for every 100msec. Both should be of higher priority otherwise, we are ending up hang issue. Increasing both priority to max resolved this issue.