【发布时间】:2021-01-21 02:56:26
【问题描述】:
任务是继续执行我在收到 SIGALRM 信号时停止的子进程。 到目前为止,我做了以下,这似乎不起作用:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void handler(int sig)
{
printf("hello from the handler\n");
kill(getpid(),SIGCONT);
printf("child is continuing executing");
}
int main()
{
int pid1=fork();
signal(SIGALRM,handler);
if (pid1==0) {
kill(getpid(),SIGTSTP);
printf(" I am in the child\n");
} else {
printf("i am in the parent \n");
kill(pid1,SIGALRM);
}
}
我尝试了许多代码变体,但从未执行过printf("I am in the child");。
【问题讨论】:
-
OT:你应该缩进你的代码
-
而且你不能从信号处理程序中安全地调用
printf()。 -
你混淆了
pids。当您的父进程执行并调用kill(pid1, SIGALRM)时,将调用信号处理程序并将SIGCONT发送给调用它的进程,即父进程。您没有发布确切的输出,但我猜您确实看到了父级和信号处理程序的打印。我说的对吗? -
子进程在停止状态时无法运行 SIGALRM 处理程序。
-
SpiderPig1297 是的,我也想在输出“我在孩子”中,所以我怎样才能让 kill(pid1,SIGALRM) 向孩子发送信号
标签: c linux multithreading signals