In the previous section, the http://www.bkjia.com/kf/201203/123062.html talked about using Zygote. forkSystemServer to generate the SystemServer process using fork. What did the process do?
The following describes the handleSystemServerProcess process.
The code snippet is as follows:
/* Request to fork the system server process */
Pid = Zygote. forkSystemServer (
ParsedArgs. uid, parsedArgs. gid,
ParsedArgs. gids, debugFlags, null );
/* For child process */
If (pid = 0 ){
HandleSystemServerProcess (parsedArgs );
}
-->
Private static void handleSystemServerProcess (
ZygoteConnection. Arguments parsedArgs)
{
// Disable socket inherited from Zygote
CloseServerSocket ();
// Call the zygoteInit function.
RuntimeInit. zygoteInit (parsedArgs. remainingArgs );
}
RuntimeInit. java @ frameworks \ base \ core \ java \ com \ android \ internal \ OS
Public static final void zygoteInit (String [] argv ){
// Call the JNI function here.
ZygoteInitNative ();
// Call the main function of startClass com. android. server. SystemServer.
InvokeStaticMain (startClass, startArgs );
}
-->
ZygoteInitNative @ frameworks \ base \ core \ jni
Call onZygoteInit @ frameworks \ base \ cmds \ app_process \ app_main.cpp
Virtual void onZygoteInit ()
{
Sp <ProcessState> proc = ProcessState: self ();
If (proc-> supportsProcesses ()){
LOGV ("App process: starting thread pool. \ n ");
Proc-> startThreadPool (); // start the thread to establish binder Communication
}
}
OK, this process is clear. Next we will focus on how the native system_server process starts?
Main @ frameworks \ base \ services \ java \ com \ android \ server \ SystemServer. java
Public static void main (String [] args ){
System. loadLibrary ("android_servers ");
Init1 (args );
}
Here init1 is a native function:
System_init @ frameworks \ base \ cmds \ system_server \ library \ system_init.cpp
Extern "C" status_t system_init ()
{
LOGI ("Entered system_init ()");
// Start the SurfaceFlinger
SurfaceFlinger: instantiate ();
Runtime-> callStatic ("com/android/server/SystemServer", "init2 ");
...
}
Here we call
Init2 @ frameworks \ base \ services \ java \ com \ android \ server \ SystemServer. java
Public static final void init2 (){
Thread thr = new ServerThread ();
Thr. setName ("android. server. ServerThread ");
Thr. start ();
}
A ServerThread thread is started and its start () function is called.
Run () function of the ServerThread
Public void run (){
Slog. I (TAG, "Entropy Service ");
ServiceManager. addService ("entropy", new EntropyService ());
Slog. I (TAG, "Power Manager ");
Power = new PowerManagerService ();
ServiceManager. addService (Context. POWER_SERVICE, power );
...
// Finally, it enters the loop message loop to process the message ....
Logoff. loop ();
Slog. d (TAG, "System ServerThread is exiting! ");
}
Oh, it turns out that many important system services are added to ServiecManager in sequence, and the entire SystemServer process has been started.
To sum up, the important execution process is as follows:
From andyhuabing's column