Linux system startup process and processes 0 and 1

Speaking of linux process 0 and process 1 involves the startup of the linux system, we start from the linux startup process.

1. The overall process of linux startup

  • When the system is first booted or rebooted, the processor will execute code from a known location. Corresponding to a personal computer, this place is the BIOS stored in the memory on the motherboard.

  • When a boot device is found, the first-stage bootloader is loaded into RAM and executed. This part of the bootloader is located in the 512-byte MBR image (MBR will be mentioned later), and its role is to load the second-stage bootloader.

  • When the second-stage bootloader is loaded into RAM and executed, the boot screen will be displayed, and Linux and the optional initial disk (temporary file system) are loaded into memory. When the image is loaded, control is passed from the second-stage bootloader to the kernel image.

  • The kernel image self-extracts and initializes. In this step, the second-stage bootloader will check the system hardware, enumerate the hardware devices, mount the main device, and load the necessary kernel modules.

  • When this is done, the first program in user space (init) starts executing, and thus the top-level system initialization begins.

Insert image description here

The above is like a shell of the Linux startup process, and then we start to explore the details of the startup process at a deeper level.

2. System startup

First, the BIOS composition is:

  • Power-on self-test code
  • Run service

Linux 0xffff0starts from the BIOS of the address. The first step is power-on self-test. The job of power-on self-test is to check the hardware. The second step is to enumerate and initialize local devices.

After POST, the POST code is cleared from memory, and running services are preserved and still valid for the target operating system.

To boot an operating system, the BIOS runtime CMOSsearches for active and bootable devices (which can be floppy disks, CD-ROMs, hard disk partitions, network devices, or USB flash drives) in the order defined by the settings.

Linux generally boots from a hard disk whose MBR contains a primary bootloader. MBR is a 512-byte sector located in the first sector of the hard disk (0 track 0 column 1 sector). After the MBR is loaded into RAM, it is up to the BIOS to control it.

3. The first stage boot program

The primary bootloader resides in a 512 byte MBR image.
The MBR image consists of a small partition table and code.

The first 446 bytes are the primary bootloader code, including execution code and error messages.

The next 64 bytes is a partition table, which contains four 16-byte partition records.

The last two bytes of the MBR define a magic number (0xaa55). This magic number is used to verify and check the MBR.

Insert image description here

The primary bootloader is mainly to find and load the second-stage bootloader. It looks for an active partition through the partition table. After finding an active partition table, it scans the remaining partitions to determine that they are not active. When these are determined, the boot boot record for the active partition is loaded from the device into RAM and executed.

4. The second stage boot program

The second-stage bootloader is actually more appropriate to call the kernel bootloader . Because its task is to load the Linux kernel and optional initial disk.

As the second stage is loaded, CRUB will display a list of available kernels (defined in /etc/grub.con, and symlinks /etc/grub/menu.lstto and ) on demand. /etc/grub.confYou can pick a kernel and improve it with additional kernel parameters.

After the second-stage bootloader is loaded into memory, it will query the file system and load the default kernel image and initrd image into memory. When all the images are ready, it will jump from the second stage to the kernel image.

5. Kernel stage

The kernel phase begins as the kernel image is loaded into memory and control is taken from the second-stage bootloader. A kernel image is not an executable kernel, but a compressed kernel image.

There is a small program routine at the head of the kernel image, which does a small amount of hardware setup, and then self-extracts the kernel image and puts it in high memory. The routine will then call the kernel to start the kernel boot.

6 init process

The init process is what we often call the No. 1 process.

The system allows a process to create a new process, and the new process is a child process. The child process can also create a new child process to form a process tree structure model. All processes in the entire Linux system are also in a tree structure. The tree root is automatically constructed by the system, which is process No. 0 executed in kernel mode. It is the ancestor of all processes. Process No. 0 creates process No. 1 (kernel state). No. 1 is responsible for performing part of the kernel initialization work and system configuration, and creates several kernel threads for cache and virtual main memory management . Subsequently, process No. 1 calls and execve()runs the executable program init, and evolves into user mode process No. 1, that is, the init process. It /etc/initabcompletes the system startup work according to the requirements of the configuration file and creates several terminal registration processes getty numbered No. 1, No. 2...

Each getty process sets its process group identification number and monitors the interface line configured to the system terminal. When a connection signal from the terminal is detected, the getty process will execute the registration program login through the function execve(). At this time, the user can enter the registration name and password to enter the login process. If successful, the login program will execute the shell through the function execve(). , the shell process receives the pid of the getty process and replaces the original getty process. Then the shell directly or indirectly spawns other processes.

The above process can be described as: 0号进程->1号内核进程->1号用户进程(init进程)->getty进程->shell进程, as shown in the figure below:

Insert image description here

reference documents

1、https://www.likecs.com/show-205059185.html

2、https://www.cnblogs.com/wanghetao/archive/2012/03/29/2422723.html

Guess you like

Origin blog.csdn.net/yuelai_217/article/details/130327715