APP進程也是從zygote進程孵化出來的,他通過socket與 zygote進行通信虹蓄;
先來看下客戶端的調(diào)用代碼:
客戶端谷朝, 首先是本地通過binder與ActivityManagerService進行通信, 最后調(diào)用到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);
}
->調(diào)用到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武花,這里參數(shù)調(diào)用打開 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發(fā)送命令給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();
}
客戶端發(fā)送消息后,下面就是服務器端的接受消息杈帐,這個要從zygote的runloopselect開始
zygoteServer.java
->runSelectLoop
void runSelectLoop(String abiList) throws Zygote.MethodAndArgsCaller {
ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
while(true){
boolean done = peers.get(i).runOnce(this);//執(zhí)行ZygoteConnection的runOnce函數(shù)
}
}
->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) { //創(chuàng)建的子進程
// 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也是非常的類似挑童,這邊根據(jù)傳入的參數(shù)累铅,會反射調(diào)用到進程的主入口ActivityThread.main().
ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion,
parsedArgs.remainingArgs, null /* classLoader */);
}