[linux] Detailed explanation of the init process

overview

Note: init进程和init程序(linuxrc程序)是有区别的. The init process exists from the beginning, it runs in the kernel state, and belongs to a kernel thread. Later, after the init process mounts the root file system and runs the application init program, the init process changes from the kernel state to the user state. Because the process number has not changed during the transformation process, it is still process 1, so some people will regard the init program (linuxrc program) as process 1. But in fact, in addition to the later init program, the init process also includes operations such as mounting the root file system in kernel mode.

The init process completes the transition from kernel mode to user mode

insert image description here
From the above figure, you can see that Init runs from kernel space first, and then switches to user space to run

(1) A process has two states successively

When the init process first started running, it was in the kernel state, which belonged to a kernel thread, and then after running a program under the user state, it forcibly converted itself into the user state (the following processes need to work in the user state).
The init process completes the transition from kernel mode to user mode, so other subsequent processes can work in user mode.

(2) The working content of the init process in kernel mode

主要是挂载根文件系统,并试图找到用户态下的那个init程序. (This sentence shows that the init process runs earlier than the init program.)

The init process must run an application program in user mode if it wants to convert itself into user mode. To run this application program, it must find this application program. To find this application program, it must mount the root file system, because all applications Programs are all in the file system.

All functions in the kernel source code are in the kernel state, and any one of them cannot be executed out of the kernel state. The application program must not belong to the kernel source code, so as to ensure that the application program is in user mode. The init program executed here is not together with the kernel, and is provided separately by the root file system.

(3) The work content of the init process in user mode

Most of the meaningful work of the init process is performed in user mode.
The significance of the init process to the operating system is that,

All other user processes are directly or indirectly derived from the init process

(4) How does the init process jump from kernel mode to user mode? Can you come back?

init进程处于内核态时,通过函数kernel_execve来执行一个用户空间编译链接的应用程序就跳跃到用户态了

  • The process number does not change during the jump, it is always process 1.
  • The jumping process is one-way. Once the init program is executed and transferred to the user mode, the entire operating system will actually run, and it can only work in the user mode in the future. If you want to enter the kernel mode in the user mode, you can only call the API.
  • The jumping process is 单向yes, once the init program is executed and transferred to the user mode, the entire operating system is really running, and it can only work in the user mode in the future. If you want to enter the kernel mode in the user mode, you can only call the API.

1. The init process mounts the root file system

(1) The prepare_namespace function mounts the root file system.

(2) Where is the root file system? What is the file system type of the root file system?

Uboot tells the kernel this information by passing parameters.
The sentence root=/dev/mmcblk0p2 rw in the uboot parameter is to tell the kernel where the root file system is.
The sentence rootfstype=ext3 in the uboot parameter is to tell the kernel the type of rootfs.

(3) Mounting result
insert image description here
If the kernel mounts the root file system successfully, it will print: VFS: Mounted root (ext3 filesystem) on device 179:2. (Other numbers may also be possible)
If the root file system fails to mount, it will print: No filesystem could mount root, tried: yaffs2
(4) If the rootfs fails to mount when the kernel starts, it cannot be executed later.

The kernel has set up a mechanism to automatically restart after a 5s break after startup failure, so it will automatically restart here, so sometimes you will see repeated restarts.
(5) If mounting rootfs fails, the possible reasons are

The most common mistake is that the bootargs of uboot is not set correctly.
Rootfs burning failed (fastboot burning is not easy to make mistakes).
Rootfs itself fails to make.

2. The init process executes the init program to complete the transition from kernel state to user state

insert image description here

(1) Once the rootfs is successfully mounted, enter the rootfs to find the init program of the application (in the init_post() function), and use run_init_process to execute it after finding it.

(2) If you determine who the init program is?

First check whether it is specified from the uboot parameter cmdline, and if it is specified, execute the program specified in the cmdline first. For example, init=/linuxrc means that the linuxrc program in the root directory of rootfs is the init program.
If there is no init=xx in the uboot parameter cmdline or the execution of the xx specified in the cmdline fails, there is a backup solution. The first backup: /sbin/init, the second backup: /etc/init, the third backup: /bin/init, the fourth backup: /bin/sh. If none of the above are successful, there is no other way.
init=/linuxrc generally points to busybox.

3. The init process builds the user interface

(1) The init process is the ancestor of other user processes.

The creation of a process in the Linux system is created through its parent process. According to this theory, as long as there is one parent process, a bunch of descendant processes can be born.
(2) init starts the login process (user login process), command line process (provides command line environment), and shell process (provides command interpretation and execution).

(3) The shell process starts other user processes.

Once the command line and the shell work, the user can execute other applications through the ./xx method under the command line, and the operation of each application is a process.

4. The init process opens the console

insert image description here
(1) Each process in the Linux system has its own file descriptor table, which stores the files opened by the process.

(2) Everything in the Linux system is a file, so the device is also accessed as a file. To access a device, the file descriptor corresponding to the device must be opened. For example, the device file /dev/fb0 represents the LCD display device, /dev/buzzer represents the buzzer device, and /dev/console represents the console device.

(3) The /dev/console file is opened here, and the file descriptor is copied twice, and a total of 3 file descriptors are obtained. These three file descriptors are 0, 1, and 2, which are the so-called standard input, standard output, and standard error.

(4) Process 1 opens these 3 file descriptors, so all processes derived from process 1 have these 3 file descriptors by default.

reference

Detailed explanation of the init process
Analysis of the init process of Linux startup process analysis

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/130660743