android开发基于Android10分析请求Zygote执行Fork一个新的应用程序的过程

```
ActivityTaskManagerService.activityPaused

ActivityStack.activityPausedLocked

RootActivityContainer.resumeFocusedStacksTopActivities

ActivityStackSupervisor.startSpecificActivityLocked


void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
    // Is this activity's application already running?
	//先mProcessNames缓存中查找是否存在这个进程
    final WindowProcessController wpc =
            mService.getProcessController(r.processName, r.info.applicationInfo.uid);
	
	if (wpc != null && wpc.hasThread()) {
        try {
            realStartActivityLocked(r, wpc, andResume, checkConfig); //如果已经存在,直接启动
            return;
        } catch (RemoteException e) {
            Slog.w(TAG, "Exception when starting activity " + r.intent.getComponent().flattenToShortString(), e);
        }
		return;
    }		
	ActivityManagerInternal::startProcess //否则执行startProcess进行fork一个子进程
}

ActivityManagerService.startProcessLocked();

ProcessList.startProcessLocked(); // Process management

Process.start(entryPoint ...);

ZYGOTE_PROCESS.start(...);

ZygoteProcess.startViaZygote();

ZygoteProcess.zygoteSendArgsAndGetResult();

ZygoteProcess.attemptZygoteSendArgsAndGetResult();

private Process.ProcessStartResult attemptZygoteSendArgsAndGetResult(
        ZygoteState zygoteState, String msgStr) throws ZygoteStartFailedEx {
    try {
        final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter;
        final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream;
        zygoteWriter.write(msgStr);	//往zygote socket server写参数
        zygoteWriter.flush();

        Process.ProcessStartResult result = new Process.ProcessStartResult();
        result.pid = zygoteInputStream.readInt();	//从zygote socket server读取fork出来的子进程pid
        result.usingWrapper = zygoteInputStream.readBoolean();
        if (result.pid < 0) {
            throw new ZygoteStartFailedEx("fork() failed");
        }
        return result;
    } catch (IOException ex) {
        zygoteState.close();
        Log.e(LOG_TAG, "IO Exception while communicating with Zygote - "
                + ex.toString());
        throw new ZygoteStartFailedEx(ex);
    }
}

ActivityManagerService.handleProcessStartedLocked();//mService.mPidsSelfLocked.put(app);//ProcessRecord缓存起来

//新进程创建,走android.app.ActivityThread类main方法,然后走attachApplication,接着回到ActivityManagerService
ActivityManagerService.attachApplication

ActivityTaskManagerInternal.LocalService.attachApplication();

RootActivityContainer.attachApplication(wpc)

ActivityStackSupervisor.realStartActivityLocked

clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent)..)
if (andResume) {
	lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward());
} else {
	lifecycleItem = PauseActivityItem.obtain();
}
clientTransaction.setLifecycleStateRequest(lifecycleItem);
mService.getLifecycleManager().scheduleTransaction(clientTransaction);

//LaunchActivityItem
client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
```