假设: SIGUSR1 用户自定义信号 默认处理:进程终止 SIGUSR2 用户自定义信号 默认处理:进程终止 当一个进程调用fork时,因为子进程在开始时复制父进程的存储映像,信号捕捉函数的地址在子进程中是有意义的,所以子进程继承父进程的信号处理方式。 但是当子进程调用exec后,因为exec运行新的程序后会覆盖从父进程继承来的存储映像,那么信号捕捉函数在新程序中已无意义,所以exec会将原先设置为要捕捉的信号都更改为默认动作。
回想我们经常在terminal上执行的ctrl+c操作可以把当前正在执行的进程以及其子进程一起杀死。原因就是上面所说的。因为当前terminal上运行的进程及其子进程都是bash的子进程。
如果使用kill -n pid,则只会把某个具体的进程杀死,当然有可能导致孤儿进程和僵尸进程的出现。这种方式是针对具体的进程而言的。不像在terminal上执行的ctrl+c,实际上它也是针对具体的进程的。
ok@u20:~/test/signalTest$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 15:56 ? 00:00:04 /sbin/init splash
root 2 0 0 15:56 ? 00:00:00 [kthreadd]
root 3 2 0 15:56 ? 00:00:00 [rcu_gp]
root 4 2 0 15:56 ? 00:00:00 [rcu_par_gp]
root 5 2 0 15:56 ? 00:00:00 [netns]
root 7 2 0 15:56 ? 00:00:00 [kworker/0:0H-events_highpri]
root 10 2 0 15:56 ? 00:00:00 [mm_percpu_wq]
root 11 2 0 15:56 ? 00:00:00 [rcu_tasks_rude_]
root 12 2 0 15:56 ? 00:00:00 [rcu_tasks_trace]
root 13 2 0 15:56 ? 00:00:00 [ksoftirqd/0]
root 14 2 0 15:56 ? 00:00:58 [rcu_sched]
root 15 2 0 15:56 ? 00:00:00 [migration/0]
root 16 2 0 15:56 ? 00:00:00 [idle_inject/0]
root 17 2 0 15:56 ? 00:00:00 [cpuhp/0]
root 18 2 0 15:56 ? 00:00:00 [cpuhp/1]
root 19 2 0 15:56 ? 00:00:00 [idle_inject/1]
root 20 2 0 15:56 ? 00:00:00 [migration/1]
root 21 2 0 15:56 ? 00:00:00 [ksoftirqd/1]
root 23 2 0 15:56 ? 00:00:00 [kworker/1:0H-events_highpri]
root 24 2 0 15:56 ? 00:00:00 [cpuhp/2]
root 25 2 0 15:56 ? 00:00:00 [idle_inject/2]
root 26 2 0 15:56 ? 00:00:00 [migration/2]
root 27 2 0 15:56 ? 00:00:00 [ksoftirqd/2]
root 29 2 0 15:56 ? 00:00:00 [kworker/2:0H-events_highpri]
root 30 2 0 15:56 ? 00:00:00 [cpuhp/3]
root 31 2 0 15:56 ? 00:00:00 [idle_inject/3]
root 32 2 0 15:56 ? 00:00:00 [migration/3]
root 33 2 0 15:56 ? 00:00:00 [ksoftirqd/3]
root 35 2 0 15:56 ? 00:00:00 [kworker/3:0H-events_highpri]
root 36 2 0 15:56 ? 00:00:00 [kdevtmpfs]
root 37 2 0 15:56 ? 00:00:00 [inet_frag_wq]
root 38 2 0 15:56 ? 00:00:00 [kauditd]
root 42 2 0 15:56 ? 00:00:00 [khungtaskd]
root 43 2 0 15:56 ? 00:00:00 [oom_reaper]
root 44 2 0 15:56 ? 00:00:00 [writeback]
root 45 2 0 15:56 ? 00:00:08 [kcompactd0]
root 46 2 0 15:56 ? 00:00:00 [ksmd]
root 47 2 0 15:56 ? 00:00:00 [khugepaged]
root 94 2 0 15:56 ? 00:00:00 [kintegrityd]
root 95 2 0 15:56 ? 00:00:00 [kblockd]
root 96 2 0 15:56 ? 00:00:00 [blkcg_punt_bio]
root 98 2 0 15:56 ? 00:00:00 [tpm_dev_wq]
root 99 2 0 15:56 ? 00:00:00 [ata_sff]
root 100 2 0 15:56 ? 00:00:00 [md]
root 101 2 0 15:56 ? 00:00:00 [edac-poller]
root 102 2 0 15:56 ? 00:00:00 [devfreq_wq]
root 103 2 0 15:56 ? 00:00:00 [watchdogd]
root 105 2 0 15:56 ? 00:00:01 [kworker/2:1H-kblockd]
root 107 2 0 15:56 ? 00:00:03 [kswapd0]
root 108 2 0 15:56 ? 00:00:00 [ecryptfs-kthrea]
root 110 2 0 15:56 ? 00:00:00 [kthrotld]
root 111 2 0 15:56 ? 00:00:00 [irq/24-pciehp]
root 112 2 0 15:56 ? 00:00:00 [irq/25-pciehp]
root 113 2 0 15:56 ? 00:00:00 [irq/26-pciehp]
root 114 2 0 15:56 ? 00:00:00 [irq/27-pciehp]
root 115 2 0 15:56 ? 00:00:00 [irq/28-pciehp]
root 116 2 0 15:56 ? 00:00:00 [irq/29-pciehp]
root 117 2 0 15:56 ? 00:00:00 [irq/30-pciehp]
root 118 2 0 15:56 ? 00:00:00 [irq/31-pciehp]
root 119 2 0 15:56 ? 00:00:00 [irq/32-pciehp]
root 120 2 0 15:56 ? 00:00:00 [irq/33-pciehp]
root 121 2 0 15:56 ? 00:00:00 [irq/34-pciehp]
root 122 2 0 15:56 ? 00:00:00 [irq/35-pciehp]
root 123 2 0 15:56 ? 00:00:00 [irq/36-pciehp]
root 124 2 0 15:56 ? 00:00:00 [irq/37-pciehp]
root 125 2 0 15:56 ? 00:00:00 [irq/38-pciehp]
root 126 2 0 15:56 ? 00:00:00 [irq/39-pciehp]
root 127 2 0 15:56 ? 00:00:00 [irq/40-pciehp]
root 128 2 0 15:56 ? 00:00:00 [irq/41-pciehp]
root 129 2 0 15:56 ? 00:00:00 [irq/42-pciehp]
root 130 2 0 15:56 ? 00:00:00 [irq/43-pciehp]
root 131 2 0 15:56 ? 00:00:00 [irq/44-pciehp]
root 132 2 0 15:56 ? 00:00:00 [irq/45-pciehp]
root 133 2 0 15:56 ? 00:00:00 [irq/46-pciehp]
root 134 2 0 15:56 ? 00:00:00 [irq/47-pciehp]
root 135 2 0 15:56 ? 00:00:00 [irq/48-pciehp]
root 136 2 0 15:56 ? 00:00:00 [irq/49-pciehp]
root 137 2 0 15:56 ? 00:00:00 [irq/50-pciehp]
root 138 2 0 15:56 ? 00:00:00 [irq/51-pciehp]
root 139 2 0 15:56 ? 00:00:00 [irq/52-pciehp]
root 140 2 0 15:56 ? 00:00:00 [irq/53-pciehp]
root 141 2 0 15:56 ? 00:00:00 [irq/54-pciehp]
root 142 2 0 15:56 ? 00:00:00 [irq/55-pciehp]
root 143 2 0 15:56 ? 00:00:00 [acpi_thermal_pm]
root 145 2 0 15:56 ? 00:00:00 [scsi_eh_0]
root 146 2 0 15:56 ? 00:00:00 [scsi_tmf_0]
root 147 2 0 15:56 ? 00:00:00 [scsi_eh_1]
root 148 2 0 15:56 ? 00:00:00 [scsi_tmf_1]
root 150 2 0 15:56 ? 00:00:00 [vfio-irqfd-clea]
root 151 2 0 15:56 ? 00:00:00 [mld]
root 152 2 0 15:56 ? 00:00:00 [ipv6_addrconf]
root 162 2 0 15:56 ? 00:00:00 [kstrp]
root 165 2 0 15:56 ? 00:00:00 [zswap-shrink]
root 168 2 0 15:56 ? 00:00:00 [kworker/u257:0-hci0]
root 173 2 0 15:56 ? 00:00:00 [charger_manager]
root 195 2 0 15:56 ? 00:00:01 [kworker/0:1H-kblockd]
root 222 2 0 15:56 ? 00:00:01 [kworker/3:1H-kblockd]
root 223 2 0 15:56 ? 00:00:00 [mpt_poll_0]
root 224 2 0 15:56 ? 00:00:00 [mpt/0]
root 226 2 0 15:56 ? 00:00:00 [scsi_eh_2]
root 227 2 0 15:56 ? 00:00:00 [scsi_tmf_2]
root 228 2 0 15:56 ? 00:00:00 [scsi_eh_3]
root 229 2 0 15:56 ? 00:00:00 [scsi_tmf_3]
root 230 2 0 15:56 ? 00:00:00 [scsi_eh_4]
root 231 2 0 15:56 ? 00:00:00 [scsi_tmf_4]
root 232 2 0 15:56 ? 00:00:00 [scsi_eh_5]
root 233 2 0 15:56 ? 00:00:00 [scsi_tmf_5]
root 234 2 0 15:56 ? 00:00:00 [scsi_eh_6]
root 235 2 0 15:56 ? 00:00:00 [scsi_tmf_6]
root 236 2 0 15:56 ? 00:00:00 [scsi_eh_7]
root 237 2 0 15:56 ? 00:00:00 [scsi_tmf_7]
root 238 2 0 15:56 ? 00:00:00 [scsi_eh_8]
root 239 2 0 15:56 ? 00:00:00 [scsi_tmf_8]
root 240 2 0 15:56 ? 00:00:00 [scsi_eh_9]
root 241 2 0 15:56 ? 00:00:00 [scsi_tmf_9]
root 242 2 0 15:56 ? 00:00:00 [scsi_eh_10]
root 243 2 0 15:56 ? 00:00:00 [scsi_tmf_10]
root 244 2 0 15:56 ? 00:00:00 [scsi_eh_11]
root 245 2 0 15:56 ? 00:00:00 [scsi_tmf_11]
root 246 2 0 15:56 ? 00:00:00 [scsi_eh_12]
root 247 2 0 15:56 ? 00:00:00 [scsi_tmf_12]
root 248 2 0 15:56 ? 00:00:00 [scsi_eh_13]
root 249 2 0 15:56 ? 00:00:00 [scsi_tmf_13]
root 250 2 0 15:56 ? 00:00:00 [scsi_eh_14]
root 251 2 0 15:56 ? 00:00:00 [scsi_tmf_14]
root 252 2 0 15:56 ? 00:00:00 [scsi_eh_15]
root 253 2 0 15:56 ? 00:00:00 [scsi_tmf_15]
root 254 2 0 15:56 ? 00:00:00 [scsi_eh_16]
root 255 2 0 15:56 ? 00:00:00 [scsi_tmf_16]
root 256 2 0 15:56 ? 00:00:00 [scsi_eh_17]
root 257 2 0 15:56 ? 00:00:00 [scsi_tmf_17]
root 258 2 0 15:56 ? 00:00:00 [scsi_eh_18]
root 259 2 0 15:56 ? 00:00:00 [scsi_tmf_18]
root 260 2 0 15:56 ? 00:00:00 [scsi_eh_19]
root 261 2 0 15:56 ? 00:00:00 [scsi_tmf_19]
root 262 2 0 15:56 ? 00:00:00 [scsi_eh_20]
root 263 2 0 15:56 ? 00:00:00 [scsi_tmf_20]
root 264 2 0 15:56 ? 00:00:00 [scsi_eh_21]
root 265 2 0 15:56 ? 00:00:00 [scsi_tmf_21]
root 266 2 0 15:56 ? 00:00:00 [scsi_eh_22]
root 267 2 0 15:56 ? 00:00:00 [scsi_tmf_22]
root 268 2 0 15:56 ? 00:00:00 [scsi_eh_23]
root 269 2 0 15:56 ? 00:00:00 [scsi_tmf_23]
root 270 2 0 15:56 ? 00:00:00 [scsi_eh_24]
root 271 2 0 15:56 ? 00:00:00 [scsi_tmf_24]
root 272 2 0 15:56 ? 00:00:00 [scsi_eh_25]
root 273 2 0 15:56 ? 00:00:00 [scsi_tmf_25]
root 274 2 0 15:56 ? 00:00:00 [scsi_eh_26]
root 275 2 0 15:56 ? 00:00:00 [scsi_tmf_26]
root 276 2 0 15:56 ? 00:00:00 [scsi_eh_27]
root 277 2 0 15:56 ? 00:00:00 [scsi_tmf_27]
root 278 2 0 15:56 ? 00:00:00 [scsi_eh_28]
root 279 2 0 15:56 ? 00:00:00 [scsi_tmf_28]
root 280 2 0 15:56 ? 00:00:00 [scsi_eh_29]
root 281 2 0 15:56 ? 00:00:00 [scsi_tmf_29]
root 282 2 0 15:56 ? 00:00:00 [scsi_eh_30]
root 283 2 0 15:56 ? 00:00:00 [scsi_tmf_30]
root 284 2 0 15:56 ? 00:00:00 [scsi_eh_31]
root 285 2 0 15:56 ? 00:00:00 [scsi_tmf_31]
root 313 2 0 15:56 ? 00:00:00 [scsi_eh_32]
root 314 2 0 15:56 ? 00:00:00 [scsi_tmf_32]
root 320 2 0 15:56 ? 00:00:00 [kworker/1:1H-kblockd]
root 349 2 0 15:56 ? 00:00:01 [jbd2/sda6-8]
root 350 2 0 15:56 ? 00:00:00 [ext4-rsv-conver]
root 391 1 0 15:56 ? 00:00:04 /lib/systemd/systemd-journald
root 411 1 0 15:56 ? 00:00:01 /lib/systemd/systemd-udevd
root 414 2 0 15:56 ? 00:00:00 [ttm_swap]
root 416 2 0 15:56 ? 00:00:05 [irq/16-vmwgfx]
root 418 2 0 15:56 ? 00:00:00 [card0-crtc0]
root 419 2 0 15:56 ? 00:00:00 [card0-crtc1]
root 420 2 0 15:56 ? 00:00:00 [card0-crtc2]
root 421 2 0 15:56 ? 00:00:00 [card0-crtc3]
root 422 2 0 15:56 ? 00:00:00 [card0-crtc4]
root 423 2 0 15:56 ? 00:00:00 [card0-crtc5]
root 424 2 0 15:56 ? 00:00:00 [card0-crtc6]
root 426 2 0 15:56 ? 00:00:00 [card0-crtc7]
root 493 1 0 15:56 ? 00:00:00 vmware-vmblock-fuse /run/vmblock-
root 619 2 0 15:56 ? 00:00:00 [kworker/u257:1-hci0]
root 692 2 0 15:56 ? 00:00:00 [cryptd]
root 827 2 0 15:56 ? 00:00:00 [jbd2/sda1-8]
root 828 2 0 15:56 ? 00:00:00 [ext4-rsv-conver]
systemd+ 853 1 0 15:56 ? 00:00:01 /lib/systemd/systemd-resolved
systemd+ 854 1 0 15:56 ? 00:00:00 /lib/systemd/systemd-timesyncd
root 865 1 0 15:56 ? 00:00:00 /usr/bin/VGAuthService
root 869 1 0 15:56 ? 00:00:46 /usr/bin/vmtoolsd
root 886 1 0 15:56 ? 00:00:00 /usr/lib/accountsservice/accounts
root 887 1 0 15:56 ? 00:00:00 /usr/sbin/acpid
avahi 890 1 0 15:56 ? 00:00:00 avahi-daemon: running [u20.local]
root 891 1 0 15:56 ? 00:00:00 /usr/lib/bluetooth/bluetoothd
root 894 1 0 15:56 ? 00:00:00 /usr/sbin/cron -f
root 895 1 0 15:56 ? 00:00:00 /usr/sbin/cupsd -l
message+ 896 1 0 15:56 ? 00:00:03 /usr/bin/dbus-daemon --system --a
root 897 1 0 15:56 ? 00:00:32 /usr/sbin/NetworkManager --no-dae
root 903 1 0 15:56 ? 00:00:01 /usr/sbin/irqbalance --foreground
root 907 1 0 15:56 ? 00:00:00 /usr/bin/python3 /usr/bin/network
root 910 1 0 15:56 ? 00:00:00 /usr/lib/policykit-1/polkitd --no
syslog 917 1 0 15:56 ? 00:00:00 /usr/sbin/rsyslogd -n -iNONE
root 919 1 0 15:56 ? 00:00:05 /usr/lib/snapd/snapd
root 921 1 0 15:56 ? 00:00:00 /usr/libexec/switcheroo-control
root 926 1 0 15:56 ? 00:00:00 /lib/systemd/systemd-logind
root 935 1 0 15:56 ? 00:00:12 /usr/sbin/thermald --no-daemon --
root 938 1 0 15:56 ? 00:00:00 /usr/lib/udisks2/udisksd
root 940 1 0 15:56 ? 00:00:00 /sbin/wpa_supplicant -u -s -O /ru
avahi 945 890 0 15:56 ? 00:00:00 avahi-daemon: chroot helper
root 972 1 0 15:56 ? 00:00:00 /usr/sbin/cups-browsed
root 983 1 0 15:56 ? 00:00:00 /usr/sbin/ModemManager --filter-p
root 990 1 0 15:56 ? 00:00:09 /usr/bin/python3 /usr/bin/supervi
root 1014 1 0 15:56 ? 00:00:00 /usr/bin/python3 /usr/share/unatt
root 1025 1 0 15:56 ? 00:00:00 /usr/sbin/gdm3
root 1039 1025 0 15:56 ? 00:00:00 gdm-session-worker [pam/gdm-autol
kernoops 1088 1 0 15:56 ? 00:00:00 /usr/sbin/kerneloops --test
kernoops 1098 1 0 15:56 ? 00:00:00 /usr/sbin/kerneloops
ok 1127 1 0 15:56 ? 00:00:03 /lib/systemd/systemd --user
ok 1130 1127 0 15:56 ? 00:00:00 (sd-pam)
ok 1153 1127 1 15:56 ? 00:06:05 /usr/bin/pulseaudio --daemonize=n
ok 1156 1127 0 15:56 ? 00:00:00 /usr/libexec/tracker-miner-fs
ok 1158 1 0 15:56 ? 00:00:00 /usr/bin/gnome-keyring-daemon --d
ok 1162 1039 0 15:56 tty2 00:00:00 /usr/lib/gdm3/gdm-x-session --run
ok 1164 1162 1 15:56 tty2 00:04:31 /usr/lib/xorg/Xorg vt2 -displayfd
ok 1169 1127 0 15:56 ? 00:00:04 /usr/bin/dbus-daemon --session --
rtkit 1170 1 0 15:56 ? 00:00:00 /usr/libexec/rtkit-daemon
ok 1178 1127 0 15:56 ? 00:00:00 /usr/libexec/gvfsd
ok 1194 1127 0 15:56 ? 00:00:00 /usr/libexec/gvfsd-fuse /run/user
ok 1201 1127 0 15:56 ? 00:00:01 /usr/libexec/gvfs-udisks2-volume-
ok 1219 1127 0 15:56 ? 00:00:00 /usr/libexec/gvfs-mtp-volume-moni
root 1220 2 0 15:56 ? 00:00:00 [krfcommd]
ok 1224 1127 0 15:56 ? 00:00:00 /usr/libexec/gvfs-goa-volume-moni
ok 1228 1127 0 15:56 ? 00:00:00 /usr/libexec/goa-daemon
ok 1235 1127 0 15:56 ? 00:00:00 /usr/libexec/goa-identity-service
ok 1240 1127 0 15:56 ? 00:00:01 /usr/libexec/gvfs-afc-volume-moni
ok 1246 1127 0 15:56 ? 00:00:00 /usr/libexec/gvfs-gphoto2-volume-
root 1252 1 0 15:56 ? 00:00:00 /usr/lib/upower/upowerd
ok 1331 1162 0 15:56 tty2 00:00:00 /usr/libexec/gnome-session-binary
ok 1405 1331 0 15:56 ? 00:00:00 /usr/bin/ssh-agent /usr/bin/im-la
ok 1422 1 0 15:56 ? 00:00:19 /usr/bin/ibus-daemon --daemonize
ok 1426 1422 0 15:56 ? 00:00:00 /usr/libexec/ibus-memconf
ok 1427 1422 0 15:56 ? 00:00:01 /usr/libexec/ibus-ui-gtk3
ok 1428 1422 0 15:56 ? 00:00:07 /usr/libexec/ibus-extension-gtk3
ok 1433 1 0 15:56 ? 00:00:00 /usr/libexec/ibus-x11 --kill-daem
ok 1436 1127 0 15:56 ? 00:00:00 /usr/libexec/ibus-portal
ok 1458 1127 0 15:56 ? 00:00:00 /usr/libexec/at-spi-bus-launcher
ok 1463 1458 0 15:56 ? 00:00:00 /usr/bin/dbus-daemon --config-fil
ok 1468 1127 0 15:56 ? 00:00:00 /usr/libexec/at-spi2-registryd --
ok 1477 1127 0 15:56 ? 00:00:01 /usr/libexec/xdg-desktop-portal
ok 1481 1127 0 15:56 ? 00:00:00 /usr/libexec/xdg-document-portal
ok 1484 1127 0 15:56 ? 00:00:00 /usr/libexec/xdg-permission-store
ok 1494 1127 0 15:56 ? 00:00:01 /usr/libexec/xdg-desktop-portal-g
ok 1502 1127 0 15:56 ? 00:00:00 /usr/libexec/dconf-service
ok 1508 1127 0 15:56 ? 00:00:00 /usr/bin/gnome-keyring-daemon --s
ok 1516 1422 0 15:56 ? 00:00:00 /usr/libexec/ibus-engine-simple
ok 1525 1127 0 15:56 ? 00:00:00 /usr/libexec/gnome-session-ctl --
ok 1531 1127 0 15:56 ? 00:00:00 /usr/libexec/gnome-session-binary
ok 1546 1127 1 15:56 ? 00:06:54 /usr/bin/gnome-shell
ok 1570 1127 0 15:56 ? 00:00:00 /usr/libexec/gnome-shell-calendar
ok 1576 1127 0 15:56 ? 00:00:00 /usr/libexec/evolution-source-reg
ok 1585 1127 0 15:56 ? 00:00:00 /usr/libexec/evolution-calendar-f
ok 1599 1127 0 15:56 ? 00:00:00 /usr/libexec/evolution-addressboo
ok 1616 1127 0 15:56 ? 00:00:00 /usr/bin/gjs /usr/share/gnome-she
ok 1619 1178 0 15:56 ? 00:00:00 /usr/libexec/gvfsd-trash --spawne
ok 1640 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-a11y-settings
ok 1641 1127 0 15:56 ? 00:00:01 /usr/libexec/gsd-color
ok 1644 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-datetime
ok 1646 1127 0 15:56 ? 00:00:01 /usr/libexec/gsd-housekeeping
ok 1647 1127 0 15:56 ? 00:00:01 /usr/libexec/gsd-keyboard
ok 1650 1127 0 15:56 ? 00:00:01 /usr/libexec/gsd-media-keys
ok 1651 1127 0 15:56 ? 00:00:01 /usr/libexec/gsd-power
ok 1652 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-print-notificati
ok 1654 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-rfkill
ok 1656 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-screensaver-prox
ok 1657 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-sharing
ok 1658 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-smartcard
ok 1665 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-sound
ok 1671 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-usb-protection
ok 1674 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-wacom
ok 1677 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-wwan
ok 1678 1127 0 15:56 ? 00:00:01 /usr/libexec/gsd-xsettings
ok 1711 1127 0 15:56 ? 00:00:46 /usr/bin/vmtoolsd -n vmusr --bloc
ok 1719 1531 0 15:56 ? 00:00:00 /usr/libexec/gsd-disk-utility-not
ok 1722 1531 0 15:56 ? 00:00:01 /usr/libexec/evolution-data-serve
colord 1764 1 0 15:56 ? 00:00:00 /usr/libexec/colord
ok 1785 1127 0 15:56 ? 00:00:00 /usr/libexec/gsd-printer
ok 1826 1422 0 15:56 ? 00:00:05 /usr/lib/ibus/ibus-engine-libpiny
ok 1871 1127 0 15:56 ? 00:00:01 /opt/sogoupinyin/files/bin/sogoup
ok 1970 1127 0 15:56 ? 00:00:35 /usr/libexec/gnome-terminal-serve
ok 2054 1127 8 15:57 ? 00:37:09 /usr/lib/firefox/firefox -new-win
ok 2144 2054 0 15:57 ? 00:00:00 /usr/lib/firefox/firefox -content
ok 2190 2054 0 15:57 ? 00:00:02 /usr/lib/firefox/firefox -content
ok 2232 2054 0 15:57 ? 00:00:01 /usr/lib/firefox/firefox -content
ok 2289 2054 0 15:57 ? 00:00:33 /usr/lib/firefox/firefox -content
ok 2292 2054 0 15:57 ? 00:00:06 /usr/lib/firefox/firefox -content
ok 2326 2054 0 15:57 ? 00:00:05 /usr/lib/firefox/firefox -content
ok 2454 2054 0 15:57 ? 00:00:01 /usr/lib/firefox/firefox -content
ok 2498 2054 0 15:57 ? 00:00:29 /usr/lib/firefox/firefox -content
ok 2657 1531 0 15:57 ? 00:00:01 update-notifier
ok 2660 1127 0 15:57 ? 00:00:00 /usr/libexec/gvfsd-metadata
ok 3416 1127 0 15:58 ? 00:00:01 /opt/sogoupinyin/files/bin/sogoup
ok 3469 1127 0 15:59 ? 00:00:04 /usr/lib/speech-dispatcher-module
ok 3473 1127 0 15:59 ? 00:00:04 /usr/lib/speech-dispatcher-module
ok 3479 1127 0 15:59 ? 00:00:00 /usr/bin/speech-dispatcher --spaw
ok 3483 2054 0 15:59 ? 00:00:36 /usr/lib/firefox/firefox -content
ok 3593 990 0 16:02 ? 00:00:00 bash test.sh
ok 3596 3593 0 16:02 ? 00:00:01 ./a.out
root 3641 1127 0 16:03 ? 00:00:00 dbus-launch --autolaunch=d5d6bd10
root 3642 1127 0 16:03 ? 00:00:00 /usr/bin/dbus-daemon --syslog-onl
root 3646 1127 0 16:03 ? 00:00:00 /usr/libexec/xdg-desktop-portal
root 3651 1127 0 16:03 ? 00:00:00 /usr/libexec/xdg-document-portal
root 3655 1127 0 16:03 ? 00:00:00 /usr/libexec/xdg-permission-store
root 3665 1127 0 16:03 ? 00:00:00 /usr/libexec/xdg-desktop-portal-g
root 3669 1127 0 16:03 ? 00:00:00 /usr/libexec/gvfsd
root 3674 1127 0 16:03 ? 00:00:00 /usr/libexec/gvfsd-fuse /root/.ca
root 3685 1127 0 16:03 ? 00:00:00 /usr/libexec/dconf-service
root 3691 1127 0 16:03 ? 00:00:00 /usr/bin/gnome-keyring-daemon --s
ok 4118 1127 0 16:57 ? 00:00:11 /usr/bin/gedit --gapplication-ser
ok 4964 1970 0 21:07 pts/2 00:00:00 bash
ok 5088 1127 0 21:10 ? 00:00:01 /usr/bin/gnome-calendar --gapplic
ok 5199 2054 0 21:11 ? 00:00:01 /usr/lib/firefox/firefox -content
ok 5219 2054 0 21:11 ? 00:00:00 /usr/lib/firefox/firefox -content
ok 5255 2054 0 21:11 ? 00:00:00 /usr/lib/firefox/firefox -content
ok 5296 2054 0 21:12 ? 00:00:00 /usr/lib/firefox/firefox -content
ok 7011 1178 0 21:17 ? 00:00:00 /usr/libexec/gvfsd-network --spaw
ok 7101 1178 0 21:18 ? 00:00:00 /usr/libexec/gvfsd-dnssd --spawne
root 7237 2 0 21:40 ? 00:00:04 [kworker/2:2-events]
ok 7273 1970 0 21:41 pts/1 00:00:00 bash
root 7279 2 0 21:41 ? 00:00:01 [kworker/1:1-rcu_par_gp]
ok 7407 1127 0 21:44 ? 00:00:00 /usr/bin/seahorse --gapplication-
root 7896 2 0 22:05 ? 00:00:00 [kworker/0:0-rcu_par_gp]
ok 7922 1970 0 22:05 pts/0 00:00:00 bash
root 8065 2 0 22:09 ? 00:00:00 [kworker/1:0-rcu_par_gp]
root 8114 2 0 22:10 ? 00:00:00 [kworker/0:2-cgroup_destroy]
root 8155 2 0 22:11 ? 00:00:00 [kworker/3:1-rcu_gp]
root 8338 2 0 22:33 ? 00:00:00 [kworker/2:1-cgroup_destroy]
root 8340 2 0 22:34 ? 00:00:00 [kworker/3:2-rcu_par_gp]
root 8345 2 0 22:35 ? 00:00:00 [kworker/u256:0-events_unbound]
root 8356 2 0 22:41 ? 00:00:00 [kworker/u256:2-ext4-rsv-conversi
root 8364 2 0 22:46 ? 00:00:00 [kworker/u256:1-events_unbound]
root 8375 2 0 22:52 ? 00:00:00 [kworker/1:2-events]
root 8376 2 0 22:52 ? 00:00:00 [kworker/0:1-events]
ok 8399 7273 0 22:52 pts/1 00:00:00 ./sig0
root 8411 2 0 22:52 ? 00:00:00 [kworker/3:0-mm_percpu_wq]
root 8414 2 0 22:52 ? 00:00:00 [kworker/2:0-events]
ok 8418 7922 0 22:53 pts/0 00:00:00 ps -ef
ok@u20:~/test/signalTest$
我们看一下sig0的pid为8399,ppid为7273;
7273对应的是bash,其ppid为1970;
1970对应的是/usr/libexec/gnome-terminal-serve,其ppid为1127;
1127对应的是/lib/systemd/systemd --user,其ppid为1;

通过示例,我们可以发现,在terminal上按下ctrl+c时(相当于sigNum=2),父子进程都能捕捉到该信号;但使用kill -2 pid时则只有一个进程能捕捉到。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
void handel(int num)
{
if(num == 2)
{
printf("%d catch the 2 signal\n",getpid());
}
else if(num == 3)
{
printf("%d catch the 3 signal\n",getpid());
}
else if(num == 4)
{
printf("%d catch the 4 signal\n",getpid());
}
}
int main()
{
//捕捉2,3,4号信号,并且执行相对应的函数
signal(2,handel);
signal(3,handel);
signal(4,handel);
//创建一个子进程
pid_t id = fork();
if(id<0)
{
perror("fork failed");
return -1;
}
else if(id==0)
{
while(1)
{
printf("i am child and pid = %d\n",getpid());
sleep(2);
}
}
else
{
while(1)
{
printf("i am parent and pid = %d\n",getpid());
sleep(1);
}
}
//退出程序并且刷新缓冲区
exit(0);
}

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void handler(int signo)
{
switch(signo) {
case SIGUSR1: //处理信号 SIGUSR1
printf("Parent : catch SIGUSR1\n");
break;
case SIGUSR2: //处理信号 SIGUSR2
printf("Child : catch SIGUSR2\n");
break;
default: //本例不支持
printf("Should not be here\n");
break;
}
}
int main(void)
{
pid_t ppid, cpid;
//为两个信号设置信号处理函数
if(signal(SIGUSR1, handler) == SIG_ERR)
{ //设置出错
perror("Can't set handler for SIGUSR1\n");
exit(1);
}
if(signal(SIGUSR2, handler) == SIG_ERR)
{ //设置出错
perror("Can't set handler for SIGUSR2\n");
exit(1);
}
ppid = getpid();//得到父进程ID
if((cpid = fork()) < 0)
{
perror("fail to fork\n");
exit(1);
}
else if(cpid == 0)
{
// 子进程内向父进程发送信号SIGUSER1
if(kill(ppid, SIGUSR1) == -1)
{
perror("fail to send signal\n");
exit(1);
}
while(1);//死循环,等待父进程的信号
}
else
{
sleep(1);//休眠,保证子进程先运行,并且发送SIGUSR1信号
// 父进程向自己发送SIGUSER2信号
if(kill(cpid, SIGUSR2) == -1)
{
perror("fail to send signal\n");
exit(1);
}
// 必须sleep一下,否则子进程捕获不到SIGUSER2信号
sleep(1);
printf("will kill child\n");//输出提示
if(kill(cpid, SIGKILL) == -1)
{ //发送SIGKILL信号,杀死子进程
perror("fail to send signal\n");
exit(1);
}
if(wait(NULL) ==-1)
{ //回收子进程状态,避免僵尸进程
perror("fail to wait\n");
exit(1);
}
printf("child has been killed.\n");
}
return;
}
除此之外,我们还可以在父子进程中利用信号进行通信。
进程与信号处理的理解及示例

本文介绍了进程的fork和exec操作对信号处理的影响,以及终端上Ctrl+C与kill命令的区别。在fork过程中,子进程继承了父进程的信号处理方式,但在exec后,新程序会恢复默认的信号处理。终端上的Ctrl+C可以同时结束进程及其子进程,而kill命令仅针对指定进程。文中还给出了示例代码,展示了如何捕获和处理SIGUSR1和SIGUSR2信号,以及利用信号进行进程间通信。
604

被折叠的 条评论
为什么被折叠?



