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)即可正常工作了。当然,被屏蔽的代码中也包含了其它逻辑上的验证,我们可以根据自己的需要酌情修改。

最新文章

  1. continue break 区别
  2. C#如何获取本机网络IP地址
  3. Android Service小记
  4. php加载xml编码错误,“Error: Input is not proper UTF-8, indicate encoding! ”
  5. 不用插件直接同步wordpress文章日志到新浪微博
  6. python 安装扩展库
  7. mysql 1130 ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server
  8. 关于use-default-filters的一个问题
  9. [Webpack 2] Hashing with Webpack for long term caching
  10. 关于asp.net程序连接不了ORACLE数据库而PL/SQL可以连接的问题
  11. 武汉科技大学ACM :1003: A+B for Input-Output Practice (III)
  12. LoadRunner性能测试中Controller场景创建需注意的几点
  13. 前端angularjs+requirejs+dhtmlx 后端asp.net webapi
  14. WisKey的眼神
  15. JAVA中使用JSONObject对象必备
  16. android调试工具 adb命令学习
  17. 自己用的Xshell配色方案
  18. C语言在宏定义中使用语句表达式和预处理器运算符
  19. HNOI2019 鱼 fish
  20. PXC中的GTIDs

热门文章

  1. Python通过fork的方式防止僵尸进程
  2. 软件磁盘阵列(RAID)
  3. MVC4 中使用 Area 和 注意的地方
  4. position:absolute width
  5. swift的值类型和引用类型
  6. LeetCode: Keyboard Row
  7. PHP领域类型Java中ibatis的API
  8. Chrome 的 Rendering 监听器
  9. P3437 [POI2006]TET-Tetris 3D
  10. INSPIRED启示录 读书笔记 - 第2章 产品管理与产品营销