Linux内核学习008——进程管理(四)

Linux内核学习007——进程管理(四)

进程家族树

Unix系统的进程之间存在一个明显的继承关系,所有的进程都是PID为1的init进程的后代。内核在系统启动的最后阶段启动init进程,然后init进程读取系统的初始化脚本并执行其他的相关程序,最终完成系统启动的整个过程。

在Linux系统中有三个特殊的进程,idle进程(PID=0),init进程(PID=1)和kthreadd(PID=2)。

idle进程由系统创建,运行在内核态,其PID=0。idle前身是系统创建的第一个进程,也是唯一一个没有通过fork()产生的进程。

init进程由idle通过kernel_thread创建,在内核空间完成初始化后,加载init程序(运行在用户空间)。Linux中的所有运行在用户空间的进程都是由init创建并运行的,在系统启动完成后,init将变为守护进程监视其他进程并负责处理孤儿进程(进程还未结束其父进程就已经结束的进程)。

kthread进程由idle通过kernel_thread()创建,并始终运行在内核空间,负责所有内核线程的调度和管理。kthread进程负责管理和调度其他内核线程,所有的内核线程都直接或间接以kthread为父进程。

我们利用ps和pstree来观察一下CentOS6.10系统上的进程。

在终端执行ps -e得到以下结果:

  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能够帮助我们展示进程树,更直观地观察进程间的关系。

观察用户空间进程树: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

观察内核空间进程树: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进程

我们可以在CentOS的/sbin目录下发现init的可执行文件,通过rpm -qf init查询出其所属的软件包为upstart-0.6.5-17.el6.x86_64。

接着,通过rpm info upstart查询其相关信息,得到以下结果:

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.

待补充。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值