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 @ 2022-09-05 22:04  threegates  阅读(836)  评论(1)    收藏  举报