东莞营销网站制作,中山做网站多少钱,浏阳seo快速排名,最近时事新闻上一篇讲了Zygote是如何收到启动Application的启动消息,并一步步进入Fork()#xff0c;下面来分析zygote fork启动application后#xff0c;application进程后续处理操作#xff0c;是如何真正的启动的。
ZygoteInit.main():--...caller ZygoteServer.runSelectLoop()…上一篇讲了Zygote是如何收到启动Application的启动消息,并一步步进入Fork()下面来分析zygote fork启动application后application进程后续处理操作是如何真正的启动的。
ZygoteInit.main():--...caller ZygoteServer.runSelectLoop();--while(true) //死循环--Zygoteconnection connection peers.get(); Runnable command connection.processOneCommand();//进行进程的处理创建新进程--args Zygote.readArgumentList(mSocketReader);//获取socket命令参数ZygoteArguments parsedArgs new ZygoteArguments();...各种参数解析中...pid zygote.forkAndSpecialize();//Fork子进程得到一个新的pid.--pid nativeForkAndSpecialize(); //调用native层接口去forkif(pid 0){ //子进程}return pid;if(pid 0) //子进程Application进程{//关闭Zygote服务Socket因为fork时复制出来的socket对Application进程来说它没有用。zygoteServer.closeServerSocket(); //application进程可以正常运行了。return handleProcessChild();--ZygoteConnection.java:ZygoteInit.zygoteInit(parseArgs.xxx); //app进程的启动--ZygoteInit.java:RuntimeInit.commonInit(); //初始化运行环境ZygoteInit.nativeZygoteInit();//启动Binder, 并在androidRuntime.cpp中注册--com_android_internal_os_ZygoetInit_ativeZygoteInit():--gCurRuntime-onZygoteInit(); //通过JNI进入Native--//进入app_main.cpp.onZygoteInit();//下面ProcessState对应Application这个进程实例里面会初始化Binder--spProcessState proc ProcessState::self();--在C构造函数初始化列表中mDriverFD(open_driver(driver))//这里总结下Application被Zygote Fork出来之后进入到Native层处理的目的是为了构建Binder.//因为后续的跨进程通信都需要借助Binder.后续将此Binder发给AMSAMS拿到App的IBinder才能//够通过AMS的服务来与APP通信。proc-startThreadPool(); //启动Binder线程池//里面通过反射创建程序入口函数的Method对象并返回Runnable对象return RuntimeInit.applicationInit();//类名字类参数加载器--return findStaticMain(args.startClass, args.startArgs,classLoader);//通过反射拿到对应类的main方法的Method对象找到的就是ActivityThread.java.main();--m cl.getMethod(main,new class[]{string[].class});return 近回一个Runnable 对象。}else{ //zygote 进程}...//Runnable对象返回到这里对应上面代码中的Runnable command connection.processOneCommand();后面//继续接着返回最后返回到上面代码的caller ZygoteServer.runSelectLoop();if(caller ! null)caller.run(); //执行返回的Runnable对象进入子进程。--RuntimeInit.java.MethodAndArgsCaller-run();--mMethod.invoke();//java反射原理。执行的是ActivityThread.java的main()
分析时需要注意的是底层调用linux fork()接口之后会有两个返回值如果pid 0,表示返回的是子进程如果pid 0,返回的是父进程即zygote的程序运行路线父进程zygote进程可以得知子进程的pid号。
补充一个要点ApplicationThread是什么它其实是一个IApplicationThread.Stub对象通过IBinder对象进行跨进程通信访问时ApplicationThread本质就是Binder线程池中的一个线程关联到上面代码中的proc-startThreadPool() )