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