The Startup Process of Zygote and System processes.

Source: Internet
Author: User

The Startup Process of Zygote and System processes.
# Start the init script

+------------+    +-------+   +-----------+|Linux Kernel+--> |init.rc+-> |app_process|+------------+    +-------+   +-----------+               create and public                           server socket

Run the init. rc Script after the Linux kernel is loaded.

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server socket zygote stream 666
  • /System/bin/app_process Zygote name of the process started by the Service
  • -- Start-system-server indicates that the System process must be started after Zygote is started.
  • Socket zygote stream 666 creates a socket with the 666 permission when Zygote is started. This socket is used to request Zygote to create a new process. The socket fd is saved in the environment variable named "ANDROID_SOCKET_zygote.
# Start Process of Zygote Process
Create rumtime + ----------- ++ ---------- + | app_process + ----------> | ZygoteInit | + ----------- ++ ----- + ---- + | registerZygoteSocket () | + ------ + startSystemServer () | System | <------- + | + ------ + fork | runSelectLoopMode () | v

App_process Process

/System/bin/app_process
An AppRuntime object is created at startup. Create a VM instance by calling JNI through the start method of the AppRuntime object, and then run the static main method of the com. android. internal. OS. ZygoteInit class to pass true (boolean
StartSystemServer) parameter.

ZygoteInit class

When running the main method of the ZygoteInit class, a server socket for ActivityManagerService is created using the registerZygoteSocket method. Then, start the System process by calling the startSystemServer method. Finally, runSelectLoopMode is used to wait for the new process request of AMS.

# Start of the System process
 +                                                      |                                                      |                                                      v fork()                                               +--------------+                                       |System Process|                                       +------+-------+                                              |                                                      | RuntimeInit.zygoteInit() commonInit, zygoteInitNative                                                     | init1()  SurfaceFlinger, SensorServic...             |                                                      |                                                      | init2() +------------+                               +-------> |ServerThread|                               |         +----+-------+                               |              |                                       |              | AMS, PMS, WMS...                      |              |                                       |              |                                       |              |                                       v              v           

The System process starts in handleSystemServerProcess of ZygoteInit.

# Android process startup

   +----------------------+       +-------+      +----------+   +----------------+   +-----------+                            |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 (through the previously saved socket), carrying various parameters, including "android. app. ActivityThread ".
  • The Zygote process fork itself, and then calls RuntimeInit. zygoteInit in the new Zygote process for a series of initialization (commonInit, Binder thread pool initialization, etc ).
  • The new Zygote process calls the main function of ActivityThread and starts a message loop.


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.