簡體   English   中英

收到 SIGUSR1 信號后執行 function 窗體子進程

[英]Execution of a function form child process after receiving a SIGUSR1 signal

我需要有關以下代碼的幫助。 我希望子進程顯示一個

你好世界 !

當從終端執行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;
}

您的代碼已經存在。 如果以下詳細信息沒有幫助,請提供更多信息。

一旦您運行代碼,創建的父進程就會終止。 讓孩子成為孤兒。 在這種情況下,“init”進程成為子進程的父進程,由於使用“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); 

從父部分。 現在,如果您從終端傳遞以下命令

kill -10 pid(你的孩子 pid)

由於 SIGUSR1 的值為 10。您的代碼已經從已注冊的“hello_signal()”方法中打印出“Hello World”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM