美文网首页
Android应用程序进程启动笔记

Android应用程序进程启动笔记

作者: 百花鱼藤 | 来源:发表于2018-07-03 21:14 被阅读0次

     ActivityManagerService.startProcessLocked
    Process.start("android.app.ActivityThread",mSimpleProcessManagement ? app.processName :null, uid, uid,gids, debugFlags,null)调用了Process.start函数开始为应用程序创建新的进程。调用supportsProcesses判断binder文件描述符是否正常打开,startViaZygote

    Process.startViaZygote
    函数将创建进程的参数放到argsForZygote列表中去
    Process.zygoteSendAndGetPid:打开socket写入流openZygoteSocketIfNeeded();
    Process. openZygoteSocketIfNeeded:创建LocalSocket,进行连接
    ZygoteInit.runSelectLoopMode:循环,创建ZygoteConnection

    ZygoteConnection.runOnce :Zygote.forkAndSpecialize创建进程,返回pid,判读pid是否大于0
    等于0,handlchildProc,大于0,handleParentProc
    ZygoteConnection.handleChildProc:调用RuntimeInit.zygoteInit:

    RuntimeInit.zygoteInit:zygoteInitNative(),invokeStaticMain:表示要执行android.app.ActivityThread类的main函数。

    AppRuntime.onZygoteInit
    调用ProcessState::startThreadPool启动线程池了,这个线程池中的线程就是用来和Binder驱动程序进行交互。

    ProcessState.startThreadPool:调用ProcessState.spawnPooledThread:创建一个PoolThread线程类,然后执行它的run函数,最终就会执行PoolThread类的threadLoop函数
     PoolThread.threadLoop
     IPCThreadState.joinThreadPool
    函数首先告诉Binder驱动程序,这条线程要进入循环了,进行while循环:通过talkWithDriver不断与Binder驱动程序进行交互,以便获得Client端的进程间调用,executeCommand(cmd),退出循环,talkWithDriver(false)

    RuntimeInit.invokeStaticMain
    ClassLoader,把ActivityThread加载进来,函数最后并没有直接调用这个静态成员函数main,而是通过抛出一个异常ZygoteInit.MethodAndArgsCaller,然后让ZygoteInit.main函数在捕获这个异常的时候再调用android.app.ActivityThread类的main函数

    ZygoteInit.main
    执行MethodAndArgsCaller的run函数:Method.invoke

    ActivityThread.main

    相关文章

      网友评论

          本文标题:Android应用程序进程启动笔记

          本文链接:https://www.haomeiwen.com/subject/eturuftx.html