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

  • 相关阅读:
    iBatis查询时报“列名无效”无列名的错误原因及解决方法
    【转】Spring结合Quartz实现多任务定时调用
    关于jar中读取包内和包外文件
    【摘自百度文库】数据库水平切分的实现原理解析
    web.xml 中的listener、 filter、servlet 加载顺序及其详解(转载)
    EL表达式
    【转】使用XFire+Spring构建Web Service
    慎用href="javascript:void(0)"
    POI导出EXCEL【摘自:oschina.net】
    【转】IBM websphere6.1 不支持泛型、intInteger类型的自动装箱和拆箱问题
  • 原文地址:https://www.cnblogs.com/swnuwangyun/p/3807723.html
Copyright © 2020-2023  润新知