Linux系统编程 74 孤儿进程和僵尸进程
学习笔记
孤儿进程:父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为
init进程,称为init进程领养了孤儿进程。
init进程会去接替父进程去管理子进程。主要是进程回收!
正常情况下,是父进程对子进程进行回收。
查看demo代码
$cat orphanprocess.c
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int main(int argc, char ** argv)
{
pid_t pid;
pid = fork();
if( 0 == pid )
{
while(1)
{
printf("I am child,my parent pid = %d\n",getppid());
sleep(1);
}
}
else if( pid > 0)
{
printf("I am parent,my pid is %d\n",getpid());
sleep(9);
printf("--------------------parent going to die!-----------------\n");
}
else
{
perror("fork error");
exit(1);
}
return 0;
}
$ps -aux可以看进程信息
$ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.5 167596 11576 ? Ss 22:25 0:03 /sbin/init s
root 2 0.0 0.0 0 0 ? S 22:25 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 22:25 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 22:25 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 22:25 0:00 [kworker/0:0
root 9 0.0 0.0 0 0 ? I< 22:25 0:00 [mm_percpu_w
root 10 0.0 0.0 0 0 ? S 22:25 0:00 [rcu_tasks_r
root 11 0.0 0.0 0 0 ? S 22:25 0:00 [rcu_tasks_t
root 12 0.0 0.0 0 0 ? S 22:25 0:00 [ksoftirqd/0
root 13 0.0 0.0 0 0 ? I 22:25 0:00 [rcu_sched]
root 14 0.0 0.0 0 0 ? S 22:25 0:00 [migration/0
root 15 0.0 0.0 0 0 ? S 22:25 0:00 [idle_inject
root 16 0.0 0.0 0 0 ? S 22:25 0:00 [cpuhp/0]
root 17 0.0 0.0 0 0 ? S 22:25 0:00 [kdevtmpfs]
root 18 0.0 0.0 0 0 ? I< 22:25 0:00 [netns]
root 19 0.0 0.0 0 0 ? I< 22:25 0:00 [inet_frag_w
root 20 0.0 0.0 0 0 ? S 22:25 0:00 [kauditd]
root 21 0.0 0.0 0 0 ? S 22:25 0:00 [khungtaskd]
root 22 0.0 0.0 0 0 ? S 22:25 0:00 [oom_reaper]
root 23 0.0 0.0 0 0 ? I< 22:25 0:00 [writeback]
root 24 0.0 0.0 0 0 ? S 22:25 0:00 [kcompactd0]
root 25 0.0 0.0 0 0 ? SN 22:25 0:00 [ksmd]
root 26 0.0 0.0 0 0 ? SN 22:25 0:00 [khugepaged]
root 72 0.0 0.0 0 0 ? I< 22:25 0:00 [kintegrityd
root 73 0.0 0.0 0 0 ? I< 22:25 0:00 [kblockd]
root 74 0.0 0.0 0 0 ? I< 22:25 0:00 [blkcg_punt_
root 75 0.0 0.0 0 0 ? I< 22:25 0:00 [tpm_dev_wq]
root 76 0.0 0.0 0 0 ? I< 22:25 0:00 [ata_sff]
root 77 0.0 0.0 0 0 ? I< 22:25 0:00 [md]
root 78 0.0 0.0 0 0 ? I< 22:25 0:00 [edac-poller
root 79 0.0 0.0 0 0 ? I< 22:25 0:00 [devfreq_wq]
root 80 0.0 0.0 0 0 ? S 22:25 0:00 [watchdogd]
root 82 0.0 0.0 0 0 ? I< 22:25 0:00 [kworker/0:1
root 84 0.0 0.0 0 0 ? S 22:25 0:00 [kswapd0]
root 85 0.0 0.0 0 0 ? S 22:25 0:00 [ecryptfs-kt
root 87 0.0 0.0 0 0 ? I< 22:25 0:00 [kthrotld]
root 88 0.0 0.0 0 0 ? S 22:25 0:00 [irq/24-pcie
root 89 0.0 0.0 0 0 ? S 22:25 0:00 [irq/25-pcie
root 90 0.0 0.0 0 0 ? S 22:25 0:00 [irq/26-pcie
root 91 0.0 0.0 0 0 ? S 22:25 0:00 [irq/27-pcie
root 92 0.0 0.0 0 0 ? S 22:25 0:00 [irq/28-pcie
root 93 0.0 0.0 0 0 ? S 22:25 0:00 [irq/29-pcie
root 94 0.0 0.0 0 0 ? S 22:25 0:00 [irq/30-pcie
root 95 0.0 0.0 0 0 ? S 22:25 0:00 [irq/31-pcie
root 96 0.0 0.0 0 0 ? S 22:25 0:00 [irq/32-pcie
root 97 0.0 0.0 0 0 ? S 22:25 0:00 [irq/33-pcie
root 98 0.0 0.0 0 0 ? S 22:25 0:00 [irq/34-pcie
root 99 0.0 0.0 0 0 ? S 22:25 0:00 [irq/35-pcie
root 100 0.0 0.0 0 0 ? S 22:25 0:00 [irq/36-pcie
root 101 0.0 0.0 0 0 ? S 22:25 0:00 [irq/37-pcie
root 102 0.0 0.0 0 0 ? S 22:25 0:00 [irq/38-pcie
root 103 0.0 0.0 0 0 ? S 22:25 0:00 [irq/39-pcie
root 104 0.0 0.0 0 0 ? S 22:25 0:00 [irq/40-pcie
root 105 0.0 0.0 0 0 ? S 22:25 0:00 [irq/41-pcie
root 106 0.0 0.0 0 0 ? S 22:25 0:00 [irq/42-pcie
root 107 0.0 0.0 0 0 ? S 22:25 0:00 [irq/43-pcie
root 108 0.0 0.0 0 0 ? S 22:25 0:00 [irq/44-pcie
root 109 0.0 0.0 0 0 ? S 22:25 0:00 [irq/45-pcie
root 110 0.0 0.0 0 0 ? S 22:25 0:00 [irq/46-pcie
root 111 0.0 0.0 0 0 ? S 22:25 0:00 [irq/47-pcie
root 112 0.0 0.0 0 0 ? S 22:25 0:00 [irq/48-pcie
root 113 0.0 0.0 0 0 ? S 22:25 0:00 [irq/49-pcie
root 114 0.0 0.0 0 0 ? S 22:25 0:00 [irq/50-pcie
root 115 0.0 0.0 0 0 ? S 22:25 0:00 [irq/51-pcie
root 116 0.0 0.0 0 0 ? S 22:25 0:00 [irq/52-pcie
root 117 0.0 0.0 0 0 ? S 22:25 0:00 [irq/53-pcie
root 118 0.0 0.0 0 0 ? S 22:25 0:00 [irq/54-pcie
root 119 0.0 0.0 0 0 ? S 22:25 0:00 [irq/55-pcie
root 120 0.0 0.0 0 0 ? I< 22:25 0:00 [acpi_therma
root 121 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_0]
root 122 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_0]
root 123 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_1]
root 124 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_1]
root 126 0.0 0.0 0 0 ? I< 22:25 0:00 [vfio-irqfd-
root 128 0.0 0.0 0 0 ? I< 22:25 0:00 [ipv6_addrco
root 139 0.0 0.0 0 0 ? I< 22:25 0:00 [kstrp]
root 142 0.0 0.0 0 0 ? I< 22:25 0:00 [zswap-shrin
root 143 0.0 0.0 0 0 ? I< 22:25 0:00 [kworker/u25
root 148 0.0 0.0 0 0 ? I< 22:25 0:00 [charger_man
root 187 0.0 0.0 0 0 ? I< 22:25 0:00 [mpt_poll_0]
root 188 0.0 0.0 0 0 ? I< 22:25 0:00 [mpt/0]
root 190 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_2]
root 191 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_2]
root 192 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_3]
root 193 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_3]
root 194 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_4]
root 195 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_4]
root 196 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_5]
root 197 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_5]
root 198 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_6]
root 199 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_6]
root 200 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_7]
root 201 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_7]
root 202 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_8]
root 203 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_8]
root 204 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_9]
root 205 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_9]
root 206 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_10]
root 207 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_10
root 208 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_11]
root 209 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_11
root 210 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_12]
root 211 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_12
root 212 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_13]
root 213 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_13
root 214 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_14]
root 215 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_14
root 216 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_15]
root 217 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_15
root 218 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_16]
root 219 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_16
root 220 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_17]
root 221 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_17
root 222 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_18]
root 223 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_18
root 224 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_19]
root 225 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_19
root 226 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_20]
root 227 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_20
root 228 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_21]
root 229 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_21
root 230 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_22]
root 231 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_22
root 232 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_23]
root 233 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_23
root 234 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_24]
root 235 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_24
root 236 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_25]
root 237 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_25
root 238 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_26]
root 239 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_26
root 240 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_27]
root 241 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_27
root 242 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_28]
root 243 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_28
root 244 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_29]
root 245 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_29
root 246 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_30]
root 247 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_30
root 248 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_31]
root 249 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_31
root 273 0.0 0.0 0 0 ? I 22:25 0:00 [kworker/u25
root 274 0.0 0.0 0 0 ? I 22:25 0:00 [kworker/u25
root 277 0.0 0.0 0 0 ? S 22:25 0:00 [scsi_eh_32]
root 278 0.0 0.0 0 0 ? I< 22:25 0:00 [scsi_tmf_32
root 300 0.0 0.0 0 0 ? S 22:25 0:00 [jbd2/sda5-8
root 301 0.0 0.0 0 0 ? I< 22:25 0:00 [ext4-rsv-co
root 340 0.0 0.8 68348 16848 ? S<s 22:25 0:00 /lib/systemd
root 364 0.0 0.0 0 0 ? S< 22:25 0:00 [loop0]
root 366 0.0 0.0 0 0 ? S< 22:25 0:00 [loop1]
root 368 0.0 0.0 0 0 ? S< 22:25 0:00 [loop2]
root 370 0.0 0.0 0 0 ? S< 22:25 0:00 [loop3]
root 373 0.0 0.0 0 0 ? S< 22:25 0:00 [loop4]
root 375 0.0 0.0 0 0 ? S< 22:25 0:00 [loop5]
root 377 0.0 0.0 0 0 ? S< 22:25 0:00 [loop6]
root 379 0.0 0.0 0 0 ? S< 22:25 0:00 [loop7]
root 382 0.0 0.0 0 0 ? S< 22:25 0:00 [loop8]
root 384 0.0 0.0 0 0 ? S< 22:25 0:00 [loop9]
root 385 0.0 0.0 0 0 ? S< 22:25 0:00 [loop10]
root 386 0.0 0.0 0 0 ? S< 22:25 0:00 [loop11]
root 388 0.0 0.4 24448 8000 ? Ss 22:25 0:00 /lib/systemd
root 389 0.0 0.0 0 0 ? S< 22:25 0:00 [loop12]
root 390 0.0 0.0 0 0 ? S 22:25 0:00 [irq/16-vmwg
root 391 0.0 0.0 0 0 ? I< 22:25 0:00 [ttm_swap]
root 392 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc0
root 393 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc1
root 394 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc2
root 395 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc3
root 396 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc4
root 397 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc5
root 398 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc6
root 399 0.0 0.0 0 0 ? S 22:25 0:00 [card0-crtc7
root 406 0.0 0.0 150668 288 ? Ssl 22:25 0:00 vmware-vmblo
root 470 0.0 0.0 0 0 ? I< 22:25 0:00 [cryptd]
systemd+ 637 0.0 0.6 23904 12028 ? Ss 22:25 0:00 /lib/systemd
systemd+ 639 0.0 0.2 90260 5936 ? Ssl 22:25 0:00 /lib/systemd
root 655 0.0 0.5 58780 10636 ? Ss 22:25 0:00 /usr/bin/VGA
root 657 0.1 0.3 248080 7556 ? Ssl 22:25 0:01 /usr/bin/vmt
root 678 0.0 0.4 250552 9532 ? Ssl 22:25 0:00 /usr/lib/acc
root 681 0.0 0.0 2548 844 ? Ss 22:25 0:00 /usr/sbin/ac
avahi 685 0.0 0.1 8532 3524 ? Ss 22:25 0:00 avahi-daemon
root 686 0.0 0.1 18052 3128 ? Ss 22:25 0:00 /usr/sbin/cr
message+ 688 0.0 0.3 9888 6372 ? Ss 22:25 0:01 /usr/bin/dbu
root 689 0.0 1.0 347140 21768 ? Ssl 22:25 0:00 /usr/sbin/Ne
root 719 0.0 1.0 47960 20156 ? Ss 22:25 0:00 /usr/bin/pyt
root 739 0.1 0.6 253608 12796 ? Ssl 22:25 0:01 /usr/lib/pol
syslog 741 0.0 0.2 224356 5276 ? Ssl 22:25 0:00 /usr/sbin/rs
root 742 0.1 1.9 725676 39580 ? Ssl 22:25 0:02 /usr/lib/sna
root 743 0.0 0.3 244356 6696 ? Ssl 22:25 0:00 /usr/libexec
root 744 0.0 0.4 16776 8152 ? Ss 22:25 0:00 /lib/systemd
root 745 0.0 0.7 395676 14600 ? Ssl 22:25 0:00 /usr/lib/udi
root 746 0.0 0.2 13688 4880 ? Ss 22:25 0:00 /sbin/wpa_su
avahi 758 0.0 0.0 8352 324 ? S 22:25 0:00 avahi-daemon
root 797 0.0 0.0 2612 96 ? Ss 22:25 0:00 /usr/sbin/gp
root 803 0.0 0.6 178528 12704 ? Ssl 22:25 0:00 /usr/sbin/cu
root 822 0.0 0.4 37204 8788 ? Ss 22:25 0:00 /usr/sbin/cu
root 844 0.0 0.5 240016 10744 ? Ssl 22:25 0:00 /usr/sbin/Mo
root 856 0.0 1.1 126764 22748 ? Ssl 22:26 0:00 /usr/bin/pyt
root 884 0.0 0.4 248248 8772 ? Ssl 22:26 0:00 /usr/sbin/gd
root 916 0.0 0.8 80376 16676 ? Ss 22:26 0:00 /usr/sbin/nm
whoopsie 917 0.0 0.8 327312 16128 ? Ssl 22:26 0:00 /usr/bin/who
kernoops 919 0.0 0.0 11264 452 ? Ss 22:26 0:00 /usr/sbin/ke
kernoops 921 0.0 0.0 11264 448 ? Ss 22:26 0:00 /usr/sbin/ke
rtkit 1022 0.0 0.1 152940 3084 ? SNsl 22:26 0:00 /usr/libexec
root 1043 0.0 1.3 95856 26048 ? Ss 22:26 0:00 /usr/sbin/sm
root 1108 0.0 0.5 93708 10276 ? S 22:26 0:00 /usr/sbin/sm
root 1109 0.0 0.4 93700 7988 ? S 22:26 0:00 /usr/sbin/sm
root 1114 0.0 0.5 95864 11436 ? S 22:26 0:00 /usr/sbin/sm
root 1135 0.0 0.4 260752 9672 ? Ssl 22:26 0:00 /usr/lib/upo
root 1337 0.0 0.0 0 0 ? I 22:26 0:00 [kworker/0:4
colord 1370 0.0 0.8 257556 16844 ? Ssl 22:26 0:00 /usr/libexec
root 1407 0.0 0.5 175848 10056 ? Sl 22:28 0:00 gdm-session-
ubuntu 1420 0.0 0.5 19092 10320 ? Ss 22:28 0:00 /lib/systemd
ubuntu 1421 0.0 0.1 169128 3568 ? S 22:28 0:00 (sd-pam)
ubuntu 1427 1.4 0.9 1417308 19436 ? S<sl 22:28 0:15 /usr/bin/pul
ubuntu 1429 0.0 1.2 519760 24296 ? SNsl 22:28 0:00 /usr/libexec
ubuntu 1431 0.0 0.3 248820 7348 ? Sl 22:28 0:00 /usr/bin/gno
ubuntu 1435 0.0 0.4 11940 9144 ? Ss 22:28 0:00 /usr/bin/dbu
ubuntu 1439 0.0 0.3 248332 7884 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1444 0.0 0.4 382064 8580 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1458 0.0 0.6 326156 12244 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1467 0.0 0.3 172652 6944 tty2 Ssl+ 22:28 0:00 /usr/lib/gdm
ubuntu 1471 0.7 3.5 286856 70668 tty2 Sl+ 22:28 0:07 /usr/lib/xor
ubuntu 1473 0.0 0.3 244336 6228 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1478 0.0 0.4 325356 9076 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1485 0.0 0.3 244508 6208 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1489 0.0 1.8 555956 37164 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1496 0.0 0.5 327240 11812 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1498 0.0 0.3 246736 6876 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1520 0.0 0.7 199368 15744 tty2 Sl+ 22:28 0:00 /usr/libexec
ubuntu 1588 0.0 0.0 6040 456 ? Ss 22:28 0:00 /usr/bin/ssh
ubuntu 1607 0.0 0.4 383540 9008 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1613 0.0 0.2 7248 4368 ? S 22:28 0:00 /usr/bin/dbu
ubuntu 1617 0.0 0.2 98696 4292 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1624 0.0 0.8 495176 17488 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1638 1.5 11.6 3666124 232052 ? Ssl 22:28 0:16 /usr/bin/gno
ubuntu 1656 0.0 0.5 323340 10664 ? Sl 22:28 0:00 ibus-daemon
ubuntu 1660 0.0 0.4 249004 9508 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1661 0.2 1.6 286704 33692 ? Sl 22:28 0:02 /usr/libexec
ubuntu 1665 0.0 1.4 208996 29896 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1667 0.0 0.4 248988 9668 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1678 0.0 0.3 162908 6380 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1682 0.0 0.2 244228 4920 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1687 0.0 1.0 581692 21080 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1695 0.0 1.3 472824 26284 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1708 0.0 1.5 716372 30212 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1716 0.0 0.2 156232 5512 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1723 0.0 1.4 682076 29680 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1737 0.0 1.3 2607396 26696 ? Sl 22:28 0:00 /usr/bin/gjs
ubuntu 1743 0.0 0.5 326176 10844 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1759 0.0 0.4 322548 9344 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1760 0.0 1.5 578968 30908 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1766 0.0 0.8 383000 17580 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1767 0.0 0.4 322784 9700 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1772 0.0 1.5 356940 30612 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1775 0.0 1.6 912152 33316 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1779 0.0 1.5 357368 31060 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1782 0.0 0.5 256852 11484 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1787 0.0 0.3 465732 6252 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1791 0.0 0.3 244140 6176 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1801 0.0 0.6 477536 12924 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1807 0.0 0.5 326716 10760 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1808 0.1 2.1 301364 42100 ? Sl 22:28 0:01 /usr/bin/vmt
ubuntu 1809 0.0 3.0 721840 61536 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1812 0.0 0.5 330416 11504 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1821 0.0 0.4 396516 9544 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1824 0.0 1.5 356500 30192 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1827 0.0 0.5 326876 10916 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1832 0.0 1.5 357572 31628 ? Ssl 22:28 0:00 /usr/libexec
ubuntu 1840 0.0 0.2 231800 5748 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1861 0.0 0.7 350824 15424 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1897 0.0 0.4 175180 9276 ? Sl 22:28 0:00 /usr/libexec
ubuntu 1985 0.0 0.3 170884 6564 ? Ssl 22:29 0:00 /usr/libexec
ubuntu 1988 0.0 1.6 431400 33484 ? Sl 22:29 0:00 update-notif
root 2308 0.0 0.0 0 0 ? I 22:34 0:00 [kworker/0:0
root 2341 0.0 0.0 0 0 ? I 22:41 0:00 [kworker/u25
root 2342 0.1 0.0 0 0 ? I 22:42 0:00 [kworker/0:1
ubuntu 2450 0.0 0.0 2496 80 ? S 22:43 0:00 ./orphanproc
ubuntu 2460 0.9 2.5 823336 51556 ? Ssl 22:44 0:01 /usr/libexec
ubuntu 2468 0.0 0.2 19384 5480 pts/1 Ss 22:44 0:00 bash
root 2498 0.1 0.4 163580 8544 ? Ssl 22:46 0:00 /usr/lib/Net
ubuntu 2506 0.0 0.1 20328 3908 pts/1 R+ 22:46 0:00 ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
ps -ajx |more
ps -ajx 可以看到父进程pid
$ps -ajx |more
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 1 1 1 ? -1 Ss 0 0:03 /sbin/init s
plash
0 2 0 0 ? -1 S 0 0:00 [kthreadd]
2 3 0 0 ? -1 I< 0 0:00 [rcu_gp]
2 4 0 0 ? -1 I< 0 0:00 [rcu_par_gp]
2 6 0 0 ? -1 I< 0 0:00 [kworker/0:0
H-events_highpri]
2 9 0 0 ? -1 I< 0 0:00 [mm_percpu_w
q]
2 10 0 0 ? -1 S 0 0:00 [rcu_tasks_r
ude_]
2 11 0 0 ? -1 S 0 0:00 [rcu_tasks_t
race]
2 12 0 0 ? -1 S 0 0:00 [ksoftirqd/0
]
2 13 0 0 ? -1 I 0 0:00 [rcu_sched]
2 14 0 0 ? -1 S 0 0:00 [migration/0
]
2 15 0 0 ? -1 S 0 0:00 [idle_inject
/0]
2 16 0 0 ? -1 S 0 0:00 [cpuhp/0]
2 17 0 0 ? -1 S 0 0:00 [kdevtmpfs]
2 18 0 0 ? -1 I< 0 0:00 [netns]
2 19 0 0 ? -1 I< 0 0:00 [inet_frag_w
q]
2 20 0 0 ? -1 S 0 0:00 [kauditd]
2 21 0 0 ? -1 S 0 0:00 [khungtaskd]
在一个终端执行程序
在另一个终端查看进程id
$./orphanprocess
I am parent,my pid is 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
I am child,my parent pid = 2990
--------------------parent going to die!-----------------
$I am child,my parent pid = 1420
I am child,my parent pid = 1420
在I am child,my parent pid = 2990 时候输入
$ps -ajx |more
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
2460 2561 2561 2561 pts/2 2990 Ss 1000 0:00 bash
2561 2990 2990 2561 pts/2 2990 S+ 1000 0:00 ./orphanprocess
2990 2991 2990 2561 pts/2 2990 S+ 1000 0:00 ./orphanprocess
可以看出来
2990 就是 ./orphanprocess
2991 也是 ./orphanprocess
2460 是 bash
从祖辈到儿孙辈的关系
bash -> ./orphanprocess -> ./orphanprocess
$I am child,my parent pid = 1420 时候,再次输入
$ps -ajx |more
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 1 1 1 ? -1 Ss 0 0:03 /sbin/init splash
1 1420 1420 1420 ? -1 Ss 1000 0:00 /lib/systemd/systemd --user
1420 2991 2990 2561 pts/2 2561 S 1000 0:00 ./orphanprocess
发现
./orphanprocess -》 /lib/systemd/systemd --user -》/sbin/init splash
所以和视频讲解的有出入。
实际上,失去父进程的子进程会被/lib/systemd/systemd --use 进程收养
收养的目的是为了回收进程会回收!
终端中下面的内容会一直发送:
I am child,my parent pid = 1420
即使是ctrl + c 也无效
原因: ctrl + c 发给 shell 的, 而子进程现在在后台跑着,
实际上是收不到该命令!
所以使用kill命令
$kill -9 2991
僵尸进程
表示进程终止,父进程尚未回收,子进程残留资源PCB 存放在内核中,
变成僵尸进程。
PCB 中会记载进程死亡的原因。
特别注意,僵尸进程不能使用kill 命令清除掉的,因为kill 命令只是用户用来终止进程的。
而僵尸进程已经终止! 那么用什么办法可以清除僵尸进程呢?
查看demo程序
$cat zombieprocess.c
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
pid_t pid;
pid = fork();
if(0 == pid)
{
printf("---child, my parent is %d\n",getppid());
sleep(10);
printf("-------child die!-------\n");
}
else if( pid > 0)
{
while(1)
{
printf("I am parent, pid = %d,myson = %d\n",getpid(),pid);
sleep(1);
}
}
else
{
perror("fork");
exit(1);
}
return 0;
}
$make zombieprocess
gcc zombieprocess.c -o zombieprocess -Wall -g
$./zombieprocess
I am parent, pid = 3263,myson = 3264
---child, my parent is 3263
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
-------child die!-------
I am parent, pid = 3263,myson = 3264
I am parent, pid = 3263,myson = 3264
在另外一个终端
$ps -ajx | grep zom
2561 3263 3263 2561 pts/2 3263 S+ 1000 0:00 ./zombieprocess
3263 3264 3263 2561 pts/2 3263 Z+ 1000 0:00 [zombieprocess] <defunct>
2468 3267 3266 2468 pts/1 3266 S+ 1000 0:00 grep --color=auto zom
<defunct> 表示僵尸进程。
尝试kill 该僵尸进程:
$kill -9 3264
$ps -ajx | grep zom
2561 3263 3263 2561 pts/2 3263 S+ 1000 0:00 ./zombieprocess
3263 3264 3263 2561 pts/2 3263 Z+ 1000 0:00 [zombieprocess] <defunct>
2468 3279 3278 2468 pts/1 3278 S+ 1000 0:00 grep --color=auto zom
发现kill 无效,僵尸进程,再kill 只是鞭尸。
只能通过杀死父亲进程,init进程去领养,init进程发现是僵尸进程,就进行进程回收了。
$kill -9 3263
$ps -ajx | grep zom
2468 3292 3291 2468 pts/1 3291 S+ 1000 0:00 grep --color=auto zom
发现成功杀死僵尸进程。