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


上面的代码用于设置线程优先级和创建线程2,这导致我的系统在某些时候挂起。

我建立了一个telnet会话,并使用“ ps -ef”命令检查线程2的实时优先级并正确查看其设置。但在稍后阶段,其导致系统挂起。我通过删除此特定功能并为线程分配了默认优先级来确认这一点。

有人可以让我知道我是否在这里遗漏了什么吗?

最佳答案

您正在线程上设置实时调度策略。这意味着只有优先级更高的实时任务才能抢占它,因此,如果您的线程进入一个无休止(或只是长时间运行)的循环,在该循环中它消耗CPU且没有阻塞,则它将使其他非实时任务饿死。

不阻塞的计算繁重的线程可能不应被赋予实时调度策略。

09-10 00:43
查看更多