Android應用程式進程啟動過程的原始碼分析(四)

來源:互聯網
上載者:User

上文中的函數將建立進程的參數放到argsForZygote列表中去。

如參數"--runtime-init"表示要為新建立的進程初始化執行階段程式庫,然後調用zygoteSendAndGetPid函數進一步操作。

Step 4. Process.zygoteSendAndGetPid

這個函數定義在frameworks/base/core/java/android/os/Process.java檔案中:

 
  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. * After the zygote process reads these it will write the pid of 
  14. * the child or -1 on failure. 
  15. */ 
  16. sZygoteWriter.write(Integer.toString(args.size())); 
  17. sZygoteWriter.newLine(); 
  18. int sz = args.size(); 
  19. for (int i = 0; i < sz; i++) { 
  20. String arg = args.get(i); 
  21. if (arg.indexOf('\n') >= 0) { 
  22. throw new ZygoteStartFailedEx( 
  23. "embedded newlines not allowed"); 
  24. sZygoteWriter.write(arg); 
  25. sZygoteWriter.newLine(); 
  26. sZygoteWriter.flush(); 
  27. // Should there be a timeout on this? 
  28. pid = sZygoteInputStream.readInt(); 
  29. if (pid < 0) { 
  30. throw new ZygoteStartFailedEx("fork() failed"); 
  31. } catch (IOException ex) { 
  32. ...... 
  33. return pid; 
  34. ...... 
  35. 這裡的sZygoteWriter是一個Socket寫入流,是由openZygoteSocketIfNeeded函數開啟的: 
  36. [java] view plaincopypublic class Process { 
  37. ...... 
  38. /** 
  39. * Tries to open socket to Zygote process if not already open. If 
  40. * already open, does nothing. May block and retry. 
  41. */ 
  42. private static void openZygoteSocketIfNeeded() 
  43. throws ZygoteStartFailedEx { 
  44. int retryCount; 
  45. if (sPreviousZygoteOpenFailed) { 
  46. /* 
  47. * If we've failed before, expect that we'll fail again and 
  48. * don't pause for retries. 
  49. */ 
  50. retryCount = 0; 
  51. } else { 
  52. retryCount = 10; 
  53. /* 
  54. * See bug #811181: Sometimes runtime can make it up before zygote. 
  55. * Really, we'd like to do something better to avoid this condition, 
  56. * but for now just wait a bit... 
  57. */ 
  58. for (int retry = 0 
  59. ; (sZygoteSocket == null) && (retry < (retryCount + 1)) 
  60. ; retry++ ) { 
  61. if (retry > 0) { 
  62. try { 
  63. Log.i("Zygote", "Zygote not up yet, sleeping..."); 
  64. Thread.sleep(ZYGOTE_RETRY_MILLIS); 
  65. } catch (InterruptedException ex) { 
  66. // should never happen 
  67. try { 
  68. sZygoteSocket = new LocalSocket(); 
  69. sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, 
  70. LocalSocketAddress.Namespace.RESERVED)); 
  71. sZygoteInputStream 
  72. = new DataInputStream(sZygoteSocket.getInputStream()); 
  73. sZygoteWriter = 
  74. new BufferedWriter( 
  75. new OutputStreamWriter( 
  76. sZygoteSocket.getOutputStream()), 
  77. 256); 
  78. Log.i("Zygote", "Process: zygote socket opened"); 
  79. sPreviousZygoteOpenFailed = false; 
  80. break; 
  81. } catch (IOException ex) { 
  82. ...... 
  83. ...... 
  84. ...... 

這個Socket由frameworks/base/core/java/com/android/internal/os/ZygoteInit.java檔案中的ZygoteInit類在runSelectLoopMode函數偵聽的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.