我正在搜索有关如何在Android上启动应用的一些信息。我希望ot发现了什么使得zygote和fork()。你知道一些有用的网站或书籍吗?
答案 0 :(得分:16)
我已经写了一个两部分系列来解释我的博客上的Android应用程序启动过程 -
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
我希望你会发现它很有用。
答案 1 :(得分:2)
在this演示文稿中有一个很好的解释。它部分用韩文写成,但大部分是英文信息。
答案 2 :(得分:0)
这是一个简洁的过程(过程非常复杂,因此简洁不会是一个简短的过程),但是基于AOSP 9.0.0的精确答案。
每个Android Java进程都是从Zygote分叉的,所以首先是Zygote的启动方式。
init进程是linux中的第一个进程,它启动可执行文件“ app_process”,其内部是:
(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
然后这是最重要的java方法:ZygoteInit.main,我们从上面的本地代码“ env-> CallStaticVoidMethod”获得此处。
这也是在主Activity的onCreate中设置断点并开始调试应用程序并在其中中断时在调用堆栈中使用的第一个方法。但是实际上您的应用程序永远不会到达ZygoteInit.main的开头,它仅在app_process(或说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();
启动活动时,最后进入到活动管理器服务(AMS,它在系统服务器进程中)。如果尚未创建该活动的进程,则AMS将向Zygote服务器发送进程启动请求请求(在上述的zygote进程中,由init进程启动),该进程类似于:
//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
这时,上面列出的Zygote代码将从中唤醒
Os.poll(pollFds, -1);
并分叉子进程,此后,父进程(即Zygote)将再次执行轮询以等待下一个请求,并且分叉的子进程将从runSelectLoop返回并执行ActivityThread.main,如上面的代码清单所述。
因此,新过程的确切入口将在Zygote.forkAndSpecialize中的本机fork()之后,在称为ForkAndSpecializeCommon的本机函数中,确切地说,然后一直到返回路径直至>
caller = zygoteServer.runSelectLoop
ZygoteInit.main中的。因此,尽管Android应用程序的调用堆栈始于ZygoteInit.main,但在ZygoteInit.main中执行的代码是在调用runSelectLoop之后而不是ZygoteInit.main的开头开始的。
关于活动:实际上,活动与入口点或启动过程无关。当AMS随时向流程发送“活动开始”请求时,就会启动“活动”。因此,活动启动过程总是在收到启动请求时在主消息循环中开始,它由AMS的消息驱动,并与应用程序启动过程完全脱钩。