QNX的线程调度策略

QNX的线程调度策略有三种方式:SCHED_FIFOSCHED_RR,SCHED_SPORADIC。默认为SCHED_RR,常用的为SCHED_RRSCHED_FIFO。

根据官方文档的介绍,可以大致理解两者的区别是:SCHED_RR首先看优先级,同一优先级下,多个线程轮流调度,不会让一个线程长时间独占CPU资源。SCHED_FIFO则是同优先级下线程执行,直到一个可以调度点才退出占用资源。

因此写了一段程序验证:

复制代码
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <gulliver.h>
static int thread_sched_policy()
{
    struct sched_param param;
    int policy, retcode;
    /* Get the scheduling parameters. */
    retcode = pthread_getschedparam( pthread_self(), &policy, &param);
    if (retcode != EOK) {
    printf ("pthread_getschedparam: %s.\n", strerror (retcode));
    return EXIT_FAILURE;
    }
    printf ("The assigned priority is %d, and the current priority is %d,policy is %d\n",
    param.sched_priority, param.sched_curpriority, policy);
    /* Increase the priority. */
//    param.sched_priority++;
    policy = SCHED_RR;
    retcode = pthread_setschedparam( pthread_self(), policy, &param);
    if (retcode != EOK) {
    printf ("pthread_setschedparam: %s.\n", strerror (retcode));
    return EXIT_FAILURE;
    }
}

void* thread_test_func2(void* arg)
{
    thread_sched_policy();
    int count = 2500000000;
    while(count>0)
    {
        count-- ;
    }
}
int testthreadschedpolicy()
{
    pthread_t tid=0;
    pthread_create(&tid, NULL, thread_test_func2, NULL);
    return 0;
}

int main(int argc, char *argv[]) {
    printf("Welcome to the QNX Momentics IDE\n");

    if(1)
        testthreadschedpolicy();

    printf("test ok!");
    while(1)
    {
        static int count = 0;
        printf("main thread %d!\n", ++count);
        sleep(5);
    }
    return EXIT_SUCCESS;
}
复制代码

测试程序有个2个线程,当子线程的调度策略为SCHED_RR时,console中有主线程中printf的打印信息;当子线程的调度策略为SCHED_FIFO时,则完全没有主线程中的打印信息。

官方文档

posted @   threegates  阅读(742)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示