接收到 SIGUSR1 信号后执行函数形式的子进程
Execution of a function form child process after receiving a SIGUSR1 signal
我需要有关以下代码的帮助。
我希望子进程显示
Hello world !
从终端执行 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;
}
您的代码已经存在。请提供更多信息,以防以下详细信息无济于事。
Parent 创建的进程在您 运行 您的代码后立即终止。使 child 成为孤儿。
在那种情况下,'init' 进程成为 child 进程的 parent,由于使用 'pause()'
,该进程继续存在
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");
}
所以,删除行
kill(pid, SIGUSR1);
来自 parent 部分。现在,如果您从终端
传递以下命令
kill -10 pid(你的 child pid)
因为 SIGUSR1 的值为 10。您的代码已经从已注册的 'hello_signal()' 方法中打印出 'Hello World'。
我需要有关以下代码的帮助。 我希望子进程显示
Hello world !
从终端执行 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;
}
您的代码已经存在。请提供更多信息,以防以下详细信息无济于事。
Parent 创建的进程在您 运行 您的代码后立即终止。使 child 成为孤儿。 在那种情况下,'init' 进程成为 child 进程的 parent,由于使用 'pause()'
,该进程继续存在 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");
}
所以,删除行
kill(pid, SIGUSR1);
来自 parent 部分。现在,如果您从终端
传递以下命令kill -10 pid(你的 child pid)
因为 SIGUSR1 的值为 10。您的代码已经从已注册的 'hello_signal()' 方法中打印出 'Hello World'。