init是Linux系統開發執行的第一個進程,進程ID為1,是系統所有進程的起點,主要用來執行一些開機初始化腳本和監視進程。Linux系統在完成核內引導以后就開始運行init程序,init程序需要讀取配置文件/etc/inittab。inittab是一個不可執行的文本文件,它由若干行命令所組成。在RHEL 4系統中,inittab配置文件的內容如下所示。
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
#
# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
//表示當前缺省運行級別為5,啟動系統進入圖形化界面
id:5:initdefault:
//啟動時自動執行/etc/rc.d/rc.sysinit腳本
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
//當運行級別為5時,以5為參數運行/etc/rc.d/rc腳本,init將等待其返回
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
//在啟動過程中允許按[CTRL+ALT+DELETE]重啟系統
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
//在運行級別2、3、4、5上以ttyX為參數執行/sbin/mingetty程序,打開ttyX終端用于用戶登錄,如果進程退出則再次運行mingetty程序
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
//在級別5上運行xdm程序,提供xdm圖形方式登錄界面,并在退出時重新執行
# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
inittab配置文件每行的基本格式如下。
id:runlevels:action:process
其中某些部分可以為空,下面我們逐一介紹。
1.id
1~2個字符,配置行的惟一標識,在配置文件中不能重復。
2.runlevels
配置行適用的運行級別,在這里可填入多個運行級別,比如12345或者35等。
Linux有7個運行級別。
● 0:關機。
● 1:單用戶字符界面。
● 2:不具備網絡文件系統(NFS)功能的多用戶字符界面。
● 3:具有網絡功能的多用戶字符界面。
● 4:保留不用。
● 5:具有網絡功能的圖形用戶界面。
● 6:重新啟動系統。
3.action
Init有如下幾種行為,如表1.1所示。
表1.1 init行為
行 為 |
描 述 |
respawn |
啟動并監視第4項指定的process,若process終止則重啟它 |
wait |
執行第4項指定的process,并等待它執行完備 |
once |
執行第4項指定的process |
boot |
不論在哪個執行等級,系統啟動時都會運行第4項指定的process |
bootwait |
不論在哪個執行等級,系統啟動時都會運行第4項指定的process,且一直等它執行完備 |
off |
關閉任何動作,相當于忽略該配置行 |
ondemand |
進入ondemand執行等級時,執行第4項指定的process |
initdefault |
系統啟動后進入的執行等級,該行不需要指定process |
sysinit |
不論在哪個執行等級,系統會在執行boot及bootwait之前執行第4項指定的process |
powerwait |
當系統的供電不足時執行第4項指定的process,且一直等它執行完備 |
powerokwait |
當系統的供電恢復正常時執行第4項指定的process,且一直等它執行完備 |
powerfailnow |
當系統的供電嚴重不足時執行第4項指定的process |
ctrlaltdel |
當用戶按下【Ctrl+Alt+Del】時執行的操作 |
kbrequest |
當用戶按下特殊的組合鍵時執行第4項指定的process,此組合鍵需在keymaps文件定義 |
4.process
process為init執行的進程,這些進程都保存在目錄/etc/rc.d/rcX中,其中的X代表運行級別,rc程序接收X參數,然后運行/etc/rc.d/rc.X下面的程序。使用如下命令可以查看/etc/rc.d目錄內容。
#ls -l /etc/rc.d/
總用量 112
drwxr-xr-x 2 root root 4096 3月 15 14:44 init.d
-rwxr-xr-x 1 root root 2352 2004-03-17 rc
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc0.d
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc1.d
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc2.d
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc3.d
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc4.d
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc5.d
drwxr-xr-x 2 root root 4096 3月 15 17:42 rc6.d
-rwxr-xr-x 1 root root 220 2003-06-24 rc.local
-rwxr-xr-x 1 root root 27411 2005-08-03 rc.sysinit
使用如下命令查看/etc/rc.d/rc5.d的內容。
# ls -l /etc/rc.d/rc5.d
總用量 432
lrwxrwxrwx 1 root root 21 4月 29 15:11 K01tog-pegasus -> ../init.d/tog-pegasus
lrwxrwxrwx 1 root root 24 4月 29 14:20 K02NetworkManager -> ../init.d/NetworkManager
lrwxrwxrwx 1 root root 17 4月 29 15:29 K02oddjobd -> ../init.d/oddjobd
lrwxrwxrwx 1 root root 14 4月 29 15:31 K05innd -> ../init.d/innd
lrwxrwxrwx 1 root root 19 4月 29 14:17 K05saslauthd -> ../init.d/saslauthd
lrwxrwxrwx 1 root root 19 4月 29 14:21 K10dc_server -> ../init.d/dc_server
lrwxrwxrwx 1 root root 16 4月 29 14:21 S09pcmcia -> ../init.d/pcmcia
lrwxrwxrwx 1 root root 17 4月 29 14:17 S10network -> ../init.d/network
lrwxrwxrwx 1 root root 16 4月 29 14:17 S12syslog -> ../init.d/syslog
這些文件都是符號鏈接,以S打頭的標識啟動該程序,而以K打頭的標識終止該程序,后面的數字標識執行順序,越小越先執行,剩下的標識程序名。系統啟動或者切換到該運行級別時會執行以S打頭的程序,系統切換到該運行級別時會執行以K打頭的程序。
這個目錄下的程序可通過chkconfig程序進行管理,當然這個目錄下的程序需要符合一定規范,如果了解shell編程,可以查看這些符號鏈接所指向的程序的源碼。
init也是一個進程,和普通的進程具有一樣的屬性。比如修改了/etc/inittab,想讓修改馬上生效,可通過運行“kill -SIGHUP 1”來實現,也可通過運行“init q”來實現。
華清遠見嵌入式linux視頻教程免費下載
熱點鏈接:
1、嵌入式linux驅動要學哪些
2、嵌入式Linux串口編程基礎
3、嵌入式Linux內核如何編譯
4、Linux內核模塊程序結構
5、典型嵌入式Linux系統設置
更多新聞>> |