Android中的即時(real-time)調度

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.