Source code analysis of Android Application Process Startup Process (4)

Source: Internet
Author: User

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:

 
 
  1. [Java] view plaincopypublic class Process {
  2. ......
  3. Private static int zygoteSendArgsAndGetPid (ArrayList args)
  4. Throws ZygoteStartFailedEx {
  5. Int pid;
  6. OpenZygoteSocketIfNeeded ();
  7. Try {
  8. /**
  9. * See com. android. internal. OS. ZygoteInit. readArgumentList ()
  10. * Presently the wire format to the zygote process is:
  11. * A) a count of arguments (argc, in essence)
  12. * B) a number of newline-separated argument strings equal to count
  13. *
  14. * After the zygote process reads these it will write the pid
  15. * The child or-1 on failure.
  16. */
  17. SZygoteWriter. write (Integer. toString (args. size ()));
  18. SZygoteWriter. newLine ();
  19. Int sz = args. size ();
  20. For (int I = 0; I <sz; I ++ ){
  21. String arg = args. get (I );
  22. If (arg. indexOf ('\ n')> = 0 ){
  23. Throw new ZygoteStartFailedEx (
  24. "Embedded newlines not allowed ");
  25. }
  26. SZygoteWriter. write (arg );
  27. SZygoteWriter. newLine ();
  28. }
  29. SZygoteWriter. flush ();
  30. // Shocould there be a timeout on this?
  31. Pid = sZygoteInputStream. readInt ();
  32. If (pid <0 ){
  33. Throw new ZygoteStartFailedEx ("fork () failed ");
  34. }
  35. } Catch (IOException ex ){
  36. ......
  37. }
  38. Return pid;
  39. }
  40. ......
  41. }
  42. Here, sZygoteWriter is a Socket write stream opened by the openZygoteSocketIfNeeded function:
  43. [Java] view plaincopypublic class Process {
  44. ......
  45. /**
  46. * Tries to open socket to Zygote process if not already open. If
  47. * Already open, does nothing. May block and retry.
  48. */
  49. Private static void openZygoteSocketIfNeeded ()
  50. Throws ZygoteStartFailedEx {
  51. Int retryCount;
  52. If (sPreviousZygoteOpenFailed ){
  53. /*
  54. * If we 've failed before, please CT that we'll fail again and
  55. * Don't pause for retries.
  56. */
  57. RetryCount = 0;
  58. } Else {
  59. RetryCount = 10;
  60. }
  61. /*
  62. * See bug #811181: Sometimes runtime can make it up before zygote.
  63. * Really, we 'd like to do something better to avoid this condition,
  64. * But for now just wait a bit...
  65. */
  66. For (int retry = 0
  67. ; (SZygoteSocket = null) & (retry <(retryCount + 1 ))
  68. ; Retry ++ ){
  69. If (retry> 0 ){
  70. Try {
  71. Log. I ("Zygote", "Zygote not up yet, sleeping ...");
  72. Thread. sleep (ZYGOTE_RETRY_MILLIS );
  73. } Catch (InterruptedException ex ){
  74. // Shocould never happen
  75. }
  76. }
  77. Try {
  78. SZygoteSocket = new LocalSocket ();
  79. SZygoteSocket. connect (new LocalSocketAddress (ZYGOTE_SOCKET,
  80. LocalSocketAddress. Namespace. RESERVED ));
  81. SZygoteInputStream
  82. = New DataInputStream (sZygoteSocket. getInputStream ());
  83. SZygoteWriter =
  84. New BufferedWriter (
  85. New OutputStreamWriter (
  86. SZygoteSocket. getOutputStream ()),
  87. 256 );
  88. Log. I ("Zygote", "Process: zygote socket opened ");
  89. SPreviousZygoteOpenFailed = false;
  90. Break;
  91. } Catch (IOException ex ){
  92. ......
  93. }
  94. }
  95. ......
  96. }
  97. ......
  98. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.