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)即可正常工作了。當然,被屏蔽的代碼中也包含了其它邏輯上的驗證,我們可以根據自己的需要酌情修改。