Zygote and system process start-up process

Source: Internet
Author: User

Han Mengfei sha Han Yafei [email protected] yue31313 Han_meng_fei_sha

#Zygote和System进程的启动过程 # #init脚本的启动
+------------+    +-------+   +-----------+|Linux Kernel+--> |init.rc+-> |app_process|+------------+    +-------+   +-----------+               create and public                           server socket

After the Linux kernel is loaded, run the init.rc script

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server socket zygote stream 666
    • /system/bin/app_process zygote Service initiated process name
    • --start-system-server indicates that the system process will be started after zygote startup is complete.
    • Socket zygote Stream 666 When Zygote is started, create a socket with a permission of 666. This socket is used to request zygote to create a new process. The FD of the SOCKET is stored in an environment variable named "Android_socket_zygote".
# #Zygote进程的启动过程
               create rumtime                              +-----------+            +----------+                    |app_process+----------> |ZygoteInit|                    +-----------+            +-----+----+                                                   |                                                        |                                                        | registerZygoteSocket()                                 |                          +------+  startSystemServer() |                          |System| <-------+            |                          +------+   fork               | runSelectLoopMode()                                    |                                                        v         

App_process process

/system/bin/app_process a Appruntime object was created at startup. Through the Start method of the Appruntime object, a virtual machine instance is created with the JNI call, and then the static Main method of the Com.android.internal.os.ZygoteInit class is run, passing True (Boolean Startsystemserver) parameter.

Zygoteinit class

When the main method of the Zygoteinit class is run, a server socket is created for activitymanagerservice using the Registerzygotesocket method. The system process is then started by calling the Startsystemserver method. Finally, wait for the new process request from AMS through Runselectloopmode.

    1. In the Registerzygotesocket method, the FD for the socket created at zygote startup is obtained through an environment named Android_socket_zygote, and then the server socket is created.
    2. In the Startsystemserver method, a child process is created by the Zygote.forksystemserver method and the ID of its user and user group is set to 1000.
    3. In the Runselectloopmode method, the previously established server socket is saved. Then enter an infinite loop, in which the Selectreadable method is used to monitor whether the socket has data readable. There is data to indicate that a request was received. The Selectreadable method returns an integer value of index. If index is 0, this is the connection request that AMs sent over. A new socket connection will be created with AMS and packaged into a Zygoteconnection object to be saved. If index is greater than 0, this is a request from AMS to create a new process. The previously saved Zygoteconnection object is taken out, and the RunOnce method is called to create a new process. After the call is complete, the connection is deleted. This is the process by which zygote processes a AMS request.
# #System进程的启动
 +                                                      |                                                      | V Fork () +--------------+ |                                       System process|                                                      +------+-------+                                              | | Runtimeinit.zygoteinit () Commoninit, zygoteinitnative |                                                      Init1 () Surfaceflinger, Sensorservic ... |                                                      | | Init2 () +------------+ +-------> |                               serverthread|         |              +----+-------+                               |                                       |              | | AMS, PMS, WMS ... |              |              |                                       |              |                                       |                V V

The system process starts in the handlesystemserverprocess of Zygoteinit.

    1. First, because the system process is directly fork the zygote process, the server socket is first switched off by the Closeserversocket method.
    2. Call the Runtimeinit.zygoteinit method to start the system process further. In Zygoteinit, common information such as the time zone and keyboard layout is set by the Commoninit method, and a binder thread pool is started by the Zygoteinitnative method. Finally, the static main method of the Systemserver class is called through the Invokestaticmain method.
    3. The Systemserver class's main init1 method is called by the CPP implementation through JNI. In the Init1 method, a variety of system services developed in C + +, such as Surfaceflinger and Sensorservice, are initiated. Then callback the Init2 method of the Serverserver class to start the system services developed in Java.
    4. In the Init2 method, a new Serverthread thread named "Android.server.ServerThread" is first created and its Start method is called. The various service (such as AMS,PMS,WMS, etc.) is then started in the thread. The way to start is to invoke the static Main method of the corresponding service class.
    5. First, AMS will be created, but not registered in Servermanager. The PMS was then created and AMS was registered to Servermanager. Then to Contentservice, WMS and so on. The name of the service is established when registering to Servermanager, and other processes can then obtain the Binder proxy object to access service-provided services.
    6. By doing this, the system starts the critical services of the systems, and other processes can take advantage of the basic services provided by the service.
    7. Finally, the Activitymanagerservice Systemready method is called, in which the system interface and the home program are started.

# #Android进程启动

   +----------------------+       +-------+      +----------+   +----------------+   +-----------+ |       activitymanagerservice| |      process| |   Zygoteinit| |   zygoteconnection| |                            Runtimeinit|                                           +--------------+-------+       +---+---+      +-----+----+   +-----------+----+   +------+----+                   |                |                    |               |                                                |                   |                |                    |               |                               |                Startprocesslocked () |                    |               |                              |                   +---------------> |                |                    |               |                                                |  |                Start () |                    |               |                                           |     |                    "Android.app.ActivityThread" |               |                                                |                +-----------------> |                    |               |                                                |                   |                |                    |               |                                                |                   |                |                    |               |                                                |                   |               |openzygotesocketifneeded () |                                                |                   |                    +------+         |               |                                                |                   |      |         |                    |               |                                                |                   | |                    <----+ |               |                                                |    |               |                    |               |                                                |                   |               |szygotewriter.write (ARG) |                                                |                   |                    +------+         |               |                                                |                   |      |         |                    |               |                                                |                   |      |         |                    |               |                                                |                   | |                    <----+ |               |                                                |                   |                |                    |               |                                                |                   |                    +--------------> |               |                                                |              |     |                    |               |                                                |                   |                |               |runselectloopmode () |                                                |                   |                |               +-----------------+  |                                                |                   |                |                 |  |               |                                                |                   |                | |               <---------------+ |                                                |                   |                |   |                                                Acceptcommandpeer () |                   |                |                    |               |                                                |                   |                |                    |               |                                                |                   |       |         |               RunOnce () |                                                |                   |                |               +------------------> |                                                |                   |                |                    |                   |forkandspecialize () |                |                    |                                                +-------------+ |                   |                |                    |             |                                                | |                   |                |                    | |                                                <-----------+ |                   |                |                    |  |                   Handlechildproc () |                |                    |               |                                                |                   |                | |                    |                                                |                   |                |                    |               |                                                |                   |                |                    | |                                                Zygoteinit () |                   |                |                    |                                                +-------------> |                   |                |                    |               |                                                |                   |                |                    |               |                   |in^okestaticmain () |                |                    |               |                   +----------------> |                |                    |               | |                   ("Android.app.ActivityThread") |                |          |          |                                                |                   |                |                    |               |                                                |                             +                   +                +                    +               +
    • AMS initiates a request to zygote (via a previously saved socket) and carries various parameters, including "Android.app.ActivityThread".
    • The zygote process fork itself, and then calls the Runtimeinit.zygoteinit method in the new zygote process for a series of initializations (Commoninit, binder thread pool initialization, and so on).
    • The new zygote process calls Activitythread's main function and starts the message loop.

Zygote and system process start-up process

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.