In an earlier session, we analyzed the startup init process.
There were three main things in startup init process.
One of them was to create the zygote process.
Right.
So what exactly is zygote process and what does it do?
In this session, we will cover zygote and its startup process.
We'll also walk through the code and see how zygote starts up the system server and also how it starts
a new process when activity management service request to do so.
In a previous lecture, we discussed about the process and we have seen how it starts the zygote process.
We also walk through the code, right?
So in this session we will start right from there and continue with the zygote process.
We'll see how zygote starts the system server and wait for the request from activity management service
or aims to create a new process.
In Android, the Android runtime or the Dalvik virtual machine, the application process and the system
server process that runs the key services of system are all created by the zygote process.
It creates the application process and the system server process using fork mechanism, so forth mechanism
is nothing, but it creates a new process by duplicating the existing process from which it is called.
So since zygote process will create a dynamic virtual machine or Android runtime when it starts, the
application process and the system process created by this mechanism can automatically obtain a copy
of the Android runtime instance internally.
So let's look at the code and see how zygote starts up and how it calls the start system server and
then how it waits for AMS or activity management services.
So in it dot zygote 64 RC So we had looked at this file in our previous session.
So this file is basically a configuration for zygote process.
As I said earlier in, it is a very simple language and there are some commands, some import statements.
So here you can see in this file it says to create a service named zygote with this process.
So this is the this is basically the program which it has to execute.
And then later we are parsing some parameters.
So here we have the start system server parameter, then zygote parameter, and then we are sending
some path.
In the second line there is a statement Class Main.
So this says that the class name of this particular program or zygote is Main.
So the zygote or this service is referenced in the RC files with the name Main.
So in init programming language there is one file called init dot RC, which is a kind of main function.
So here you can find that.
So this is a big file.
So this is very similar to our main function within Java or C++ or C language.
You have we have this main function, right, which executes at the start.
So this is very similar to that.
So in a dot, RC is the main file or main function which gets executed.
And this will do all the import and all the operations and it will also call these RC files internally.
So if you come down, you can see here that class start main.
So this is basically a command to start a class named as Main.
So this will basically start the zygote.
Okay.
So this will trigger the statement will basically start this configuration or this process.
Okay.
So now this process will start at process 64, right?
So the app process is T4 basically points to app main dot CP.
So it will start executing from this main function.
As you can see, the main function basically does a lot of initialization and it will also loop through
the argument which is parsed in from here.
So these arguments basically do loop through those arguments and it will set many variables.
So here you can see the START system server is initialized to false first and then it checks.
If it has a parameter, then it sets its true.
True.
So this basically says to start the system server or not and then it checks if it is true, then add
this argument and at the bottom if it is zygote, then start the runtime with this package name and
pass this arguments.
So this is basically passing these arguments to this Java package.
So this is a Java package.
You can see this package name, right?
It seems to be a Java package and it's it's a Java package.
Okay.
We'll look at it soon.
So and it's basically parsing all these parameters.
And then what exactly is runtime?
So runtime then object of app runtime.
You can see the app runtime inherits from Android runtime.
It's basically running the start function within Android runtime.
So this file start function should be from Android runtime.
So let's go to Android runtime here.
You can see the start function, right?
So this is the one which is going to get called the so first one class name is nothing but.
The zygote in it.
Last name.
So this is the one.
So in the start process of Android runtime, you can see it takes in all these parameters which is passed
from App Main and it does a lot of initialization here.
And when we come down we can see start VM.
So it starts the virtual machine.
So this basically starts the Java virtual machine and the START rig.
This function is basically to register the JNI for the Android runtime or the Dalvik virtual machine.
So this is basically registers the Android functions and this starts the virtual machine.
And then if you come down, you can see here it's trying to find the main function or it's trying to
call the main function.
So this will eventually core zygote init plus.
So then the control goes to zygote in it, this one.
So from here it goes to the Java layer.
So this communication happens through JNI and once we are in zygote in it.
So let's start from the main.
So here so the arguments are being passed.
So this is the same argument which comes from here.
And then you come down, you can see it does similar things, like it goes through the arguments and
it picks up the START system server and it configures the START system.
So over to true.
If you come further down here, you can see it's starting the system server by forking it.
So here, if this value is true, it's true because we have passed that.
So then it's true and it forks the system server here.
So this is the point where we start the system server and further if you come down we can see that it
runs a select loop and if we go inside this, so this is within zygote server dot java.
So here we can see there is a infinite loop.
So this loop will be always running and it's basically waiting for some client request.
So this will basically wait for the activity manager service to request to create a new child process.
So this infinite loop is basically responsible.
Whenever a new application has to be started, the Activity Management Service will basically request,
the request will basically come here and it will start the process.
So now we have seen how the system server starts up so from here and then finally how it gets into a
loop, an infinite loop and waits for a request.
So to summarize, the zygote process does the following steps.
So it creates an app runtime from here and calls the start method to start the zygote process.
And from here it creates the Dalvik virtual machine.
And registers the janai for the DBM and then it calls the zygote init process.
So from here the control goes to the Java world, so the control comes to java init here and this process
is through JNI and then here does the same thing.
It goes through the arguments and initializes some of the variables and finally it forks or starts the
system server and then here it gets into an infinite loop and this will wait forever for the request
from Activity Management Service.
So that's all about the zygote process.
I hope you learn something out of this video and if you like the video, please subscribe so that I
can continue creating videos.
Thank you.