04 December 2014

zygote fork

Q. how does zygote exactly fork Dalvik VM?
Short Answer: Zygote process basically cold boots a VM on system start up. Once done, it listens to a socket for incoming commands. Other processes (e.g. ActivityManagerService) writes commands to this socket, whenever it needs a new process for an application. This command is read by the Zygote process and calls fork() - so the child process now gets a pre-warmed up VM in which to run. This is how zygote forks the Dalvik VM.
Long answer: (this could be really long) After kernel is loaded, init.rc is parsed and native services are started. With this, the /system/bin/app_process (Source code:frameworks/base/cmds/app_process/app_main.cpp) is run. This eventually calls AndroidRuntime.start() function (Source code: frameworks/base/core/jni/AndroidRuntime.cpp), passing it the parameters com.android.internal.os.ZygoteInit and start-system-server.
The AndroidRuntime.start() starts the Java VM, then calls ZygoteInit.main() (Source code:frameworks/base/core/java/com/android/internal/os/ZygoteInit.java), passing it the parameter start-system-server.
ZygoteInit.main() first registers the zygote socket (the zygote process listens to a socket for incoming commands, and on receiving new command, spawns a new process as requested). Next thing that happens is, it preloads a lot of classes (which is listed in frameworks/base/preloaded-classes, last I checked, 2307 lines, in android 4.0.4) and all the system-wide resources like drawables, xmls, etc. Then it calls startSystemServer() which forks a new process for com.android.server.SystemServer(Source code: frameworks/base/services/java/com/android/server/SystemServer.java). Forking of system server is a special case, because in all other cases, zygote process listens to a socket and forks for new commands, as we will see soon.
After SystemServer is forked, a function runSelectLoopMode() is called. This is a while(true) loop which basically establishes a ZygoteConnection with the zygote socket and waits for commands on it. When a command is received, ZygoteConnection.runOnce() is called (Source code:frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java)
ZygoteConnection.runOnce() calls Zygote.forkAndSpecialize() (Source code:libcore/dalvik/src/main/java/dalvik/system/Zygote.java) which simply calls a natvie function to do the fork. Thus, like in the case of SystemServer, a child process is created which has a pre-warmed up Dalvik for itself.
Q. why it is not possible to run multiple applications in the same Dalvik VM?
This is a design decision as far as I know. Android guys just decided to fork a new VM per process, for security via sandboxing.




ref: http://stackoverflow.com/questions/9153166/understanding-android-zygote-and-dalvikvm

No comments:

Post a Comment

Terima kasih