Android Nice's role in process and thread scheduling

Source: Internet
Author: User

Basic Concepts:

Process: The basic unit of the resource allocation and height of a computer, the entity that the program runs, and the container of the thread.

Threads: The unit that is actually executing in a process, the smallest unit of program execution, and belongs to a process.

A process can have more than one thread.


Nice 's role in process debugging

In Android, you can use the Nice value to set the priority of a process, the system scheduler can be based on the nice value of the reasonable scheduling process, the main features are as follows: In Android, Nice's range is -20~19 in Android, the nice value has the default size of 0 In Android, the greater the nice value, the lower the priority of the process, the fewer CPU calls you get; the higher the Nice value, the higher the priority of the process, the more CPU calls you get in Android, the fork nice value of the parent process is the same as the parent process, After you process Renice, the nice value of the subprocess does not change


How to change the process's nice value

The following command can be used in Android to change the process's nice value, Usage:nice [-N PRIORITY] command [args ...]

Run a command line at a increased or decreased scheduling priority.

Higher numbers make a program yield more CPU time, from-20 (highest
Priority) to (lowest). By default processes inherit their parent ' s
Niceness (usually 0). By default this command adds to the parent ' s
Priority. Only root can set a negative niceness level.


Usage:renice [[R] [-T TYPE] priority PIDs ...] [G-PID]

Where the nice command sets the priority of a newly created process, the Renice command is used to change the priority of the process that has been created, and both commands need to run under root permissions.


Thread Scheduling

In Android development, we use the most of the control of thread priority, but in Android, there are two versions of thread priority control: the Java version and the Android version, the two versions of the main differences between the priority of the following: The precedence in the Java version uses setpriority (int priority) and getpriority () in Java.lang.Thread and obtains the priority of the thread, which includes only three types of thread precedence, which is coarse-grained control, as follows:

Public static Final intmax_priority = Ten Public static Final intmin_priority = 5 Public static Final intmin_priority = 1

The thread priority in the Android version uses the setthreadpriority (int priority) and getpriority () in the Android.os.Process class to set and get the thread's priority, which includes the 10-thread priority, The finer-grained control strategy relative to Java is as follows:

   

Therefore, it is recommended that you use the Android version of the API when setting and getting thread priority. The Android API's thread priority and Java Native API priority are relatively independent, such as using android.os.Process.setThreadPriority (process.thread_priority_ BACKGROUND), the value obtained using the Java Native api,thread.getpriority () does not change.

Because of the difference in the 2nd, it is necessary to pay special attention to the analysis of the ANR log, in the following ANR log information, prio=5 the corresponding Java Native API thread priority in the Proi value. And the nice=-6 in Nice represents the thread priority of the Android API version.

Main prio=5 tid=1 NATIVE
  | group= "main" scount=1 dscount=0 obj=0x41690f18 self=0x4167e650
  | sys tid=1765 nice=-6 sched=0/0 cgrp=apps handle=1074196888
  | state=s schedstat= (0 0 0) utm=5764 stm=3654 core=2   #00   PC 00022624 /system/lib/libc.so (__futex_syscall3+8)
  #01   PC 0000f054 /sy Stem/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02   PC 0000f0b4 /system/lib/libc.so (_ _pthread_cond_timedwait+64)

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.