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

05-23 14:53