The above function places the parameters of the created process in the argsForZygote list.
For example, the parameter "-- runtime-init" indicates that you want to initialize the runtime Library for the newly created process, and then call the zygoteSendAndGetPid function for further operations.
Step 4. Process. zygoteSendAndGetPid
This function is defined in the frameworks/base/core/java/android/OS/Process. java file:
- [Java] view plaincopypublic class Process {
- ......
- Private static int zygoteSendArgsAndGetPid (ArrayList args)
- Throws ZygoteStartFailedEx {
- Int pid;
- OpenZygoteSocketIfNeeded ();
- Try {
- /**
- * See com. android. internal. OS. ZygoteInit. readArgumentList ()
- * Presently the wire format to the zygote process is:
- * A) a count of arguments (argc, in essence)
- * B) a number of newline-separated argument strings equal to count
- *
- * After the zygote process reads these it will write the pid
- * The child or-1 on failure.
- */
- SZygoteWriter. write (Integer. toString (args. size ()));
- SZygoteWriter. newLine ();
- Int sz = args. size ();
- For (int I = 0; I <sz; I ++ ){
- String arg = args. get (I );
- If (arg. indexOf ('\ n')> = 0 ){
- Throw new ZygoteStartFailedEx (
- "Embedded newlines not allowed ");
- }
- SZygoteWriter. write (arg );
- SZygoteWriter. newLine ();
- }
- SZygoteWriter. flush ();
- // Shocould there be a timeout on this?
- Pid = sZygoteInputStream. readInt ();
- If (pid <0 ){
- Throw new ZygoteStartFailedEx ("fork () failed ");
- }
- } Catch (IOException ex ){
- ......
- }
- Return pid;
- }
- ......
- }
- Here, sZygoteWriter is a Socket write stream opened by the openZygoteSocketIfNeeded function:
- [Java] view plaincopypublic class Process {
- ......
- /**
- * Tries to open socket to Zygote process if not already open. If
- * Already open, does nothing. May block and retry.
- */
- Private static void openZygoteSocketIfNeeded ()
- Throws ZygoteStartFailedEx {
- Int retryCount;
- If (sPreviousZygoteOpenFailed ){
- /*
- * If we 've failed before, please CT that we'll fail again and
- * Don't pause for retries.
- */
- RetryCount = 0;
- } Else {
- RetryCount = 10;
- }
- /*
- * See bug #811181: Sometimes runtime can make it up before zygote.
- * Really, we 'd like to do something better to avoid this condition,
- * But for now just wait a bit...
- */
- For (int retry = 0
- ; (SZygoteSocket = null) & (retry <(retryCount + 1 ))
- ; Retry ++ ){
- If (retry> 0 ){
- Try {
- Log. I ("Zygote", "Zygote not up yet, sleeping ...");
- Thread. sleep (ZYGOTE_RETRY_MILLIS );
- } Catch (InterruptedException ex ){
- // Shocould never happen
- }
- }
- Try {
- SZygoteSocket = new LocalSocket ();
- SZygoteSocket. connect (new LocalSocketAddress (ZYGOTE_SOCKET,
- LocalSocketAddress. Namespace. RESERVED ));
- SZygoteInputStream
- = New DataInputStream (sZygoteSocket. getInputStream ());
- SZygoteWriter =
- New BufferedWriter (
- New OutputStreamWriter (
- SZygoteSocket. getOutputStream ()),
- 256 );
- Log. I ("Zygote", "Process: zygote socket opened ");
- SPreviousZygoteOpenFailed = false;
- Break;
- } Catch (IOException ex ){
- ......
- }
- }
- ......
- }
- ......
- }
This Socket is listened by the ZygoteInit class in the frameworks/base/core/java/com/android/internal/OS/ZygoteInit. java file in the runSelectLoopMode function.