Joke Collection Website - Talk about mood - High-frequency interview on Android development in big factories: tell me about your understanding of Zygote.
High-frequency interview on Android development in big factories: tell me about your understanding of Zygote.
The zygote has two functions:
It is possible to answer these two points about this question. The second point may be answered by most friends, but the first point is not very clear. SystemServer is also started by Zygote, because the system resources that SystemServer needs Zygote to prepare include:
Inheriting directly from Zygote does not require reloading, so the performance will be greatly improved.
Before talking about the startup process of Zygote, let's first clarify a concept: startup three steps, which can be understood as a common routine for process startup in Android, and it is divided into three steps:
Here, we need to know what a LOOP loop is. In fact, the role of loop is to constantly accept messages and process them. The source of the message can be the message sent by Soket, MessageQueue and Binder, but no matter where the message comes from, the whole process is to accept the message and process it. This three-step start is not only a zygote process, but also an independent process, such as a system service process, which is its own application process.
The startup of Zygote process depends on init process, which is the first process in user space after linux starts. Let's look at the startup process:
3. Start the system services defined in the configuration file, in which the Zygote service is defined in the configuration.
4. Besides Zygote, other system services will be started at the same time, such as the ServiceManager process, which is started by fork+execve system call.
In the init.rc file, we will import /init. ${ro.zygote}。 Rc, init.zygoteXX, XX, which means 32 or 64. It doesn't matter to us. We can look at init.zygote32.rc directly. The configuration file is long, and the relevant parts of Zygot are intercepted and kept here.
Having finished the startup configuration, let's talk about the startup process. There are two ways to start this process:
The first type: fork+handle
The second type: fork+execve
They look very similar. First, they all call the fork function to create a child process. This strange function will return twice, once for the child process and once for the parent process. The difference is:
Mainly divided into two parts: native layer processing and Java layer processing. After the Zygote process is started, the executable system call is executed, with the main function in the binary executable program written by C++ as the entrance, and then it runs in the Java layer!
Let's take a look at the processing flow of the primary layer first.
In the app_main.cpp file, the AndroidRuntime.cpp file. We can find several main function names.
According to the above code, you can directly call JNI in our application without creating a virtual machine. Because the application process is hatched by the zygote process, it inherits the virtual machine owned by the parent process and only needs to reset the data.
Then take a look at the processing of Java layer, and refer to the main method of ZygoteInit file for details.
1. preload resources, such as common class libraries, subject resources and some * * * shared libraries.
2. Start the SystemServer process.
3. When you enter the Loop loop loop of the socket, you will see the call of ZygoteServer.runSelectLoop(…).
There are two main problems to be solved in the process of zygote startup.
finally
- Related articles
- Two-dimensional small fresh animation qq avatar
- Simple happy mood sentences about hair bands (selected 80 sentences)
- The warmest sentence on an elder's birthday
- How to make a haircut look high-end and classy
- Lying in bed, tossing and turning, insomnia
- I want to give up this relationship when I am tired, which means to let go and put it down (78 selected sentences)
- Talk about the delivery ceremony in the circle of friends.
- Say baby, how to do math exercises?
- The sentence about the breakdown of marriage is about mood, and the sentence about betraying marriage is about mood.
- Aesthetic nature of literature and art