In computer operating systems, processes are the basic units for resource allocation and scheduling, and each process can also contain multiple threads. So in the Android system (Linux Kernel), how do processes grab resources and how do threads switch based on priority? This article will try to analyze this issue and study the application of nice in Linux and Android systems.
Some concepts
Nice and process scheduling In Linux, the nice value (hereinafter referred to as the nice value) is used to set the priority of a process, and the system task scheduler arranges the scheduling reasonably according to the nice value.
Etymology There is almost no information about the origin of the command nice, so I tried to infer it myself. The meanings given in dictionaries such as Ciba and Hujiang are all good; beautiful; lovely; kind, friendly. However, Youdao Dictionary gives a slightly different meaning than other dictionaries, which is kind. I personally think that the one given by Youdao is more reasonable. To be kind, you need to be humble, so you need to sacrifice yourself a little to help others. So the higher the nice value, the kinder you are, but the lower your priority will be. renice For a new process, we can set the nice value for a process according to the following code. nice -n 10 adb logcat For the created process, we can use renice to modify the nice value su do renice -n 0 -p 24161 This command requires root privileges, and the value corresponding to -p is the process id. Note that the -n value of the renice command in Linux distributions should be the target priority of the process. On Mac, -n represents the increase in the current privilege. For example, on Mac, to change the nice value of a process from 19 to 10, you can do this: sudo renice -n -9 -p 24161. This is important to note to avoid falling into a trap. Nice in Android Since Android is based on the Linux Kernel, there is also a nice value in Android. However, we generally cannot control it for the following reasons:
Thread Scheduling Although we cannot control the priority of the process, we can control the priority of the threads in the process. There are two kinds of thread priorities in Android, one is the Android API version and the other is the Java native version. Android API The thread priority levels in Android are currently defined as follows. Once you understand the relationship between process priority and nice value, the relationship between thread priority and value will be easier to understand.
It is also very simple to set the priority of a thread using the Android API. You only need to call the android.os.Process.setThreadPriority method when the thread is executing. This method of modifying the priority of a thread while it is running has an effect similar to renice. new Thread () { Java Native API Java provides three levels of settings for Thread.
Using setPriority we can set the priority for a thread, and using getPriority we can get the priority of a thread. In the Android system, it is not recommended to use the Java native API, because the API provided by Android is divided into more levels and is more suitable for setting detailed priorities in the Android system. Notice The thread priority of Android API is relatively independent from the priority of Java native API. For example, after using android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND), the value obtained by using Java native API, Thread.getPriority() will not change. As shown in the following code: new Thread() { The running log of the above code is I/MainActivity ( 3679 ) : Java Thread Priority Before = 5 I/MainActivity ( 3679 ) : Java Thread Priority = 5 Due to the above defect, we need to pay attention when analyzing ANR trace. In the ANR log information below, the value of proi in prio=5 corresponds to the thread priority of the Java native API, and nice in nice=-6 represents the thread priority of the Android API version. "main" prio=5 tid=1 NATIVE Avoiding ANRs In my previous article about ANR in Android, I mentioned using WorkerThread to handle time-consuming IO operations and lowering the priority of WorkerThread. For time-consuming IO operations, such as reading databases and files, we can set the priority of the workerThread to THREAD_PRIORITY_BACKGROUND to reduce the ability to compete with the main thread. |
<<: What is the background data of the most popular application in WeChat?
>>: 10 Questions Programmers Should Ask Themselves
On November 5, 2019, Galanz released an announcem...
Recently, the news that Starbucks causes cancer h...
The Climate Change Strategy, approved by the FAO ...
Nowadays, more and more companies and projects ar...
Tesla has just released its second quarter 2016 f...
Recently, I have been paying attention to the iss...
According to data from the National Bureau of Sta...
gossip Autumn is a time for abundant harvests. Th...
Christmas and New Year's Day are important fe...
[[439732]] Preface The screen refresh frame rate ...
(May 24, 2024, Shanghai) At the "SAIC New En...
What should you do if you encounter freeloaders d...
New energy vehicles are the general trend. Both t...
Baidu Cloud download link address of "Fan De...
Millet is the most important food crop in ancient...