APP进程也是从zygote进程孵化出来的,他通过socket与 zygote进行通信;
先来看下客户端的调用代码:
客户端, 首先是本地通过binder与ActivityManagerService进行通信, 最后调用到processStart;
ActivityManagerService.java
private final void startProcessLocked(ProcessRecord app, String hostingType){
startResult = Process.start(entryPoint,
app.processName, uid, uid, gids, debugFlags, mountExternal,
app.info.targetSdkVersion, app.info.seinfo, requiredAbi, instructionSet,
app.info.dataDir, invokeWith, entryPointArgs);
}
->调用到Process.start,
Process.java
public static final ProcessStartResult start(final String processClass..){
return zygoteProcess.start(processClass, niceName, uid, gid, gids,..)
}
->zygoteProcess.start
zygoteProcess.java
public final Process.ProcessStartResult start(final String processClass,..){
return startViaZygote(processClass, niceName, uid, gid, gids);
}
->startViaZygote, sendARGs,这里参数调用打开 zygoteSocket
private Process.ProcessStartResult startViaZygote(final String processClass,..){
synchronized(mLock) {
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
}
}
->openZygoteSocketIfNeeded
private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
primaryZygoteState = ZygoteState.connect(mSocket);//打开socket连接
}
->zygoteSendArgsAndGetResult
private static Process.ProcessStartResult zygoteSendArgsAndGetResult(){
//这里就是通过socket发送命令给Zygote进程
final BufferedWriter writer = zygoteState.writer;
final DataInputStream inputStream = zygoteState.inputStream;
writer.write(Integer.toString(args.size()));
writer.newLine();
for (int i = 0; i < sz; i++) {
String arg = args.get(i);
writer.write(arg);
writer.newLine();
}
writer.flush();
}
客户端发送消息后,下面就是服务器端的接受消息,这个要从zygote的runloopselect开始
zygoteServer.java
->runSelectLoop
void runSelectLoop(String abiList) throws Zygote.MethodAndArgsCaller {
ArrayList peers = new ArrayList();
while(true){
boolean done = peers.get(i).runOnce(this);//执行ZygoteConnection的runOnce函数
}
}
->runOnce
boolean runOnce(ZygoteServer zygoteServer) throws Zygote.MethodAndArgsCaller {
//开始fork进程,这个先不跟踪下去了, 跟systemserver的流程是类似的
pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, parsedArgs.gids,
parsedArgs.debugFlags, rlimits, parsedArgs.mountExternal, parsedArgs.seInfo,
parsedArgs.niceName, fdsToClose, fdsToIgnore, parsedArgs.instructionSet,
parsedArgs.appDataDir);
if (pid == 0) { //创建的子进程
// in child
zygoteServer.closeServerSocket();
IoUtils.closeQuietly(serverPipeFd);
serverPipeFd = null;
handleChildProc(parsedArgs, descriptors, childPipeFd, newStderr);
// should never get here, the child is expected to either
// throw Zygote.MethodAndArgsCaller or exec().
return true;
} else {
// in parent...pid of < 0 means failure
IoUtils.closeQuietly(childPipeFd);
childPipeFd = null;
return handleParentProc(pid, descriptors, serverPipeFd, parsedArgs);
}
}
->handleChileProc
private void handleChildProc(Arguments parsedArgs,..){
//这边跟systemserver也是非常的类似,这边根据传入的参数,会反射调用到进程的主入口ActivityThread.main().
ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion,
parsedArgs.remainingArgs, null /* classLoader */);
}