在Android中使用实时调度(real-time)
Linux的线程调度策略中有FIFO和RT的实时调度方法,但是在Android中做了限制,普通用户不能修改线程的调度算法为FIFO和RT,必须ROOT用户才能更改。但问题是程序是以普通用户启动和运行的,因此没有办法在程序里面设置FIFO调度。只能使用chrt命令手动修改。
要实现在程序中自动设置FIFO,必须弄清楚Android里面做了什么限制?在内核kernel/kernel/sched.c中我们发现了sched_setscheduler的系统调用,其调用了static 函数__sched_setscheduler来实现功能。在此函数中有这么几行:
/*
* Allow unprivileged RT tasks to decrease priority:
*/
if (user && !capable(CAP_SYS_NICE)) {
capable(CAP_SYS_NICE)似乎是权限控制!CAP_SYS_NICE的说明文档如下:
CAP_SYS_NICE
* Raise process nice value (nice(2), setpriority(2)) and
change the nice value for arbitrary processes;
* set real-time scheduling policies for calling process, and
set scheduling policies and priorities for arbitrary
processes (sched_setscheduler(2), sched_setparam(2),
shed_setattr(2));
* set CPU affinity for arbitrary processes
(sched_setaffinity(2));
* set I/O scheduling class and priority for arbitrary
processes (ioprio_set(2));
* apply migrate_pages(2) to arbitrary processes and allow
processes to be migrated to arbitrary nodes;
* apply move_pages(2) to arbitrary processes;
* use the MPOL_MF_MOVE_ALL flag with mbind(2) and
move_pages(2).
http://man7.org/linux/man-pages/man7/capabilities.7.html
至此,问题找到,正是CAP_SYS_NICE禁止了普通用户设置线程的FIFO调度策略,我们把这一部分代码屏蔽后,重新编译内核。sched_setscheduler(SCHED_FIFO)即可正常工作了。当然,被屏蔽的代码中也包含了其它逻辑上的验证,我们可以根据自己的需要酌情修改。
最新文章
- continue break 区别
- C#如何获取本机网络IP地址
- Android Service小记
- php加载xml编码错误,“Error: Input is not proper UTF-8, indicate encoding! ”
- 不用插件直接同步wordpress文章日志到新浪微博
- python 安装扩展库
- mysql 1130 ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server
- 关于use-default-filters的一个问题
- [Webpack 2] Hashing with Webpack for long term caching
- 关于asp.net程序连接不了ORACLE数据库而PL/SQL可以连接的问题
- 武汉科技大学ACM :1003: A+B for Input-Output Practice (III)
- LoadRunner性能测试中Controller场景创建需注意的几点
- 前端angularjs+requirejs+dhtmlx 后端asp.net webapi
- WisKey的眼神
- JAVA中使用JSONObject对象必备
- android调试工具 adb命令学习
- 自己用的Xshell配色方案
- C语言在宏定义中使用语句表达式和预处理器运算符
- HNOI2019 鱼 fish
- PXC中的GTIDs