4.1【Linux】启动第一个程序

       在Windows中由不同的盘用于存放用户数据,在Linux下也有类似的概念。同Windows的系统盘C盘一样,Linux也有用于存放内核映像文件、内核启动后的第一个程序(init())、Shell程序、以及应用程序所依赖的库等的地方,这些加起来成为根文件系统,它们存放在一个分区里,内核运行后会去挂接这个分区。Linux没有分区的概念,而是以树状结构的方式管理所有目录。

内核如何启动一个应用程序呢?内核启动后的最后一步会去启动init()进程。在init_post()里会首先打开一个设备open(“dev/console”),sys_dup(0),sys_dup(0),这是一个初始化串口的操作,这样我们可以通过控制台看到内核输出,然后通过run_init_process()启动第一个应用程序。启动的程序由用户设置init = xxx,如果没有指定,则默认依次执行系统指定的程序,如果都没有则报错

一、分析init_post

    从内核启动流程可知, start_kernel最后会启动init_port来执行应用程序的代码(linux-2.6.22.6\init)

static int noinline init_post(void)

{
    ...省略...

    if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)

        printk(KERN_WARNING "Warning: unable to open an initial console.\n");

    (void) sys_dup(0);

    (void) sys_dup(0);

    if (ramdisk_execute_command) {

        run_init_process(ramdisk_execute_command);

        printk(KERN_WARNING "Failed to execute %s\n",

                ramdisk_execute_command);

    }

    ...省略...

}

 

        问题一:内核怎么启动第一个应用程序

            第一步-->open("/dev/console")

                 第二步--->(void) sys_dup(0);

                                  (void) sys_dup(0);

static int noinline init_post(void)

{

    ...省略...

/*

     * 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.");

}

        如果execute_command被定义,则代码执行if下的语句

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("/sbin/init");不成功就进入run_init_process("/etc/init");,run_init_process("/etc/init");不成功就进入下一个,以此类推。

        如果都失败将执行

panic("No init found.  Try passing init= option to kernel.");

 

    问:execute_command在哪被定义?

        

static int __init init_setup(char *str)

{

    unsigned int i;

    execute_command = str;

    /*

     * In case LILO is going to boot us with default command line,

     * it prepends "auto" before the whole cmdline which makes

     * the shell think it should execute a script with such name.

     * So we ignore all arguments entered _before_ init=... [MJ]

     */

    for (i = 1; i < MAX_INIT_ARGS; i++)

        argv_init[i] = NULL;

    return 1;

}

__setup("init=", init_setup);

        __setup("init=", init_setup);--->如果uboot传入参数init=xxx(某些东西),则execute_command = xxx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林零七

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

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

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

打赏作者

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

抵扣说明:

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

余额充值