Linux內(nèi)核學(xué)習(xí)007——進(jìn)程管理(四)
進(jìn)程家族樹(shù)
Unix系統(tǒng)的進(jìn)程之間存在一個(gè)明顯的繼承關(guān)系,所有的進(jìn)程都是PID為1的init進(jìn)程的后代柿冲。內(nèi)核在系統(tǒng)啟動(dòng)的最后階段啟動(dòng)init進(jìn)程信卡,然后init進(jìn)程讀取系統(tǒng)的初始化腳本并執(zhí)行其他的相關(guān)程序惰聂,最終完成系統(tǒng)啟動(dòng)的整個(gè)過(guò)程执桌。
在Linux系統(tǒng)中有三個(gè)特殊的進(jìn)程家厌,idle進(jìn)程(PID=0)扔仓,init進(jìn)程(PID=1)和kthreadd(PID=2)褐奥。
idle進(jìn)程由系統(tǒng)創(chuàng)建,運(yùn)行在內(nèi)核態(tài)翘簇,其PID=0撬码。idle前身是系統(tǒng)創(chuàng)建的第一個(gè)進(jìn)程,也是唯一一個(gè)沒(méi)有通過(guò)fork()產(chǎn)生的進(jìn)程版保。
init進(jìn)程由idle通過(guò)kernel_thread創(chuàng)建呜笑,在內(nèi)核空間完成初始化后夫否,加載init程序(運(yùn)行在用戶(hù)空間)。Linux中的所有運(yùn)行在用戶(hù)空間的進(jìn)程都是由init創(chuàng)建并運(yùn)行的叫胁,在系統(tǒng)啟動(dòng)完成后凰慈,init將變?yōu)槭刈o(hù)進(jìn)程監(jiān)視其他進(jìn)程并負(fù)責(zé)處理孤兒進(jìn)程(進(jìn)程還未結(jié)束其父進(jìn)程就已經(jīng)結(jié)束的進(jìn)程)。
kthread進(jìn)程由idle通過(guò)kernel_thread()創(chuàng)建驼鹅,并始終運(yùn)行在內(nèi)核空間微谓,負(fù)責(zé)所有內(nèi)核線(xiàn)程的調(diào)度和管理。kthread進(jìn)程負(fù)責(zé)管理和調(diào)度其他內(nèi)核線(xiàn)程谤民,所有的內(nèi)核線(xiàn)程都直接或間接以kthread為父進(jìn)程堰酿。
我們利用ps和pstree來(lái)觀察一下CentOS6.10系統(tǒng)上的進(jìn)程。
在終端執(zhí)行ps -e得到以下結(jié)果:
ps -e
# 輸出如下:
PID TTY TIME CMD
1 ? 00:00:01 init
2 ? 00:00:00 kthreadd
3 ? 00:00:00 migration/0
4 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 stopper/0
6 ? 00:00:00 watchdog/0
7 ? 00:00:00 migration/1
8 ? 00:00:00 stopper/1
9 ? 00:00:00 ksoftirqd/1
10 ? 00:00:00 watchdog/1
11 ? 00:00:00 migration/2
12 ? 00:00:00 stopper/2
13 ? 00:00:00 ksoftirqd/2
14 ? 00:00:00 watchdog/2
15 ? 00:00:00 migration/3
16 ? 00:00:00 stopper/3
17 ? 00:00:00 ksoftirqd/3
18 ? 00:00:00 watchdog/3
19 ? 00:00:03 events/0
20 ? 00:00:00 events/1
21 ? 00:00:00 events/2
22 ? 00:00:00 events/3
23 ? 00:00:00 events/0
24 ? 00:00:00 events/1
25 ? 00:00:00 events/2
26 ? 00:00:00 events/3
27 ? 00:00:00 events_long/0
28 ? 00:00:00 events_long/1
29 ? 00:00:00 events_long/2
30 ? 00:00:00 events_long/3
31 ? 00:00:00 events_power_ef
32 ? 00:00:00 events_power_ef
33 ? 00:00:00 events_power_ef
34 ? 00:00:00 events_power_ef
35 ? 00:00:00 cgroup
36 ? 00:00:00 khelper
37 ? 00:00:00 netns
38 ? 00:00:00 async/mgr
39 ? 00:00:00 pm
40 ? 00:00:00 sync_supers
41 ? 00:00:00 bdi-default
42 ? 00:00:00 kintegrityd/0
43 ? 00:00:00 kintegrityd/1
44 ? 00:00:00 kintegrityd/2
45 ? 00:00:00 kintegrityd/3
46 ? 00:00:00 kblockd/0
47 ? 00:00:00 kblockd/1
48 ? 00:00:00 kblockd/2
49 ? 00:00:00 kblockd/3
50 ? 00:00:00 kacpid
51 ? 00:00:00 kacpi_notify
52 ? 00:00:00 kacpi_hotplug
53 ? 00:00:00 ata_aux
54 ? 00:00:00 ata_sff/0
55 ? 00:00:00 ata_sff/1
56 ? 00:00:00 ata_sff/2
57 ? 00:00:00 ata_sff/3
58 ? 00:00:00 ksuspend_usbd
59 ? 00:00:00 khubd
60 ? 00:00:00 kseriod
61 ? 00:00:00 md/0
62 ? 00:00:00 md/1
63 ? 00:00:00 md/2
64 ? 00:00:00 md/3
65 ? 00:00:00 md_misc/0
66 ? 00:00:00 md_misc/1
67 ? 00:00:00 md_misc/2
68 ? 00:00:00 md_misc/3
69 ? 00:00:00 linkwatch
72 ? 00:00:00 khungtaskd
73 ? 00:00:00 lru-add-drain/0
74 ? 00:00:00 lru-add-drain/1
75 ? 00:00:00 lru-add-drain/2
76 ? 00:00:00 lru-add-drain/3
77 ? 00:00:00 kswapd0
78 ? 00:00:00 ksmd
79 ? 00:00:00 khugepaged
80 ? 00:00:00 aio/0
81 ? 00:00:00 aio/1
82 ? 00:00:00 aio/2
83 ? 00:00:00 aio/3
84 ? 00:00:00 crypto/0
85 ? 00:00:00 crypto/1
86 ? 00:00:00 crypto/2
87 ? 00:00:00 crypto/3
94 ? 00:00:00 kthrotld/0
95 ? 00:00:00 kthrotld/1
96 ? 00:00:00 kthrotld/2
97 ? 00:00:00 kthrotld/3
98 ? 00:00:00 pciehpd
100 ? 00:00:00 kpsmoused
101 ? 00:00:00 usbhid_resumer
102 ? 00:00:00 deferwq
135 ? 00:00:00 kdmremove
136 ? 00:00:00 kstriped
316 ? 00:00:00 scsi_eh_0
317 ? 00:00:00 scsi_eh_1
403 ? 00:00:00 mpt_poll_0
404 ? 00:00:00 mpt/0
405 ? 00:00:00 scsi_eh_2
464 ? 00:00:00 kdmflush
466 ? 00:00:00 kdmflush
532 ? 00:00:00 jbd2/dm-0-8
533 ? 00:00:00 ext4-dio-unwrit
639 ? 00:00:00 udevd
806 ? 00:00:00 vmmemctl
1241 ? 00:00:00 kdmflush
1287 ? 00:00:00 jbd2/sda1-8
1288 ? 00:00:00 ext4-dio-unwrit
1289 ? 00:00:00 jbd2/dm-2-8
1290 ? 00:00:00 ext4-dio-unwrit
1312 ? 00:00:00 flush-253:0
1343 ? 00:00:00 kauditd
1402 ? 00:00:00 ib_addr
1407 ? 00:00:00 infiniband/0
1408 ? 00:00:00 infiniband/1
1409 ? 00:00:00 infiniband/2
1410 ? 00:00:00 infiniband/3
1419 ? 00:00:00 ib_mcast
1424 ? 00:00:00 iw_cm_wq
1429 ? 00:00:00 ib_cm/0
1430 ? 00:00:00 ib_cm/1
1431 ? 00:00:00 ib_cm/2
1432 ? 00:00:00 ib_cm/3
1437 ? 00:00:00 rdma_cm
1458 ? 00:00:00 ipoib_flush
1651 ? 00:00:00 auditd
1685 ? 00:00:00 rsyslogd
1719 ? 00:00:00 irqbalance
1744 ? 00:00:00 rpcbind
1764 ? 00:00:00 dbus-daemon
1779 ? 00:00:00 NetworkManager
1786 ? 00:00:00 modem-manager
1802 ? 00:00:00 rpc.statd
1840 ? 00:00:00 cupsd
1843 ? 00:00:00 wpa_supplicant
1875 ? 00:00:00 acpid
1887 ? 00:00:00 hald
1888 ? 00:00:00 hald-runner
1930 ? 00:00:00 hald-addon-inpu
1953 ? 00:00:00 hald-addon-acpi
1962 ? 00:00:00 automount
2127 ? 00:00:00 sshd
2138 ? 00:00:00 ntpd
2225 ? 00:00:00 master
2236 ? 00:00:00 pickup
2237 ? 00:00:00 qmgr
2243 ? 00:00:06 vmtoolsd
2296 ? 00:00:00 abrtd
2330 ? 00:00:00 ksmtuned
2343 ? 00:00:00 crond
2358 ? 00:00:00 atd
2375 ? 00:00:00 libvirtd
2491 ? 00:00:00 dnsmasq
2493 ? 00:00:00 certmonger
2521 ? 00:00:00 gdm-binary
2528 tty2 00:00:00 mingetty
2530 tty3 00:00:00 mingetty
2532 tty4 00:00:00 mingetty
2534 tty5 00:00:00 mingetty
2536 tty6 00:00:00 mingetty
2544 ? 00:00:00 udevd
2545 ? 00:00:00 udevd
2589 ? 00:00:00 gdm-simple-slav
2594 tty1 00:00:09 Xorg
2648 ? 00:00:00 console-kit-dae
2718 ? 00:00:00 dbus-launch
2724 ? 00:00:00 devkit-power-da
2767 ? 00:00:00 polkitd
2776 ? 00:00:00 rtkit-daemon
2783 ? 00:00:00 gdm-session-wor
2793 ? 00:00:00 gnome-keyring-d
2803 ? 00:00:00 gnome-session
2811 ? 00:00:00 dbus-launch
2812 ? 00:00:00 dbus-daemon
2887 ? 00:00:00 gconfd-2
2891 ? 00:00:00 gnome-settings-
2901 ? 00:00:00 gvfsd
2906 ? 00:00:00 seahorse-daemon
2908 ? 00:00:00 metacity
2912 ? 00:00:07 pulseaudio
2913 ? 00:00:00 gnome-panel
2924 ? 00:00:00 gconf-helper
2926 ? 00:00:01 nautilus
2932 ? 00:00:00 bonobo-activati
2937 ? 00:00:00 wnck-applet
2940 ? 00:00:00 trashapplet
2942 ? 00:00:00 gvfs-gdu-volume
2944 ? 00:00:00 udisks-daemon
2945 ? 00:00:00 udisks-daemon
2951 ? 00:00:00 gvfsd-trash
2954 ? 00:00:00 nm-applet
2955 ? 00:00:05 vmtoolsd
2956 ? 00:00:00 restorecond
2957 ? 00:00:00 polkit-gnome-au
2960 ? 00:00:00 gvfs-afc-volume
2961 ? 00:00:00 gnome-volume-co
2967 ? 00:00:00 abrt-applet
2968 ? 00:00:00 python
2970 ? 00:00:00 im-settings-dae
2993 ? 00:00:00 gnome-power-man
3012 ? 00:00:00 bluetooth-apple
3019 ? 00:00:00 gpk-update-icon
3039 ? 00:00:00 gdu-notificatio
3058 ? 00:00:00 gnome-screensav
3131 ? 00:00:00 gvfs-gphoto2-vo
3145 ? 00:00:00 gconf-im-settin
3154 ? 00:00:00 gnote
3156 ? 00:00:00 clock-applet
3158 ? 00:00:00 gdm-user-switch
3160 ? 00:00:00 notification-ar
3183 ? 00:00:00 gvfsd-metadata
3189 ? 00:00:00 gvfsd-burn
3197 ? 00:00:02 gnome-terminal
3198 ? 00:00:00 gnome-pty-helpe
3224 ? 00:00:00 dhclient
3344 pts/1 00:00:00 bash
3775 ? 00:00:00 flush-253:2
3810 ? 00:00:00 packagekitd
3834 ? 00:00:00 anacron
4011 ? 00:00:00 sleep
4012 pts/1 00:00:00 ps
4013 pts/1 00:00:00 xsel
pstree能夠幫助我們展示進(jìn)程樹(shù)张足,更直觀地觀察進(jìn)程間的關(guān)系。
觀察用戶(hù)空間進(jìn)程樹(shù):pstree 1
pstree 1
# 輸出如下:
init-+-NetworkManager-+-dhclient
| `-{NetworkManager}
|-abrtd
|-acpid
|-anacron
|-atd
|-auditd---{auditd}
|-automount---4*[{automount}]
|-bonobo-activati---{bonobo-activat}
|-certmonger
|-clock-applet
|-console-kit-dae---63*[{console-kit-da}]
|-crond
|-cupsd
|-2*[dbus-daemon---{dbus-daemon}]
|-2*[dbus-launch]
|-devkit-power-da
|-dnsmasq
|-gconf-im-settin
|-gconfd-2
|-gdm-binary-+-gdm-simple-slav-+-Xorg---4*[{Xorg}]
| | |-gdm-session-wor-+-gnome-session-+-abrt-applet
| | | | |-bluetooth-apple
| | | | |-gdu-notificatio
| | | | |-gnome-panel
| | | | |-gnome-power-man
| | | | |-gnome-volume-co
| | | | |-gpk-update-icon
| | | | |-metacity---{metacity}
| | | | |-nautilus
| | | | |-nm-applet
| | | | |-polkit-gnome-au
| | | | |-python
| | | | |-restorecond
| | | | `-{gnome-session}
| | | `-{gdm-session-wo}
| | `-{gdm-simple-sla}
| `-{gdm-binary}
|-gdm-user-switch
|-gnome-keyring-d---2*[{gnome-keyring-}]
|-gnome-screensav
|-gnome-settings----{gnome-settings}
|-gnome-terminal-+-bash---man---sh---sh---less
| |-bash-+-pstree
| | `-xsel
| |-gnome-pty-helpe
| `-{gnome-terminal}
|-gnote
|-gvfs-afc-volume---{gvfs-afc-volum}
|-gvfs-gdu-volume
|-gvfs-gphoto2-vo
|-gvfsd
|-gvfsd-burn
|-gvfsd-metadata
|-gvfsd-trash
|-hald-+-hald-runner-+-hald-addon-acpi
| | `-hald-addon-inpu
| `-{hald}
|-im-settings-dae
|-irqbalance
|-ksmtuned---sleep
|-libvirtd---10*[{libvirtd}]
|-master-+-pickup
| `-qmgr
|-5*[mingetty]
|-modem-manager
|-notification-ar
|-ntpd
|-polkitd
|-pulseaudio-+-gconf-helper
| `-2*[{pulseaudio}]
|-rpc.statd
|-rpcbind
|-rsyslogd---3*[{rsyslogd}]
|-rtkit-daemon---2*[{rtkit-daemon}]
|-seahorse-daemon
|-sshd
|-trashapplet
|-udevd---2*[udevd]
|-udisks-daemon-+-udisks-daemon
| `-{udisks-daemon}
|-vmtoolsd---{vmtoolsd}
|-vmtoolsd
|-wnck-applet
`-wpa_supplicant
觀察內(nèi)核空間進(jìn)程樹(shù):pstree 2
pstree 2
# 輸出如下:
kthreadd-+-aio/0
|-aio/1
|-aio/2
|-aio/3
|-async/mgr
|-ata_aux
|-ata_sff/0
|-ata_sff/1
|-ata_sff/2
|-ata_sff/3
|-bdi-default
|-cgroup
|-crypto/0
|-crypto/1
|-crypto/2
|-crypto/3
|-deferwq
|-2*[events/0]
|-2*[events/1]
|-2*[events/2]
|-2*[events/3]
|-events_long/0
|-events_long/1
|-events_long/2
|-events_long/3
|-4*[events_power_ef]
|-3*[ext4-dio-unwrit]
|-flush-253:0
|-flush-253:2
|-ib_addr
|-ib_cm/0
|-ib_cm/1
|-ib_cm/2
|-ib_cm/3
|-ib_mcast
|-infiniband/0
|-infiniband/1
|-infiniband/2
|-infiniband/3
|-ipoib_flush
|-iw_cm_wq
|-jbd2/dm-0-8
|-jbd2/dm-2-8
|-jbd2/sda1-8
|-kacpi_hotplug
|-kacpi_notify
|-kacpid
|-kauditd
|-kblockd/0
|-kblockd/1
|-kblockd/2
|-kblockd/3
|-3*[kdmflush]
|-kdmremove
|-khelper
|-khubd
|-khugepaged
|-khungtaskd
|-kintegrityd/0
|-kintegrityd/1
|-kintegrityd/2
|-kintegrityd/3
|-kpsmoused
|-kseriod
|-ksmd
|-ksoftirqd/0
|-ksoftirqd/1
|-ksoftirqd/2
|-ksoftirqd/3
|-kstriped
|-ksuspend_usbd
|-kswapd0
|-kthrotld/0
|-kthrotld/1
|-kthrotld/2
|-kthrotld/3
|-linkwatch
|-lru-add-drain/0
|-lru-add-drain/1
|-lru-add-drain/2
|-lru-add-drain/3
|-md/0
|-md/1
|-md/2
|-md/3
|-md_misc/0
|-md_misc/1
|-md_misc/2
|-md_misc/3
|-migration/0
|-migration/1
|-migration/2
|-migration/3
|-mpt/0
|-mpt_poll_0
|-netns
|-pciehpd
|-pm
|-rdma_cm
|-scsi_eh_0
|-scsi_eh_1
|-scsi_eh_2
|-stopper/0
|-stopper/1
|-stopper/2
|-stopper/3
|-sync_supers
|-usbhid_resumer
|-vmmemctl
|-watchdog/0
|-watchdog/1
|-watchdog/2
`-watchdog/3
init進(jìn)程
我們可以在CentOS的/sbin目錄下發(fā)現(xiàn)init的可執(zhí)行文件坎藐,通過(guò)rpm -qf init查詢(xún)出其所屬的軟件包為upstart-0.6.5-17.el6.x86_64为牍。
接著,通過(guò)rpm info upstart查詢(xún)其相關(guān)信息岩馍,得到以下結(jié)果:
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.nju.edu.cn
* epel: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Installed Packages
Name : upstart
Arch : x86_64
Version : 0.6.5
Release : 17.el6
Size : 554 k
Repo : installed
From repo : anaconda-CentOS-201806291108.x86_64
Summary : An event-driven init system
URL : http://upstart.ubuntu.com
License : GPLv2 and LGPLv2+
Description : Upstart is an event-based replacement for the /sbin/init daemon
: which handles starting of tasks and services during boot, stopping
: them during shutdown and supervising them while the system is
: running.
待補(bǔ)充碉咆。。蛀恩。