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.