内核的启动过程

本文深入探讨了Linux内核的启动过程,包括idle进程、kernel_init和kthreadd进程的创建及其相互关系,揭示了Linux系统启动的核心机制。


内核的启动过程——Linux内核分析课程

主要了解了CPU内核源码,看了大概的结构。

cd LinuxKernel/
qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img

内核启动完成后进入menu程序
执行结果如图

使用gdb跟踪调试内核

然后就是通过

(gdb)file linux-3.18.6/vmlinux

在gdb界面中targe remote之前加载符号表

(gdb)target remote:1234 

建立gdb和gdbserver之间的连接,按c 让qemu上的Linux继续运行

(gdb)break start_kernel 

设置断点
下面分析一下代码,了解内核的启动过程

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

这里面包含了三个进程idle,kernel_init,kthreadd。

其中idle进程(PID = 0), kernel_init进程(PID = 1),kthreadd(PID = 2)
kernel_init是所有用户态进程的祖先,kthreadd是所有内核线程的祖先。
三个进程之间的关系

  • idle进程由系统自动创建, 运行在内核态;
  • kernel_init进程由idle通过kernel_thread创建,在内核空间完成初始化后, 加载init程序, 并最终返回到用户空间;
  • kthreadd进程由idle通过kernel_thread创建,并始终运行在内核空间, 负责所有内核线程的调度和管理;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庭一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值