I searched on the internet for which is the first process which gets executed upon system startup.
I found two answers which are init
and sched
. What is it really?
Which gets executed first? sched
process or init
process?
Typically it is the init
process, the path of which is hard coded into the kernel itself. init
performs very low level functions like starting upstart
in the case of Ubuntu (prior to 15.40) or systemd
in the case of Ubuntu 15.04 and later, Arch, Fedora, and others, which load the remaining processes and setup. Note that the system is not done booting when init
runs - that is a common misconception. In fact, init
sets up your login screen and other related tasks. Here's a WikiPedia page on init
: https://en.wikipedia.org/wiki/Linux_startup_process#SysV_init
Init is the father of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab. This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system. A run level is a software configuration of the system which allows only a selected group of processes to exist. The processes spawned by init for each of these run levels are defined in the /etc/inittab file.
However, the Linux kernel does start the scheduler but it is not in userspace, which is what most people associate as the home for a process. Also, the Bourne Shell (/bin/sh
) can be substituted if the init
is missing or cannot be called. You can also in theory substitute it for any executable by using the init=*some path here*
Linux kernel boot option.
Its sched
, as per Linux 3.13 start kernel() first calls sched_init() and runs first user space process init i.e rest_init() creates a kernel thread passing another function kernel_init() as the entry point and kernel goes to idle unless called.
start_kernel() {
...
sched_init();
rest_init(); calls function kernel_init();
}
The kernel at least has one runnable process, which is known as the idle task, swapper, init_task and sched. They are different names of the same process whose pid is 0. This init_task is a global variable of kernel, so it have a fixed address, you can see it from System.map by command grep 'D init_task' /boot/System.map-*
. The address wouldn't change unless you recompile the kernel.
The program init whose pid is 1, spawned by init_task(pid 0). In Ubuntu, The program init is the Upstart process management daemon, while in other systems, it could be systemd. The address of init changed every time while system rebooting.
So, process 0 run first, then spawn process 1.
You can try
pstree 0
it will show the all process hierarchy in tree form right from the children on sched
process (PID 0). No doubt init
is the parent of all process but sched
gets executed before init
and spaws both init
and kthread
.
You can also see the PPID (i.e. process id of parent process ) using:
ps -eaf
You will notice it to be 0 for both init
and kthread
.