【问题标题】:Issue with pthread_setschedparam, system hangspthread_setschedparam 问题,系统挂起
【发布时间】:2015-06-15 10:06:06
【问题描述】:
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 的实时优先级并正确查看其设置。但在后期它导致系统挂起。我通过删除此特定功能并为线程分配默认优先级来确认这一点。

如果我在这里遗漏了什么,谁能告诉我?

【问题讨论】:

  • 嗨,Olaf,它不依赖于嵌入式设备/系统,而是在嵌入式设备中实现代码。
  • 嗨,奥拉夫,感谢您的支持。我尝试增加父线程的线程优先级,它解决了这个问题。在我们的设计中,就像线程 2(子线程)通过每 100 毫秒连续轮询来将数据传输到线程 1(父线程)。两者都应该具有更高的优先级,否则我们最终会出现挂起问题。将两个优先级都提高到最大解决了这个问题。
  • 尽管我很想获得这份荣誉,但我相信这不是我要感谢的。由于它显然与嵌入无关,我可能会建议删除“嵌入”标签?只是为了不混淆其他人。

标签: c embedded pthreads posix rtos


【解决方案1】:

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

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

【讨论】:

  • 嗨,咖啡馆,感谢您提供的信息。现在这是有意义的,因为创建的线程(在设置上述策略和优先级之后)有一个循环,该循环有一个轮询调用,每 100 毫秒唤醒一次以从较低级别的驱动程序读取数据。是否有任何替代方案。该线程的主要目的是每 100 秒(如果可用)从较低级别的 USB 数据中收集数据
  • @Murali:你能用显示poll()循环的代码更新你的问题吗?
  • 嗨,Caf,我尝试增加父线程的线程优先级,它解决了这个问题。在我们的设计中,就像线程 2(子线程)通过每 100 毫秒连续轮询来将数据传输到线程 1(父线程)。两者都应该具有更高的优先级,否则我们最终会出现挂起问题。将两个优先级都提高到最大解决了这个问题。感谢您的支持。
【解决方案2】:

我尝试增加父线程的线程优先级,它解决了这个问题。在我们的设计中,它就像线程 2(子线程)通过每 100 毫秒连续轮询将数据传输到线程 1(父线程)。两者都应该具有更高的优先级,否则我们最终会出现挂起问题。将两个优先级都提高到最大解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多