Linux kernel startup process-second stage rest_init function

1. Linux kernel startup

The previous article gave a brief understanding of the second phase of Linux kernel startup, involving the start_kernel function. The start_kernel function finally calls the rest_init function. Next, let’s take a brief look at the rest_init function.

This article continues the study of the previous article, the address is as follows:

Linux kernel startup process-second stage start_kernel function_Ling Xiaozhan's blog-CSDN blog

2. The second stage of the Linux kernel startup process

1. rest_init function

The rest_init function is defined in the file init/main.c . The function content is as follows:

383 static noinline void __init_refok rest_init(void)
384 {
385 int pid;
386
387 rcu_scheduler_starting();
388 smpboot_thread_init();
389 /*
390 * We need to spawn init first so that it obtains pid 1, however
391 * the init task will end up wanting to create kthreads, which, 
392 * if we schedule it before we create kthreadd, will OOPS.
393 */
394 kernel_thread(kernel_init, NULL, CLONE_FS);
395 numa_default_policy();
396 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
397 rcu_read_lock();
398 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
399 rcu_read_unlock();
400 complete(&kthreadd_done);
401
402 /*
403 * The boot idle thread must execute schedule()
404 * at least once to get things moving:
405 */
406 init_idle_bootup_task(current);
407 schedule_preempt_disabled();
408 /* Call into cpu_idle with preempt disabled */
409 cpu_startup_entry(CPUHP_ONLINE);
410 }

Line 387 calls the function rcu_scheduler_starting to start the RCU lock scheduler.
Line 394 calls the function kernel_thread to create the kernel_init process, which is the famous init kernel process.
The PID of the init process is 1 . The init process is a kernel process at the beginning ( that is, running in the kernel mode ) . Later, the init process will be in the root process.
Find the program named " init " in the file system . This " init " program is in user mode. By running this " init " program
sequence, the init process will realize the transition from kernel mode to user mode.
Line 396 calls the function kernel_thread to create the kthreadd kernel process. The PID of this kernel process is 2 . The kthreadd process is responsible for the scheduling and management of all kernel processes.
Line 409 , finally calls the function cpu_startup_entry to enter the idle process. cpu_startup_entry will call cpu_idle_loop . cpu_idle_loop is a while loop, which is the idle process code. The PID of the idle process is 0 , idle
The process is called an idle process.
The idle process , when the CPU has nothing to do, will "hang around" in the idle process. Anyway, it is just to find something for the CPU to do. When other processes want to work, they will seize the idle process and seize the CPU usage rights. In fact, you should be able to see that the idle process is not created using the kernel_thread or fork function, because it evolved from the main process.

After the development board is powered on, enter the "ps -A" command to print out all processes in the current system, where you can see the init process and kthreadd process, as shown in the following figure:

It can be seen that the PID of the init process is 1 and the PID of the kthreadd process is 2 . The reason why the idle process with PID 0 is not shown in the above picture is because the idle process is a kernel process.

Let's focus on the init process next. kernel_init is the process function of the init process.

Guess you like

Origin blog.csdn.net/wojiaxiaohuang2014/article/details/133286117