linux的init进程

一个在线编辑markdown文档的编辑器,是内核启动的第一个进程,init进程有很多重要的任务,它的pit
为1,在linux shell中使用pstree命令可以看到它为其它用户进程的根.以下为某机器的pstree结果。 init─┬─AliHids───9*[{AliHids}]
├─AliYunDun───8*[{AliYunDun}]
├─AliYunDunUpdate───6*[{AliYunDunUpdate}]
├─atd
├─bluetoothd
├─console-kit-dae───64*[{console-kit-dae}]
├─cron
├─dbus-daemon
├─6*[getty]
├─gshelld───3*[{gshelld}]
├─nscd───7*[{nscd}]
├─ntpd
├─polkitd───{polkitd}
├─rsyslogd───3*[{rsyslogd}]
├─sshd─┬─sshd───sftp-server
│ └─sshd───bash───pstree
├─udevd───2*[udevd]
├─upstart-socket-
└─upstart-udev-br

android中的init进程

*android 本身就是基于linux操作系统的,所以两者的init进程是类似的.

init进程的主要工作

首先大概参考下安卓/system/core/ini.c中的目录

int main(int argc, char **argv)
{
int fd_count = 0;
struct pollfd ufds[4];
char *tmpdev;
char* debuggable;
char tmp[32];
int property_set_fd_init = 0;
int signal_fd_init = 0;
int keychord_fd_init = 0;
bool is_charger = false; if (!strcmp(basename(argv[0]), "ueventd"))
return ueventd_main(argc, argv); if (!strcmp(basename(argv[0]), "watchdogd"))
return watchdogd_main(argc, argv); /* clear the umask */
umask(0);
// 下面的代码开始建立各种用户空间的目录,如/dev、/proc、/sys等
mkdir("/dev", 0755);
mkdir("/proc", 0755);
mkdir("/sys", 0755); mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
mkdir("/dev/pts", 0755);
mkdir("/dev/socket", 0755);
mount("devpts", "/dev/pts", "devpts", 0, NULL);
mount("proc", "/proc", "proc", 0, NULL);
mount("sysfs", "/sys", "sysfs", 0, NULL); /* 检测/dev/.booting文件是否可读写和创建*/
close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); open_devnull_stdio();
klog_init();
// 初始化属性
property_init(); get_hardware_name(hardware, &revision);
// 处理内核命令行
process_kernel_cmdline();
… … is_charger = !strcmp(bootmode, "charger"); INFO("property init\n");
if (!is_charger)
property_load_boot_defaults(); INFO("reading config file\n");
// 分析/init.rc文件的内容
init_parse_config_file("/init.rc");
… …// 执行初始化文件中的动作
action_for_each_trigger("init", action_add_queue_tail);
// 在charger模式下略过mount文件系统的工作
if (!is_charger) {
action_for_each_trigger("early-fs", action_add_queue_tail);
action_for_each_trigger("fs", action_add_queue_tail);
action_for_each_trigger("post-fs", action_add_queue_tail);
action_for_each_trigger("post-fs-data", action_add_queue_tail);
} queue_builtin_action(property_service_init_action, "property_service_init");
queue_builtin_action(signal_init_action, "signal_init");
queue_builtin_action(check_startup_action, "check_startup"); if (is_charger) {
action_for_each_trigger("charger", action_add_queue_tail);
} else {
action_for_each_trigger("early-boot", action_add_queue_tail);
action_for_each_trigger("boot", action_add_queue_tail);
} /* run all property triggers based on current state of the properties */
queue_builtin_action(queue_property_triggers_action, "queue_property_triggers"); #if BOOTCHART
queue_builtin_action(bootchart_init_action, "bootchart_init");
#endif
// 进入无限循环,建立init的子进程(init是所有进程的父进程)
for(;;) {
int nr, i, timeout = -1;
// 执行命令(子进程对应的命令)
execute_one_command();
restart_processes(); if (!property_set_fd_init && get_property_set_fd() > 0) {
ufds[fd_count].fd = get_property_set_fd();
ufds[fd_count].events = POLLIN;
ufds[fd_count].revents = 0;
fd_count++;
property_set_fd_init = 1;
}
if (!signal_fd_init && get_signal_fd() > 0) {
ufds[fd_count].fd = get_signal_fd();
ufds[fd_count].events = POLLIN;
ufds[fd_count].revents = 0;
fd_count++;
signal_fd_init = 1;
}
if (!keychord_fd_init && get_keychord_fd() > 0) {
ufds[fd_count].fd = get_keychord_fd();
ufds[fd_count].events = POLLIN;
ufds[fd_count].revents = 0;
fd_count++;
keychord_fd_init = 1;
} if (process_needs_restart) {
timeout = (process_needs_restart - gettime()) * 1000;
if (timeout < 0)
timeout = 0;
} if (!action_queue_empty() || cur_action)
timeout = 0;
// bootchart是一个性能统计工具,用于搜集硬件和系统的信息,并将其写入磁盘,以便其
// 他程序使用
#if BOOTCHART
if (bootchart_count > 0) {
if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
timeout = BOOTCHART_POLLING_MS;
if (bootchart_step() < 0 || --bootchart_count == 0) {
bootchart_finish();
bootchart_count = 0;
}
}
#endif
// 等待下一个命令的提交
nr = poll(ufds, fd_count, timeout);
if (nr <= 0)
continue; for (i = 0; i < fd_count; i++) {
if (ufds[i].revents == POLLIN) {
if (ufds[i].fd == get_property_set_fd())
handle_property_set_fd();
else if (ufds[i].fd == get_keychord_fd())
handle_keychord();
else if (ufds[i].fd == get_signal_fd())
handle_signal();
}
}
} return 0;
}
  • 初始化操作

    • 22-24行的创建/dev /proc /sys 目录,具体目录结构可以参考linux目录详解
    • 26-31为挂载文件系统
    • 51行执行了解析配置文件,然后根据解析结果进行服务的启动。
  • 守护服务:84行开始进入守护服务,无限循执行init子进程。

参考链接

感谢以作者文章,排名不分先后

init进程学习的更多相关文章

  1. Linux init进程学习 转

    http://oss.org.cn/kernel-book/ch13/13.6.1.htm init进程的建立 Linux将要建立的第一个进程是init进程,建立该进程是以调用kernel_threa ...

  2. Linux进程学习(孤儿进程和守护进程)

    孤儿进程和守护进程 通过前面的学习我们了解了如何通过fork()函数和vfork()函数来创建一个进程.现在 我们继续深入来学习两个特殊的进程:孤儿进程和守护进程 一.孤儿进程 1.什么是 孤儿进程如 ...

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

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

  4. Linux进程学习 - 孤儿进程和守护进程

    孤儿进程和守护进程 通过前面的学习我们了解了如何通过fork()函数和vfork()函数来创建一个进程.现在 我们继续深入来学习两个特殊的进程:孤儿进程和守护进程 一.孤儿进程 1.什么是 孤儿进程如 ...

  5. Linux进程学习

    进程与进程管理: 清屏:system("clear"); //#include <signal.h> 进程环境与进程属性: 什么是进程:简单的说,进程就是程序的一次执行 ...

  6. Linux---从start_kernel到init进程启动

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” ini ...

  7. Linux init进程详解

    init模块 一般来说,Linux程序只能用另一个Linux程序启动.例如,登录Linux终端程序Mingetty. 但终端程序又由谁启动呢?在计算机上启动Linux时,内核装入并启动init程序. ...

  8. imx6 启动 init进程

    之前不知道imx6内核是怎么启动文件系统的init进程,查了下资料,记录于此,以后再来补充. kernel/init/main.c static noinline int init_post(void ...

  9. 僵尸进程学习 & 进程状态列表 & Linux信号学习

    参考这篇文章: http://www.mike.org.cn/articles/treatment-of-zombie-processes-under-linux/ 在Linux进程的状态中,僵尸进程 ...

随机推荐

  1. sql之left join、right join、inner join的区别(转)

    感谢:http://www.cnblogs.com/pcjim/articles/799302.html ----------------------------------------------- ...

  2. Mesh Baker的基本操作与功能演示

    原地址:http://www.narkii.com/club/thread-301789-1.html 如何降低游戏在系统中的消耗并带给用户最佳的体验是开发者一直追求的目标,在Unity里面对于模型与 ...

  3. python语法-[with来自动释放对象]

    python语法-[with来自动释放对象] http://www.cnblogs.com/itech/archive/2011/01/13/1934779.html 一 with python中的w ...

  4. CAS SiteMinder (单点登录)

    http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

  5. 两页pdf打印为一页,并且放大(打印英文pdf常用)

    多很英文书籍都是小书,若我们直接打印它的pdf会很厚,比如我要打印一本 thinking in C++,就要800+页.不如把两页打成一页.但是打成一页之后又太小了,需要放大.具体方法如下:   前提 ...

  6. phpStorm 安装配置

    首先配置php解释器,进行相关设置: 配置nodejs支持 安装plugins nodejs. 问:如何修改模板的默认注释? 答:创建一个php文件,默认有: <?php/** * Create ...

  7. wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from SOAP UI (Doc ID 1314946.1)

    wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from ...

  8. 1220. Stacks

    1220 又一神题啊  卡内存可以卡到这种地步 省得不行了 开两个[N]数组 一个来记录前驱  一个存数 记录前驱的用unsigned short类型 最大可达65535 不过可以标记一下是否比这个数 ...

  9. poj2482Stars in Your Window(线段树+离散化+扫描线)

    http://poj.org/problem?id=2482 类似于上一篇 这题转化的比较巧妙 将一个点转化为一个矩形(x,y, x+w,y+h),扫描线入值为正 出值为负 也就是一根线过去 每进入一 ...

  10. [转载]Java的内存回收机制

    转自:http://www.admin10000.com/document/1671.html 在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由J ...