6.分析具体的调用过程(很痛苦)
app_main 调用 zygoteInit
p { margin-bottom: 0.21cm; }
/ Next arg is startup classname or "--zygote"
if ( i < argc ) {
arg = argv [ i ++];
if ( 0 == strcmp ( "--zygote" , arg )) {
bool startSystemServer = ( i < argc ) ?
strcmp ( argv [ i ], "--start-system-server" ) == 0 : false ;
setArgv0 ( argv0 , "zygote" );
set_process_name ( "zygote" );
runtime. start ( "com.Android.internal.os.ZygoteInit" ,
startSystemServer );
} else {
set_process_name ( argv0 );
runtime. mClassName = arg ;
// Remainder of args get passed to startup class main ()
runtime. mArgC = argc - i ;
runtime. mArgV = argv + i ;
LOGV ( "App process is starting with pid=%d, class=%s. \n " ,
getpid (), runtime. getClassName ());
runtime. start ();
}
}
Zygote 分裂出 system_server
p { margin-bottom: 0.21cm; }
public static void main ( String argv []) {
try {
// Start profiling the zygote initialization.
if ( argv [ 1 ] . equals ( "true" )) {
startSystemServer ();
}
Log. i ( TAG, "Accepting command socket connections" );
if ( ZYGOTE_FORK_MODE ) {
runForkMode ();
} else {
runSelectLoopMode ();
}
}
frameworks/base/core/java/com/android/internal/os/
ZygoteInit.java
p { margin-bottom: 0.21cm; }
private static boolean startSystemServer()
throws MethodAndArgsCaller, RuntimeException {
/* Hardcoded command line to start the system server */
try {
/* Request to fork the system server process */
pid = Zygote. forkSystemServer (
parsedArgs. uid , parsedArgs. gid ,
parsedArgs. gids , debugFlags, null );
} catch ( IllegalArgumentException ex) {
throw new RuntimeException (ex);
}
/* For child process */
if (pid == 0 ) {
handleSystemServerProcess(parsedArgs);
}
return true ;
}
1.如果大家还能看到这里请复习一下 Linux的两个系统调用 fork和exec
frameworks/base/services/java/com/android/server/SystemService.java
p { margin-bottom: 0.21cm; }
public static void main ( String [] args ) {
if ( SamplingProfilerIntegration. isEnabled ()) {
SamplingProfilerIntegration. start ();
timer = new Timer ();
timer . schedule ( new TimerTask () {
@ Override
public void run () {
SamplingProfilerIntegration. writeSnapshot ( "system_server" );
}
} , SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL );
}
VMRuntime. getRuntime () . setTargetHeapUtilization ( 0 .8f );
System . loadLibrary ( "android_servers" );
init1 ( args );------------------>
}
public static final void init2 () {
Log. i ( TAG, "Entered the Android system server!" );
Thread thr = new ServerThread ();
thr. setName ( "android.server.ServerThread" );-------------->
thr.start();
}
红色的部分最后会执行我们上面列出的jni代码
frameworks/base/cmds/system_server/
library/system_init.cpp
Init2 这里启动了java的system service
p { margin-bottom: 0.21cm; }
class ServerThread extends Thread {
@ Override
public void run () {
Log. i ( TAG, "System Content Providers" );
ActivityManagerService. installSystemProviders ();
Log. i ( TAG, "Battery Service" );
battery = new BatteryService ( context );
ServiceManager. addService ( "battery" , battery );
Log. i ( TAG, "Hardware Service" );
hardware = new HardwareService ( context );
ServiceManager. addService ( "hardware" , hardware );
Log. i ( TAG, "Alarm Manager" );
AlarmManagerService alarm = new AlarmManagerService ( context );
ServiceManager. addService ( Context . ALARM_SERVICE , alarm );
}
}
原创文章欢迎转载,转载请注明住处rickleaf
7. 整理Android系统启动流程
至此System Service的服务环境启动起来了
8. 最后引用霍金的一句话:“懂与不懂都是收获”
