[Android] System startup process (zygote process startup process)

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_serverthe process .
The detailed sub-processes and what happened system_serverafter that will be explained in detail in subsequent articles:

insert image description here
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 BootLoaderinto 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 idlethe process , and its process number is 0.

When idlethe process is started, it will look for init.rca file in the system file, which is specially used to start initthe process .

In addition to starting initthe process , another process is also started: kthreaddthe 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 zygotethe process , the first fork process is system_serverthe process, and at the same time, the App process on our device is created by zygotethe process .

system_server process

Commonly used system services (AMS/WMS/PMS) in Android are all started by system_serverthis process, so it is very important.

zygote / system_server process startup process

The following source code is based on Android 11 .

initAfter 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 AndroidRuntimethe object (runtime), which is the Android runtime environment object, as follows:

insert image description here

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 zygotethe entry point to start the process, as follows:

insert image description here

So let's go inside runtime.start()the method to see what's written in it:

insert image description here

As you can see, in runtime.start()the method, two methods are executed:

  • Execution startVMmethod : Create a virtual machine (it can also be seen here that a process contains a virtual machine inside)
  • Execution startRegmethod : 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:

insert image description here

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.

zygoteProcess 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:

insert image description here

It can be seen that the Le preload()method , which is used to preload information, and the preload information includes frameworksome 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:

insert image description here

It can be seen that zygoteServer = new ZygoteServer(isPrimaryZygote)this . This line of code is used to create the socket service of zygote, socketwhich 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:

insert image description here

It can be seen that forkSystemServer(abiList, zygoteSocketName, zygoteServer)the method ,

In fact, this forkmethod creates system_setvera 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.mainsystem_servier

Next, continue to look down the ZygoteInit.main()method code:

insert image description here

You'll find zygoteServer.runSelectLoop()ways ,

This method puts zygoteinto an infinite loop, waiting AMSfor to send him a message (telling the zygotecreation 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:

insert image description here

Guess you like

Origin blog.csdn.net/yang553566463/article/details/126544996