c – 具有实时优先级的pthreads
我需要管理具有不同优先级的线程池,因此我编写了以下线程启动过程:
static int startup(thrd_t *thrd,thrd_sync_t *sync,int prio) { pthread_attr_t attr; int err; struct sched_param param = { .sched_priority = prio }; assert(pthread_attr_init(&attr) == 0); assert(pthread_attr_setschedpolicy(&attr,SCHED_FIFO) == 0); assert(pthread_attr_setschedparam(&attr,¶m) == 0); err = pthread_create(&thrd->handler,&attr,thread_routine,(void *)thrd); pthread_attr_destroy(&attr); return err; } 原则上,不应允许非特权用户执行此代码:pthread_create()调用应返回EPERM,因为运行具有高优先级的线程具有安全隐患. 出乎意料的是,它适用于普通用户,但它根本不尊重给定的优先级. 我尝试通过删除pthread_attr_t并在创建线程后设置调度属性来修改代码: static int startup(thrd_t *thrd,int prio) { pthread_attr_t attr; int err; struct sched_param param = { .sched_priority = prio }; err = pthread_create(&thrd->handler,NULL /*&attr*/,(void *)thrd); if (err != 0) return err; err = pthread_setschedparam(thrd->handler,SCHED_FIFO,¶m); if (err != 0) return err; return err; } 顺便说一下,这种方法管理起来要困难得多,因为如果出现错误,我需要终止新创建的线程.至少它似乎在权限要求方面正常工作(只有root可以执行此操作),但仍然不遵守优先级. 难道我做错了什么? 编辑 我刚刚添加了以下一段由每个线程执行的代码: static void getinfo () { struct sched_param param; int policy; sched_getparam(0,¶m); DEBUG_FMT("Priority of this process: %d",param.sched_priority); pthread_getschedparam(pthread_self(),&policy,¶m); DEBUG_FMT("Priority of the thread: %d,current policy is: %d and should be %d",param.sched_priority,policy,SCHED_FIFO); } 使用第一种方法(即pthread_attr_t方法),结果是pthread_attr_setschedpolicy完全无效,因为优先级为0且策略不是SCHED_FIFO. 使用第二种方法(即pthread_setschedparam方法),该函数打印预期的数据,但执行仍然以错误的方式运行. 解决方法
我认为您还必须使用pthread_attr_setinheritsched来确保考虑对优先级设置的更改.从手册页:
在手册页中,您还有以下内容:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – 另一种写法:if some_variable \u002
- c# – 为什么在将SQLite Nuget Package添加到LINQPad时会出
- How To Open Physical Standby For Read Write Testing and
- ORACLE - sqlplus查询oracle数据库返回结果为“?”或者乱码
- AJAX基础篇
- Convert Xml to json object in android
- (转)设计模式六大原则(1):单一职责原则
- xml日期2016-01-07T11:33:58.000+08:00和正常日期互转
- C# 操作外部Internet Explorer浏览器
- oracle创建一个数据库三步走