- 论坛徽章:
- 5
|
原帖由 eezzrr 于 2009-10-16 09:40 发表
我还是 不太明白 内核是c语言写的,在没有中断的情况下,应该一句一句的执行。假设执行了2这个函数,函数执行完,
网上说它不返回,那它运行到了哪里啊?
如果 说是进程切换的原因,假设我只有两个进程 0号和 ...
1,问题是他执行不完
- 691 /* Do the rest non-__init'ed, we're now alive */
- 692 rest_init();
复制代码
进入这个函数
然后
- 427 kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
复制代码
进入kernel_init
如果进去了,就起了一个内核线程
- 891 if (!ramdisk_execute_command)
- 892 ramdisk_execute_command = "/init";
- 893
- 894 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
- 895 ramdisk_execute_command = NULL;
- 896 prepare_namespace();
- 897 }
- 898
- 899 /*
- 900 * Ok, we have completed the initial bootup, and
- 901 * we're essentially up and running. Get rid of the
- 902 * initmem segments and start the user-mode stuff..
- 903 */
- 904
- 905 init_post();
- 906 return 0;
- 907 }
复制代码
挂initcramfs/initrd
然后进入init_post
进到init_post里以后
- 839 if (execute_command) {
- 840 run_init_process(execute_command);
- 841 printk(KERN_WARNING "Failed to execute %s. Attempting "
- 842 "defaults...\n", execute_command);
- 843 }
- 844 run_init_process("/sbin/init");
- 845 run_init_process("/etc/init");
- 846 run_init_process("/bin/init");
- 847 run_init_process("/bin/sh");
- 848
- 849 panic("No init found. Try passing init= option to kernel.");
- 850 }
复制代码
执行init,我这里的init是一个脚本
- [root@localhost ~]# cd busybox-initramfs
- [root@localhost busybox-initramfs]# pwd
- /root/busybox-initramfs
- [root@localhost busybox-initramfs]# cat init
- #!/bin/busybox sh
- mount -t proc proc /proc
- # configure network
- ifconfig eth0 192.168.1.2/24
- route add default gw 192.168.1.1 eth0
- # invoke shell
- exec busybox sh
- [root@localhost busybox-initramfs]#
复制代码
通过一个系统调用,exec一个新进程
- 237 /*
- 238 * Do a system call from kernel instead of calling sys_execve so we
- 239 * end up with proper pt_regs.
- 240 */
- 241 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
- 242 {
- 243 long __res;
- 244 asm volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx"
- 245 : "=a" (__res)
- 246 : "0" (__NR_execve), "ri" (filename), "c" (argv), "d" (envp) : "memory");
- 247 return __res;
- 248 }
复制代码
他启动了一个新的进程
这个进程就是bash
当然,你也可以启动N多个进程,通过这个init可以启动N多个子进程
子又有子,子又有孙,孙又有子,孙又有孙,/proc/sys/kernel/pid_max穷溃也
[ 本帖最后由 T-bagwell 于 2009-10-16 10:30 编辑 ] |
|