c - 为什么子进程不会正常退出?
问题描述
为什么下面的程序不能正常工作?
int cnt = 0;
void deal(int sig) {
++cnt;
}
int main() {
signal(SIGUSR1, deal);
pid_t child = fork();
if (child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
raise(SIGSTOP);
freopen("ab.out", "w", stdout);
printf("child : %d\n", cnt);
} else {
int app_status, app_sig, fd;
struct user_regs_struct app_reg;
struct rusage app_ruse;
waitpid(child, &app_status, 0);
printf("father: waitpid: %d\n", app_status);
while (1) {
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
wait4(child, &app_status, 0, &app_ruse);
if (WIFEXITED(app_status)) {
printf("father: child exit normally, pid - %d\n", child);
break;
}
if (WIFSIGNALED(app_status) || (WIFSTOPPED(app_status)
&& WSTOPSIG(app_status) != SIGTRAP)) {
....
break;
}
ptrace(PTRACE_GETREGS, child, NULL, &app_reg);
if (app_reg.orig_rax == SYS_open && (fd = app_reg.rax) >= 0) {
kill(child, SIGUSR1);
}
}
}
return 0;
}
运行程序时,得到以下结果
为什么子进程收到信号后直接退出?
完整的代码可以在这里。
解决方案
推荐阅读
- java - 我将如何使用 printf() 和输入单词的字符串数组在星号框中打印出来
- invoice-ninja - Docker invoiceninja/invoiceninja 在哪里存储数据?
- python - 无法找出我的代码有什么问题?(使用 numpy 进行二维矩阵卷积)
- python - 为什么我的 pyinstaller exe 不能工作?(我正在使用 pygame)
- angular - 如何使用 Angular 查询和显示子集合?
- python - 结构化数组中数组的 NumPy dtypes 的适当格式
- c++ - 尽管正确分配内存,但内存访问错误
- airflow - 气流将时区显示为 UTC
- .net-core - 从 .NET Core 连接到 iSeries 时遇到问题
- c++ - 为什么使用 SFINAE 查找方法是否存在因 std::vector::begin 失败