【发布时间】:2021-05-04 17:27:27
【问题描述】:
我需要有关以下代码的帮助。 我希望子进程显示一个
世界你好!
当从终端执行kill -USR1 pid时。
你能帮我解决这个问题吗?
提前谢谢你。
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
static int count = 0;
void hello_signal (int sig_num) {
if (sig_num == SIGUSR1)
{
printf ("signal received successfully \ n");
printf ("Hello, world! \ n");
count = 1;
}
}
int main () {
signal (SIGUSR1, hello_signal);
pid_t pid = fork ();
if (pid == 0) {
printf ("The child has been created");
pause();
}
else if (pid <0) {
printf ("cannot create the child");
}
else {
// kill (pid, SIGUSR1);
kill (pid, SIGUSR1);
printf ("signal sent \ n");
}
return 0;
}
【问题讨论】: