foreword
First, let’s take a look at the picture above to get a general understanding of the process from the Android device’s power-on button to the creation of system_server
the process .
The detailed sub-processes and what happened system_server
after that will be explained in detail in subsequent articles:
What are these nouns involved in the picture? Let's briefly talk about it.
Boot Rom
When the power button is pressed, the boot chip will start, and the boot chip code will start to execute from a predefined place (the code is stored in Rom), and the boot program will be loaded BootLoader
into RAM. In other words, when starting, the boot program code will be loaded Move from ROM to RAM, and then execute the boot program BootLoader
.
ROM can be simply understood as a hard disk, and the internal data will not be lost after power failure.
RAM can be simply understood as runtime memory, and the internal data will be lost after power failure.
BootLoader
BootLoader is a small program before the Android operating system starts running. Its main function is to pull up the system OS and run it. After the BootLoader is executed, the operating system will be started.
idle process
When the operating system starts, it will start the first process of the system , namely idle
the process , and its process number is 0.
When idle
the process is started, it will look for init.rc
a file in the system file, which is specially used to start init
the process .
In addition to starting init
the process , another process is also started: kthreadd
the process.
init process
The process number of the init process is 1, which is located in the kernel layer.
Its main job is to initialize and manage processes, memory management, load Binder Driver, Display, Camera Driver, etc. init 进程
It is the first process to develop user space and the originator of user space .
Inside the init process, a well-known process will be forked: the zygote process.
kthreadd process
The kthreadd process, like the init process, resides at the kernel level.
It is mainly used to create kernel-related work such as kernel worker threads and soft interrupt threads, and we don't need to pay too much attention to it.
zygote process
The zygote process, also known as the incubator process, is the originator of the java process.
In zygote
the process , the first fork process is system_server
the process, and at the same time, the App process on our device is created by zygote
the process .
system_server process
Commonly used system services (AMS/WMS/PMS) in Android are all started by system_server
this process, so it is very important.
zygote / system_server process startup process
The following source code is based on Android 11 .
init
After the process is started, the methodframeworks/base/cmds/app_process/app_main.cpp
of the file will be executed,main()
The main()
method first creates AndroidRuntime
the object (runtime), which is the Android runtime environment object, as follows:
Continue reading main()
the method ,
It is found that runtime.start("com,android.internal.os.ZygoteInit", args, zygote)
the method , and it can be guessed that this should be zygote
the entry point to start the process, as follows:
So let's go inside runtime.start()
the method to see what's written in it:
As you can see, in runtime.start()
the method, two methods are executed:
- Execution
startVM
method : Create a virtual machine (it can also be seen here that a process contains a virtual machine inside) - Execution
startReg
method : Register the JNI method, the jni method can only be used after registration
Continue reading runtime.start()
the method ,
It can be found that the following code starts to use the jni method to call com.android.inter.os.ZygoteInit.main()
the method , as follows:
From then on, the code will execute to the code of the java layer and enter the java world.
So why use the jni method to call ZygoteInit.main()
the method ?
The reason is that jni is a bridge between the native layer and the java layer. If you want to enter the java layer from the native layer, you need to use jni.
zygote
Process The first java process is also the originator of all java processes in Android. For example, the apps we usually develop belong to the java layer process.
Next, continue to enter ZygoteInit.main()
the method to see what is done inside:
It can be seen that the Le preload()
method , which is used to preload information, and the preload information includes framework
some resources and some commonly used java classes.
This part of the preloaded information can be directly used by the processes created later . So the function of preloading here is to speed up the startup speed of the application process.
Continue to look down the code:
It can be seen that zygoteServer = new ZygoteServer(isPrimaryZygote)
this . This line of code is used to create the socket service of zygote, socket
which is used for cross-process communication.
Someone may ask, doesn't Android use Binder for cross-process communication, why not use Binder here? In fact, the code execution has reached this point, and Binder has not yet started to initialize.
Continue to look down the ZygoteInit.main()
method code:
It can be seen that forkSystemServer(abiList, zygoteSocketName, zygoteServer)
the method ,
In fact, this fork
method creates system_setver
a process in the form of and returns a Runnable object.
After getting the Runnable object, execute r.run()
the method , and r.run()
inside this method, the method of SystemServer will be executed to start the process.main
system_servier
Next, continue to look down the ZygoteInit.main()
method code:
You'll find zygoteServer.runSelectLoop()
ways ,
This method puts zygote
into an infinite loop, waiting AMS
for to send him a message (telling the zygote
creation process).
So far, the startup process of the zygote process and the system_server process is simply finished. Here is a picture for everyone to understand: