Real-time (real-time) scheduling in Android

Source: Internet
Author: User

Linux Thread scheduling strategy has FIFO and RT Real-time scheduling method, but in Android restrictions, ordinary users can not modify the thread scheduling algorithm for FIFO and RT, must be the root user to change. However, the problem is that the program is started and run by ordinary users, so there is no way to set the FIFO scheduling inside the program. Can only be modified manually using the Chrt command.

To implement automatic FIFO setup in your program, you have to figure out what restrictions are being made in Android? In the kernel kernel/kernel/sched.c we found the Sched_setscheduler system call, which called the static function __sched_setscheduler to implement the function. There are a few lines in this function:

/*
* Allow unprivileged RT tasks to decrease priority:
*/
if (user &&!capable (cap_sys_nice)) {

Capable (cap_sys_nice) seems to be permission control! The documentation for Cap_sys_nice is as follows:

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 being 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

At this point, the problem is found, it is cap_sys_nice prohibit the normal user set the thread FIFO scheduling policy, we block this part of the code, recompile the kernel. Sched_setscheduler (SCHED_FIFO) can work properly. Of course, the masked Code also contains other logical validations that we can modify as appropriate to our needs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.