QNX的线程调度策略
QNX的线程调度策略有三种方式:SCHED_FIFO, SCHED_RR,SCHED_SPORADIC。默认为SCHED_RR,常用的为SCHED_RR和SCHED_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, ¶m); 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, ¶m); 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时,则完全没有主线程中的打印信息。
《官方文档》
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话