I am searching for some information about how are apps launch on android. I want ot found infromation what make zygote and about fork() . Do you know some usefull web-sites or books?
I have written a two part series to explain Android Application launch process on my blog -
http://multi-core-dump.blogspot.com/2010/04/android-application-launch.html
http://multi-core-dump.blogspot.com/2010/04/android-application-launch-part-2.html
I hope you will find it useful.
There is a nice explanation in this presentation. It's written partially in Korean, but the most part of information in English.
Here is an concise(the process is very complex so a concise one still won't be a short one) yet precise answer based on AOSP 9.0.0.
Every Android Java process is forked from Zygote, so first is how Zygote starts.
init process is the first process in linux, it start the executable "app_process", whose internal is :
(entry point of app_process)int main
->void AndroidRuntime::start
startVm //start Java VM
startReg //register common native functions
//call java method ZygoteInit.main from native code
env->CallStaticVoidMethod
Then here is the most important java method: ZygoteInit.main, we get here from the above native code "env->CallStaticVoidMethod ".
This is also the first method in the call stack when you set a break point in main Activity's onCreate and start debuging your application and break there. But in fact your application never get to the beginning of ZygoteInit.main, it is executed from the beginning only in app_process(or say Zygote).
//Java code
->ZygoteInit.main
//Android application never get here(the beginning)
//start system server process which contains AMS/WMS,etc
forkSystemServer
//for Zygote, runSelectLoop never return
//for Android application, runSelectLoop returns a Runnable
//whose run() method will just execute ActivityThread.main
//which is considered as the real main entry of Android application
//since it contains the message loop
caller = zygoteServer.runSelectLoop
->zygoteServer.runSelectLoop
//this is the main loop of Zygote who is a
//server that receive process-creating requests
//from client processes and fork them
loop forever
//Zygote wait here for other process's requests to start new Java process
Os.poll(pollFds, -1);
//after wake up upon requests arrive, process the request
final Runnable command = connection.processOneCommand(this);
->ZygoteConnection.processOneCommand
read and parse process-start request command from client process
//the native linux fork() is executed in this methdod
//after it returns, we can decide which process we are in
//from pid's value just like the native fork()
pid = Zygote.forkAndSpecialize
//if in child
return handleChildProc
->ZygoteConnection.handleChildProc
return ZygoteInit.zygoteInit
->ZygoteInit.zygoteInit
RuntimeInit.commonInit();
ZygoteInit.nativeZygoteInit();//JNI method
//native code
->AppRuntime::onZygoteInit()
sp<ProcessState> proc = ProcessState::self();
//starting thread pool which contains binder threads
proc->startThreadPool();
return RuntimeInit.applicationInit
->RuntimeInit.applicationInit
return findStaticMain
->RuntimeInit.findStaticMain
//MethodAndArgsCaller is a Runnable,
//whose run() is constructed so that
//it just call ActivityThread.main()
//it is ActivityThread since there is a string parameter
//whose content is "android.app.ActivityThread" from the client process's request
return new MethodAndArgsCaller
//if in parent process i.e. Zygote
return null
//if in forked child process
return command;
//if in parent process i.e. Zygote
continue to run the loop
//Zygote never get here
//for Android application, now caller contains a MethodAndArgsCaller which is a Runnable
//whose run() calls ActivityThread.main and never return
//since ActivityThread.main runs the main message loop
caller.run();
When you start an Activity, finally you get into in Activity Manager Service(AMS, which is in system server process). If the process for that Activity is not created yet, AMS will send process-start request request to Zygote server (in the zygote process described above,started by init process), the process is like:
//in AMS (system server process)
final String entryPoint = "android.app.ActivityThread";
return startProcessLocked(....,entryPoint, ....);
->startProcessLocked
->Process.start
->ZygoteProcess.start
->ZygoteProcess.startViaZygote
->ZygoteProcess.zygoteSendArgsAndGetResult
//send the request to Zygote server through sockets
//note that "android.app.ActivityThread" is send to Zygote server as a parameter
At this time, the above listed Zygote code will wake up from
Os.poll(pollFds, -1);
and fork the child process, and after this, the parent process i.e. Zygote will again execute poll waiting for the next request, and the forked child process will return from runSelectLoop and execute ActivityThread.main as described in the above code listing.
So the exact entry point of the new process will be after the native fork() deep down in Zygote.forkAndSpecialize, in a native function called ForkAndSpecializeCommon to be exactly, then all the way up through a return route up to
caller = zygoteServer.runSelectLoop
in ZygoteInit.main. So although the call stack of an Android application start at ZygoteInit.main, the code executed in ZygoteInit.main begins after the call to runSelectLoop instead of the beginning of ZygoteInit.main.
About Activity: in fact Activity has nothing to do with entry point or start up process. An Activity is started when AMS send an Activity-start request to a process at any time. So Activity start process always begin in the main message loop when the start request is received, it is driven by message from AMS and is fully decoupled from the application start process.
© 2022 - 2024 — McMap. All rights reserved.
startActivity
. For getting in-depth knowledge how android handles processes you better search for thelinux kernel
instead of android :) – Remediosremedy