参考:

1.  Linux下1号进程的前世(kernel_init)今生(init进程)----Linux进程的管理与调度(六)

2. linux挂载根文件系统过程

3. BusyBox init工作流程

4. kthreadd-linux下2号进程

linux内核在启动的最后用kernel_thread生成两个内核线程:rest_init()会开启两个进程:kernel_init,kthreadd,之后主线程变成idle线程,init/main.c。

其中kernel_init内核线程转换为用户态1号进程init,原来的内核线程转换为idle内核线程。

/*
* We need to finalize in a non-__init function, or else race conditions
* between the root thread and the init thread may cause start_kernel to
* be reaped by free_initmem before the root thread has proceeded to
* cpu_idle.
*
* gcc-3.4 accidentally inlines this function, so use noinline.
*/
static __initdata DECLARE_COMPLETION(kthreadd_done); static noinline void __init_refok rest_init(void)
{
int pid; rcu_scheduler_starting();
/*
* 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 | CLONE_SIGHAND);
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);
preempt_enable_no_resched();
schedule(); /* Call into cpu_idle with preempt disabled */
preempt_disable();
cpu_idle();
}

kthread_init继续完成系统初始化工作,最后阶段调用init_post(),init_post()完成异步初始化并释放init内存,然后执行init代码,开启init进程。

linux到默认位置寻找init代码,大部分系统默认/sbin/init,若执行不成功,按以下顺序继续查找并执行:/etc/init, /bin/init, /bin/sh,若都不能找到,panic;

若能找到,并成功执行后,不会返回。

static int __init kernel_init(void * unused)
{
/*
* Wait until kthreadd is all set-up.
*/
wait_for_completion(&kthreadd_done);
/*
* init can allocate pages on any node
*/
set_mems_allowed(node_states[N_HIGH_MEMORY]);
/*
* init can run on any cpu.
*/
set_cpus_allowed_ptr(current, cpu_all_mask); cad_pid = task_pid(current); smp_prepare_cpus(setup_max_cpus); do_pre_smp_initcalls();
lockup_detector_init(); smp_init();
sched_init_smp(); do_basic_setup(); /* Open the /dev/console on the rootfs, this should never fail */
if (sys_open((const char __user *) "/dev/console", O_RDWR, ) < )
printk(KERN_WARNING "Warning: unable to open an initial console.\n"); (void) sys_dup();
(void) sys_dup();
/*
* check if there is an early userspace init. If yes, let it do all
* the work
*/ if (!ramdisk_execute_command)
ramdisk_execute_command = "/init"; if (sys_access((const char __user *) ramdisk_execute_command, ) != ) {
ramdisk_execute_command = NULL;
prepare_namespace();
} /*
* Ok, we have completed the initial bootup, and
* we're essentially up and running. Get rid of the
* initmem segments and start the user-mode stuff..
*/ init_post();
return ;
}
/* This is a non __init function. Force it to be noinline otherwise gcc
* makes it inline to init() and it becomes part of init.text section
*/
static noinline int init_post(void)
{
/* need to finish all async __init code before freeing the memory */
async_synchronize_full();
free_initmem();
mark_rodata_ro();
system_state = SYSTEM_RUNNING;
numa_default_policy(); current->signal->flags |= SIGNAL_UNKILLABLE; if (ramdisk_execute_command) {
run_init_process(ramdisk_execute_command);
printk(KERN_WARNING "Failed to execute %s\n",
ramdisk_execute_command);
}
/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* trying to recover a really broken machine.
*/
if (execute_command) {
run_init_process(execute_command);
printk(KERN_WARNING "Failed to execute %s. Attempting "
"defaults...\n", execute_command);
}
run_init_process("/sbin/init");
run_init_process("/etc/init");
run_init_process("/bin/init");
run_init_process("/bin/sh"); panic("No init found. Try passing init= option to kernel. "
"See Linux Documentation/init.txt for guidance.");
}

执行init采用的函数为run_init_process(),实调用kernel_execv()。

static const char * argv_init[MAX_INIT_ARGS+] = { "init", NULL, };
const char * envp_init[MAX_INIT_ENVS+] = { "HOME=/", "TERM=linux", NULL, }; static void run_init_process(const char *init_filename)
{
argv_init[] = init_filename;
kernel_execve(init_filename, argv_init, envp_init);
}

kernel_execv()调用init,切换到用户态。

arch/arm/kernel/sys_arm.c

int kernel_execve(const char *filename,
const char *const argv[],
const char *const envp[])
{
struct pt_regs regs;
int ret; memset(&regs, , sizeof(struct pt_regs));
ret = do_execve(filename,
(const char __user *const __user *)argv,
(const char __user *const __user *)envp, &regs);
if (ret < )
goto out; /*
* Save argc to the register structure for userspace.
*/
regs.ARM_r0 = ret; /*
* We were successful. We won't be returning to our caller, but
* instead to user space by manipulating the kernel stack.
*/
asm( "add r0, %0, %1\n\t"
"mov r1, %2\n\t"
"mov r2, %3\n\t"
"bl memmove\n\t" /* copy regs to top of stack */
"mov r8, #0\n\t" /* not a syscall */
"mov r9, %0\n\t" /* thread structure */
"mov sp, r0\n\t" /* reposition stack pointer */
"b ret_to_user"
:
: "r" (current_thread_info()),
"Ir" (THREAD_START_SP - sizeof(regs)),
"r" (&regs),
"Ir" (sizeof(regs))
: "r0", "r1", "r2", "r3", "ip", "lr", "memory"); out:
return ret;
}
EXPORT_SYMBOL(kernel_execve);

至此,以后的所有进程都有用户态init完成。

linux下1号进程的前世(kthread_init)今生(init)的更多相关文章

  1. Linux下1号进程的前世(kernel_init)今生(init进程)----Linux进程的管理与调度(六)

    前面我们了解到了0号进程是系统所有进程的先祖, 它的进程描述符init_task是内核静态创建的, 而它在进行初始化的时候, 通过kernel_thread的方式创建了两个内核线程,分别是kernel ...

  2. Linux下0号进程的前世(init_task进程)今生(idle进程)----Linux进程的管理与调度(五)【转】

    前言 Linux下有3个特殊的进程,idle进程(PID = 0), init进程(PID = 1)和kthreadd(PID = 2) idle进程由系统自动创建, 运行在内核态 idle进程其pi ...

  3. Linux下2号进程的kthreadd--Linux进程的管理与调度(七)

    2号进程 内核初始化rest_init函数中,由进程 0 (swapper 进程)创建了两个process init 进程 (pid = 1, ppid = 0) kthreadd (pid = 2, ...

  4. linux的0号进程和1号进程

    linux的 0号进程 和 1 号进程 Linux下有3个特殊的进程,idle进程(PID = 0), init进程(PID = 1)和kthreadd(PID = 2) * idle进程由系统自动创 ...

  5. kthreadd-linux下2号进程

    参考: 1. linux常见进程与内核线程 2. Linux下2号进程的kthreadd--Linux进程的管理与调度(七) 本文中代码内核版本:3.2.0 kthreadd:这种内核线程只有一个,它 ...

  6. windows和linux下关闭Tomcat进程

    windows和linux下解决Tomcat进程 windows下启动Tomcat报错,8080端口号被占用,报错信息如下 两种解决方法,一种是关闭了这个端口号,另外一种是修改Tomcat下的serv ...

  7. windows和linux下杀死Tomcat进程,解决端口占用

    windows和linux下解决Tomcat进程 windows下启动Tomcat报错,8080端口号被占用,报错信息如下 两种解决方法,一种是关闭了这个端口号,另外一种是修改Tomcat下的serv ...

  8. linux下查看当前进程以及杀死进程

    ###linux下查看当前进程以及杀死进程 查看进程 ps命令查找与进程相关的PID号: ps a :显示现行终端机下的所有程序,包括其他用户的程序. ps -A :显示所有程序. ps c :列出程 ...

  9. Linux下查看某个进程打开的文件数-losf工具常用参数介绍

    Linux下查看某个进程打开的文件数-losf工具常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在linux操作系统中,一切皆文件.通过文件不仅仅可以访问常规数据,还 ...

随机推荐

  1. mybatis-xml特殊字符处理

    1. 使用CDATA区: 它的全称为character data,以"<![CDATA[ "开始,以" ]]>" 结束,在两者之间嵌入不想被解析程序 ...

  2. hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)

    还是畅通工程                                                                            Time Limit: 4000/2 ...

  3. C#分析URL参数获取参数和值得对应列表(一)

    C#操作Url参数 http://www.cnblogs.com/RobotH/archive/2008/11/17/1335322.html 用 C# 分析 URL 中的参数信息 http://ww ...

  4. Hadoop Trash回收站使用指南

    转载:https://blog.csdn.net/sunnyyoona/article/details/78869778 我们在删除一个文件时,遇到如下问题,提示我们不能删除文件放回回收站: sudo ...

  5. Java ArrayList的模拟实现

    package test; import java.util.Arrays; import java.util.Collection; public class MyArrayList<E> ...

  6. Android API Guides---Host-based Card Emulation

    Host-based Card Emulation 很多提供NFC功能的Andr​​oid手机已经支持NFC卡模拟.在大多数情况下.该卡是由在该装置的单独芯片仿真,称为安全元件. 无线运营商提供了非常 ...

  7. System.getProperty("line.separator")

    转自:http://blog.sina.com.cn/s/blog_707577700100nv74.html 标题所写的代码能获得当前系统的换行符. 不要随便用 \n\r    \n    \r,因 ...

  8. Spring搭配Ehcache实例解析

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50538085 本文出自[我是干勾鱼的博客] 1 Ehcache简单介绍 EhCa ...

  9. 批量杀进程 ps awk grep

    ps aux |grep lt-newindexclient |grep -v grep |awk grep -v 反向输出,即过滤掉带有grep的输出. xargs:传递参数

  10. Linux下安卓ndk混合编译调用so方法——QuickStart学习

    转自:http://www.52pojie.cn/thread-313869-1-1.html #注意:.h 和.c中的错误eclipse不会检查,只会调用时在手机或虚拟机中死掉.因此需要仔细检查其中 ...