Android FrameWork--SytemServer進(jìn)程fork

1、Linux的fork

在Linux平臺(tái)我們可以通過fork系統(tǒng)調(diào)用來(lái)創(chuàng)建一個(gè)新的進(jìn)程,這個(gè)新的進(jìn)程將會(huì)擁有原始進(jìn)程的一份副本,包括代碼、數(shù)據(jù)、內(nèi)存等等。唯一的區(qū)別是新的進(jìn)程擁有一個(gè)新的ID,使得它成為一個(gè)獨(dú)立的進(jìn)程,運(yùn)行自己的代碼。
fork()系統(tǒng)調(diào)用會(huì)返回兩次,在原始進(jìn)程中會(huì)返回進(jìn)程ID,在新的進(jìn)程中會(huì)返回0。兩個(gè)進(jìn)程可以執(zhí)行相同的任務(wù),也可以按照需要執(zhí)行不同的代碼。
fork的例子

#include <stdio.h>

#include <unistd.h>

int main(){

  printf("main start \n");

  int result = fork();

  pid_t pid = getpid();

  printf("pid is %d and result is %d\n",pid,result );

  return 0;

}
main start 
pid is 11647 and result is 11648
pid is 11648 and result is 0

我們看到main start只打印了一次,子進(jìn)程從fork之后開始分叉執(zhí)行。
第二行當(dāng)前pid是11647 ,fork的結(jié)果是11648,表示當(dāng)前在父進(jìn)程執(zhí)行
第三行當(dāng)前pid是11648,恰好是父進(jìn)程fork返回的進(jìn)程ID,result的結(jié)果是0,也說明了本次的打印是在子進(jìn)程中執(zhí)行的。

2、SystemServer進(jìn)程的fork

接著分析上次的ZygoteInit的源碼。
frameworks/base/core/java/com/android/internal/os/ZygoteInit.java

  if (startSystemServer) {
                Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
                // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
                // child (system_server) process.
              //如果是在system_server進(jìn)程中執(zhí)行,則返回Runnable ,并執(zhí)行r.run函數(shù)
                if (r != null) {
                    r.run();
                    return;
                }
            }
    /**
     * Prepare the arguments and forks for the system server process.
     *
     * Returns an {@code Runnable} that provides an entrypoint into system_server code in the
     * child process, and {@code null} in the parent.
     */
private static Runnable forkSystemServer(String abiList, String socketName,
            ZygoteServer zygoteServer) {
       int pid;

        try {
            parsedArgs = new ZygoteConnection.Arguments(args);
            ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
            ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);

            /* Request to fork the system server process */
            //在這里請(qǐng)求fork  system server  進(jìn)程
            pid = Zygote.forkSystemServer(
                    parsedArgs.uid, parsedArgs.gid,
                    parsedArgs.gids,
                    parsedArgs.debugFlags,
                    null,
                    parsedArgs.permittedCapabilities,
                    parsedArgs.effectiveCapabilities);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        }
        
        /* For child process */
      //如果pid為0,說明是在子進(jìn)程中執(zhí)行,因此關(guān)掉zygote進(jìn)程繼承下來(lái)的Socket
        if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }

            zygoteServer.closeServerSocket();
            return handleSystemServerProcess(parsedArgs);
        }

調(diào)用Zygote類的forkSystemServe函數(shù)請(qǐng)求fork子進(jìn)程,如果pid為0,說明是在子進(jìn)程中執(zhí)行,因此關(guān)掉zygote進(jìn)程繼承下來(lái)的Socket。繼續(xù)看forkSystemServer的代碼。

    public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
            int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
       ......
        int pid = nativeForkSystemServer(
                uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
        .......
        return pid;
    }
  native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
           int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);

在forkSystemServer函數(shù)中又調(diào)用了nativeForkSystemServer方法,這里要通過JNI調(diào)用Native函數(shù)
frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
jint com_android_internal_os_Zygote_nativeForkSystemServer
ForkAndSpecializeCommon

static jint com_android_internal_os_Zygote_nativeForkSystemServer(
        JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
        jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
        jlong effectiveCapabilities) {
  pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
                                      debug_flags, rlimits,
                                      permittedCapabilities, effectiveCapabilities,
                                      MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL,
                                      NULL, NULL, NULL);
  if (pid > 0) {
    .....
  }
  return pid;
}
// Utility routine to fork zygote and specialize the child process.
static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
                                     jint debug_flags, jobjectArray javaRlimits,
                                     jlong permittedCapabilities, jlong effectiveCapabilities,
                                     jint mount_external,
                                     jstring java_se_info, jstring java_se_name,
                                     bool is_system_server, jintArray fdsToClose,
                                     jintArray fdsToIgnore,
                                     jstring instructionSet, jstring dataDir) {
 ...........

  pid_t pid = fork();
.....
}

在ForkAndSpecializeCommon函數(shù)中我們看到了fork()的系統(tǒng)調(diào)用,這樣system server進(jìn)程就會(huì)被fork出來(lái)了。
而在最開始的代碼我們看到最終調(diào)用forkSystemServer之后返回了一個(gè)Runnable r對(duì)象。

      Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
          // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
                // child (system_server) process.
              //如果是在system_server進(jìn)程中執(zhí)行,則返回Runnable ,并執(zhí)行r.run函數(shù)
                if (r != null) {
                    r.run();
                    return;
                }

如果是在system_server進(jìn)程中執(zhí)行的話會(huì)返回一個(gè)Runnable r對(duì)象,并執(zhí)行r.run()函數(shù),也就是之后system_server進(jìn)程的執(zhí)行主要流程就在Runnable 的run函數(shù)中。
下一篇我們就介紹Runnable 的run函數(shù)中的主要執(zhí)行流程。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容