![GNU C - 信号永远不会到达处理程序](https://linux22.com/image/866016/GNU%20C%20-%20%E4%BF%A1%E5%8F%B7%E6%B0%B8%E8%BF%9C%E4%B8%8D%E4%BC%9A%E5%88%B0%E8%BE%BE%E5%A4%84%E7%90%86%E7%A8%8B%E5%BA%8F.png)
我正在做一个大学项目,需要用 GNU C 编写并在 Ubuntu 服务器上运行。我家里的主要操作系统是 PC 上的 Ubuntu 14.04。该项目是关于多线程编程的,当我需要从子进程向父进程发送信号时,我总是卡住。
我用过斯特拉斯调查信号发送情况,发现信号没有发送,或者信号没有到达处理程序。最后,我编写了一个最简单的信号处理程序应用程序,用于测试。
以下是代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h> // DateTime kezeles
#include <fcntl.h> // Rendszerhivasok file
#include <unistd.h> // fork
#include <sys/types.h> // pid_t típus
#include <sys/wait.h> // wait, waitpid
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
void HandlerParent(int signalNumber){
printf("Parent received signal: %d\n", signalNumber);
}
int main (void)
{
signal(SIGUSR1, HandlerParent);
signal(SIGUSR2, HandlerParent);
kill(getpid(), SIGUSR1);
sleep(3);
return 0;
}
再次强调,这不是项目代码。我已将其踢出我的电脑,并且它已完成,无需到达处理程序。我已将其上传到大学的服务器,并且运行良好。让我附上 2 个堆栈跟踪:
我的电脑上的结果
user@userPC:~/fold/fold1/test$ ./ownkill
没有任何结果文本... strace:
user@userPC:~/fold/fold1/test$ strace -Ff -tt ./ownkill
20:22:46.939854 execve("./ownkill", ["./ownkill"], [/* 66 vars */]) = 0
20:22:46.940705 brk(0) = 0xe2e000
20:22:46.941014 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
20:22:46.941220 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba7b000
20:22:46.941499 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
20:22:46.941622 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
20:22:46.941921 fstat(3, {st_mode=S_IFREG|0644, st_size=145965, ...}) = 0
20:22:46.942198 mmap(NULL, 145965, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa16ba57000
20:22:46.942324 close(3) = 0
20:22:46.942397 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
20:22:46.942515 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
20:22:46.942664 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
20:22:46.942749 fstat(3, {st_mode=S_IFREG|0755, st_size=1845024, ...}) = 0
20:22:46.942818 mmap(NULL, 3953344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa16b495000
20:22:46.942883 mprotect(0x7fa16b650000, 2097152, PROT_NONE) = 0
20:22:46.942948 mmap(0x7fa16b850000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7fa16b850000
20:22:46.943025 mmap(0x7fa16b856000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa16b856000
20:22:46.943097 close(3) = 0
20:22:46.943167 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba56000
20:22:46.943234 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba54000
20:22:46.943303 arch_prctl(ARCH_SET_FS, 0x7fa16ba54740) = 0
20:22:46.943509 mprotect(0x7fa16b850000, 16384, PROT_READ) = 0
20:22:46.943582 mprotect(0x600000, 4096, PROT_READ) = 0
20:22:46.943654 mprotect(0x7fa16ba7d000, 4096, PROT_READ) = 0
20:22:46.943715 munmap(0x7fa16ba57000, 145965) = 0
20:22:46.943816 rt_sigaction(SIGUSR1, {0x40064d, [USR1], SA_RESTORER|SA_RESTART, 0x7fa16b4cbc30}, {SIG_DFL, [], 0}, 8) = 0
20:22:46.943895 rt_sigaction(SIGUSR2, {0x40064d, [USR2], SA_RESTORER|SA_RESTART, 0x7fa16b4cbc30}, {SIG_DFL, [], 0}, 8) = 0
20:22:46.943966 getpid() = 15023
20:22:46.944022 kill(15023, SIGUSR1) = 0
20:22:46.944082 rt_sigprocmask(SIG_BLOCK, [CHLD], [USR1], 8) = 0
20:22:46.944148 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
20:22:46.944208 rt_sigprocmask(SIG_SETMASK, [USR1], NULL, 8) = 0
20:22:46.944267 nanosleep({3, 0}, 0x7fff71e310f0) = 0
20:22:49.944652 exit_group(0) = ?
20:22:49.944888 +++ exited with 0 +++
服务器上的结果
USER@SERVER:~/fold/fold1> ./ownkill
Parent received signal: 10
信号工作正常……strace:
USER@SERVER:~/fold/fold1> strace -Ff -tt ./ownkill
20:13:21.027730 execve("./ownkill", ["./ownkill"], [/* 64 vars */]) = 0
20:13:21.031375 brk(0) = 0x602000
20:13:21.033631 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d50000
20:13:21.035918 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
20:13:21.038207 open("/etc/ld.so.cache", O_RDONLY) = 3
20:13:21.040400 fstat(3, {st_mode=S_IFREG|0644, st_size=200673, ...}) = 0
20:13:21.042700 mmap(NULL, 200673, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7feec2d1f000
20:13:21.044877 close(3) = 0
20:13:21.047067 open("/lib64/libc.so.6", O_RDONLY) = 3
20:13:21.049276 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\354\1\0\0\0\0\0"..., 832) = 832
20:13:21.051473 fstat(3, {st_mode=S_IFREG|0755, st_size=1661454, ...}) = 0
20:13:21.053668 mmap(NULL, 3528776, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7feec27d5000
20:13:21.055848 fadvise64(3, 0, 3528776, POSIX_FADV_WILLNEED) = 0
20:13:21.058040 mprotect(0x7feec2929000, 2097152, PROT_NONE) = 0
20:13:21.060232 mmap(0x7feec2b29000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x154000) = 0x7feec2b29000
20:13:21.062417 mmap(0x7feec2b2e000, 18504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7feec2b2e000
20:13:21.064619 close(3) = 0
20:13:21.066834 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1e000
20:13:21.069021 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1d000
20:13:21.071213 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1c000
20:13:21.073461 arch_prctl(ARCH_SET_FS, 0x7feec2d1d700) = 0
20:13:21.075774 mprotect(0x7feec2b29000, 16384, PROT_READ) = 0
20:13:21.077954 mprotect(0x600000, 4096, PROT_READ) = 0
20:13:21.080143 mprotect(0x7feec2d51000, 4096, PROT_READ) = 0
20:13:21.082318 munmap(0x7feec2d1f000, 200673) = 0
20:13:21.084716 rt_sigaction(SIGUSR1, {0x40064d, [USR1], SA_RESTORER|SA_RESTART, 0x7feec28079e0}, {SIG_DFL, [], 0}, 8) = 0
20:13:21.086917 rt_sigaction(SIGUSR2, {0x40064d, [USR2], SA_RESTORER|SA_RESTART, 0x7feec28079e0}, {SIG_DFL, [], 0}, 8) = 0
20:13:21.089118 getpid() = 16382
20:13:21.091313 kill(16382, SIGUSR1) = 0
20:13:21.092463 --- SIGUSR1 (User defined signal 1) @ 0 (0) ---
20:13:21.094575 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
20:13:21.096769 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d4f000
20:13:21.098964 write(1, "Parent received signal: 10\n", 27Parent received signal: 10) = 27
20:13:21.101292 rt_sigreturn(0x1) = 0
20:13:21.103473 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
20:13:21.105654 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
20:13:21.107841 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
20:13:21.110018 nanosleep({3, 0}, {3, 0}) = 0
20:13:24.112493 exit_group(0) = ?
附加信息
我也测试了 sigaction() 函数,但它也不起作用!:(
请帮我看看是什么原因导致的这个问题。我不知道这是否是由安装的应用程序或库引起的。我还请一些使用 Ubuntu 14.04 的朋友对其进行了测试,结果很好。
我该如何开始调查根本原因?项目太庞大了,无法在服务器上开发。所以我需要使用我的电脑而不是服务器。我使用 Geany 进行开发。
请在评论中提出您的问题或疑虑,我会尽快回答。
提前谢谢您!
答案1
你患有GDM 中的这个错误. Debian 软件包3.12.2-4 已修复。查看 Ubuntu 14.04 的 GDM 更新日志,似乎修复程序并未移植到 14.04,我也找不到 Launchpad 错误报告。因此,请执行以下操作:
- 使用其他信号(仅
SIGUSR1
受到影响),或者 - 使用其他显示管理器(例如
lightdm
),或者 - 升级 GDM(可能使用 GNOME3 PPA),或者
- 使用 TTY 工作(CtrlAltF1(或F2)F6,或
- 明确解除封锁
SIGUSR1
,或 - 提交一个错误(
ubuntu-bug gdm
),并希望修复能够被重新移植。
我建议你现在就执行 (1) 和 (6)。
无论如何,对于你使用的任何信号,请执行 (5)。