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 */);
```
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.