fork出来的父子进程对信号的处理

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

 除此之外,我们还可以在父子进程中利用信号进行通信。

参考:Linux的SIGUSR1和SIGUSR2信号 - 小 楼 一 夜 听 春 雨 - 博客园

### 回答1: Linux中的fork()函数可以创建一个新的进程,这个新进程是原进程的副本,也就是说,它们有相同的代码、数据和堆栈。fork()函数会返回两次,一次是在父进程中返回子进程进程ID,另一次是在子进程中返回。父进程和子进程之间的区别在于它们的进程ID不同,以及它们的父进程ID也不同。父进程和子进程共享文件描述符、信号处理程序和文件锁等资源,但是它们各自拥有自己的地址空间和堆栈。 ### 回答2: Linux中fork()是创建进程的一个系统调用,fork()会复制一份父进程的全部资源,包括代码段、数据段、堆栈、打开的文件、进程组信息等都会被复制到子进程中。因此,原来父进程有的资源,在子进程中都会有一个副本。fork()调用成功后,会返回两次,一次是在父进程中返回子进程的PID,另一次是在子进程中返回0。 在fork()调用完成后,就会出现两个进程:父进程和子进程。父进程中的所有资源都被完全复制到了子进程中,但子进程具有独立的内存空间和进程ID,因此父进程和子进程之间的内存空间是互相独立的。在这种情况下,父进程和子进程执行的代码相同,但是子进程是一个全新的进程,具有自己的内存空间和进程上下文。 如果子进程想和父进程之间进行通信,可以使用管道或者共享内存。透过管道或共享内存,子进程可以读取父进程中的数据,或者将自己产生的数据发送给父进程。另外,还可以使用信号或者消息队列进行通信。 需要特别注意的是,fork()调用成功后,子进程中会复制父进程中的所有资源,包括打开的文件描述符等。因此,子进程需要关闭不需要的文件描述符,避免浪费系统资源。此外,子进程也需要确保在调用exec()函数之前,所有需要使用的文件描述符都已经打开了,否则在子进程中打开的文件描述符可能会覆盖父进程中已经打开的文件描述符,导致出现错误。 ### 回答3: 在Linux系统中,每个进程都有一个唯一的进程ID(PID),以及一些其他的属性和信息。除了在启动时由init进程创建的特殊进程以外,每个进程都是由另一个进程fork”出来的,即在原有进程的基础上创建一个全新的进程。 在C语言中,可以使用fork()函数来实现这个操作。每次调用fork()函数时,会创建出一个全新的进程,称为“子进程”,并且这个子进程就是由“父进程fork出来的。父进程和子进程在大部分方面都是相同的,例如二者运行相同的程序、拥有相同的内存空间和变量等等,但在某些方面也有一些差异,例如二者的进程ID不同(父进程进程ID就是调用fork()函数前的进程ID,而子进程进程ID是新分配的),以及二者对共享资源的访问方式不同(具体取决于程序的实现方式)。 通常情况下,fork()函数的返回值为0,表示子进程;或者返回一个大于0的数值,表示父进程,并且这个数值就是子进程进程ID。如果fork()函数返回一个负数,则表示创建子进程失败。 在实际编程中,可以利用fork()函数来实现一些复杂的应用,例如多进程并行计算、进程间通信等等。此外,fork()函数也是Unix/Linux系统中一种重要的机制,可以实现进程的动态创建和销毁,从而增强了系统的灵活性和可扩展性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值