SoFunction
Updated on 2025-04-06

Android FrameWork SystemServer Process Fork Example

1. Linux fork

On the Linux platform, we can create a new process through the fork system call. This new process will have a copy of the original process, including code, data, memory, etc. The only difference is that the new process has a new ID, making it a separate process running its own code.

The fork() system call will return twice, the process ID will be returned in the original process, and the new process will be returned 0. Two processes can perform the same task or execute different codes as needed.

Fork example

#include <>
#include <>
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

We see that main start is printed only once, and the child process starts forking execution after fork.

The current pid in the second line is 11647, and the result of fork is 11648, indicating that it is currently executing in the parent process.

The current pid in the third line is 11648, which happens to be the process ID returned by the parent process fork. The result is 0, which also means that the printing is executed in the child process.

2. SystemServer process fork

Then analyze the source code of ZygoteInit last time.

frameworks/base/core/java/com/android/internal/os/

  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.
              //If it is executed in the system_server process, return Runnable and execute the function                if (r != null) {
                    ();
                    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 (args);
            (parsedArgs);
            (parsedArgs);
            /* Request to fork the system server process */
            //Request the fork system server process here            pid = (
                    , ,
                    ,
                    ,
                    null,
                    ,
                    );
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        }
        /* For child process */
      //If the pid is 0, it means it is executed in the child process. Therefore, the Socket inherited by the zygote process is turned off.        if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }
            ();
            return handleSystemServerProcess(parsedArgs);
        }

Call the forkSystemServe function of the Zygote class to request the fork child process. If the pid is 0, it means it is executed in the child process. Therefore, the Socket inherited by the zygote process is turned off.

Code 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);

The nativeForkSystemServer method is called in the forkSystemServer function. Here, the Native function needs to be called through JNI

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();
.....
}

In the ForkAndSpecializeCommon function, we see the system call of fork(), so that the system server process will be forked.

In the initial code, we saw that after the final call to forkSystemServer, a Runnable r object was returned.

      Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
          // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
                // child (system_server) process.
              //If it is executed in the system_server process, return Runnable and execute the function                if (r != null) {
                    ();
                    return;
                }

If it is executed in the system_server process, a Runnable r object will be returned and the() function will be executed. That is, the main execution process of the system_server process will be in the Runnable run function

The above is the detailed content of the SystemServer process fork example of Android FrameWork. For more information about Android FrameWork SystemServer, please follow my other related articles!