Linux系统里,有些进程只有kernel部分的代码,即由一个kernel函数进入,在sched的时候,将其与用户进程同等对待。

PID为0的叫swapper或sched进程,对应函数为rest_init

init进程PID为1,

kthreadd进程PID为2,

0号进程,即rest_init主体如下

 static noinline void __init_refok rest_init(void)
{
int pid; rcu_scheduler_starting();
smpboot_thread_init();
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
kernel_thread(kernel_init, NULL, CLONE_FS);
numa_default_policy();
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
rcu_read_lock();
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
rcu_read_unlock();
complete(&kthreadd_done); /*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current);
schedule_preempt_disabled();
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE);

先是rcu_scheduler_starting函数,这个函数的作用是

再是smpboot_thread_init函数,这个函数注册smpboot时候的notifier,响应CPU动作

 static struct notifier_block smpboot_thread_notifier = {
.notifier_call = smpboot_thread_call,
.priority = CPU_PRI_SMPBOOT,
}; void __cpuinit smpboot_thread_init(void)
{
register_cpu_notifier(&smpboot_thread_notifier);
}

然后调用kernel_thread函数fork init进程

 /*
* Create a kernel thread.
*/
pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
{
return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
(unsigned long)arg, NULL, NULL);
}

do_fork函数被调用

这里有一点要注意,init进程执行的时候要等kerneladd进程执行后才被complete(&kthreadd_done)唤醒,然后真正执行,不然直接调度到init的话会OOPs

numa_default_policy被调用设置内存numa默认策略

然后调用kernel_thread函数fork kthreadd进程

然后用find_task_by_pid_ns(pid, &init_pid_ns)获取kthreadd的task_struct

最后唤醒complete(&kthreadd_done)进程

最后,0号进程,即空闲进程,要让一切跑起来,调用一次schedule

Kernel rest_init相关的更多相关文章

  1. Linux kernel驱动相关抽象概念及其实现 之“bus,device,driver”

    bus,device,driver三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/642 ...

  2. Linux kernel驱动相关抽象概念及其实现 之“linux设备模型kobject,kset,ktype”

    kobject,kset,ktype三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/64 ...

  3. linux kernel笔记

    文章目录 关于linux内核中的__attribute__关键字 Linux kernel启动参数 gdt / ldt PCB 关于linux内核中的__attribute__关键字 part I: ...

  4. Linux kernel parameter command line设置

    现在CPU2核以上比较普遍了,平时用linux上上网可能用不着双核甚至4核,大部分发行版内核都启用了CPU_HOTPLUG,到/sys/devices/system/cpu下可以看到文件夹cpu0.c ...

  5. OpenSUSE 内核编译教程 (kernel 2.6.x)

    http://cn.opensuse.org/OpenSUSE_%E5%86%85%E6%A0%B8%E7%BC%96%E8%AF%91%E6%95%99%E7%A8%8B_(kernel_2.6.x ...

  6. LINUX KERNEL启动参数

    LINUX KERNEL启动参数 在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时 ...

  7. Linux kernel启动选项(参数)(转)

    Linux kernel启动选项(参数)  转载链接https://www.cnblogs.com/linuxbo/p/4286227.html 在Linux中,给kernel传递参数以控制其行为总共 ...

  8. Linux kernel启动选项(参数)

    在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...

  9. /etc/sysctl.conf 控制内核相关配置文件

    /etc/sysctl.conf 用于控制内核相关的配置参数,而且它的内容全部是对应于 /proc/sys/ 这个目录的子目录及文件 [root@MongoDB ~]# ll /proc/sys to ...

随机推荐

  1. git的初步了解

    其实git我也不熟,请两天假回来宿友告诉我,我们有一份高大尚的作业.我问她们才知道原来是让我们以博客的形式写两份作业交上去.git还是我在网上查找到的,才对它有一些的了解. git是一个开源分布式版本 ...

  2. Leetcode#56 Merge Intervals

    原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...

  3. Cross Validation done wrong

    Cross Validation done wrong Cross validation is an essential tool in statistical learning 1 to estim ...

  4. 制作一个可以给team所有成员用的开发者证书

    1. 将证书的private key导出一个.p12的文件给别人 2. 从apple developer网站下载证书对应的provisioning profile 待优化

  5. Reactor构架模式

    http://www.cnblogs.com/hzbook/archive/2012/07/19/2599698.html Reactor框架是ACE各个框架中最基础的一个框架,其他框架都或多或少地用 ...

  6. 最小PE文件讨论

    1.实例1国外的人写的最小的PE文件--97Bytes 4D5A0000504500004C0101006A2A58C30000000000000000040003010B01080004000000 ...

  7. The life of an HTML HTTP request

    https://developer.mozilla.org/en-US/docs/The_life_of_an_HTML_HTTP_request http://www.html5rocks.com/ ...

  8. applicationContext.xml xxx-servlet.xml

    applicationContext.xml是随ContextLoaderListener的加载而执行的,而xxx-servlet.xml是随DispatcherServlet的加载而执行的,在web ...

  9. (转载)李剑英的CSLight入门指南结合NGUI热更新

    原地址:http://www.xuanyusong.com/archives/3075 李剑英的CSLight入门指南文档撰写者:GraphicQQ: 1065147807 一. CSLIGHT 作者 ...

  10. PHP PDO_MYSQL 操作类 YAF嵌入高性能类而准备

    https://github.com/indieteq/PHP-MySQL-PDO-Database-Class PHP-PDO-MySQL-Class A PHP MySQL PDO class s ...