子进程无法正常工作

发布于 2024-10-04 06:29:25 字数 1334 浏览 1 评论 0原文

我尝试了以下网站的示例: http://www.cs.cf .ac.uk/Dave/C/node24.html 有时,输出不会显示来自子进程的消息。 我试图理解为什么?有人可以帮忙吗?

#include <stdio.h>
#include <signal.h>

void sighup(); /* routines child will call upon sigtrap */
void sigint();
void sigquit();

main()
{ int pid;

  /* get child process */

   if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    }

   if (pid == 0)
     { /* child */
       signal(SIGHUP,sighup); /* set function calls */
       signal(SIGINT,sigint);
       signal(SIGQUIT, sigquit);
       for(;;); /* loop for ever */
     }
  else /* parent */
     {  /* pid hold id of child */
       printf("\nPARENT: sending SIGHUP\n\n");
       kill(pid,SIGHUP);
       sleep(3); /* pause for 3 secs */
       printf("\nPARENT: sending SIGINT\n\n");
       kill(pid,SIGINT);
       sleep(3); /* pause for 3 secs */
       printf("\nPARENT: sending SIGQUIT\n\n");
       kill(pid,SIGQUIT);
       sleep(3);
     }
}

void sighup()

{  signal(SIGHUP,sighup); /* reset signal */
   printf("CHILD: I have received a SIGHUP\n");
}

void sigint()

{  signal(SIGINT,sigint); /* reset signal */
   printf("CHILD: I have received a SIGINT\n");
}

void sigquit()

{ printf("My DADDY has Killed me!!!\n");
  exit(0);
}

I tried an example from this website: http://www.cs.cf.ac.uk/Dave/C/node24.html
Sometimes, the output does not show up message from child processes.
I'm trying to understand why? Can anybody help?

#include <stdio.h>
#include <signal.h>

void sighup(); /* routines child will call upon sigtrap */
void sigint();
void sigquit();

main()
{ int pid;

  /* get child process */

   if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    }

   if (pid == 0)
     { /* child */
       signal(SIGHUP,sighup); /* set function calls */
       signal(SIGINT,sigint);
       signal(SIGQUIT, sigquit);
       for(;;); /* loop for ever */
     }
  else /* parent */
     {  /* pid hold id of child */
       printf("\nPARENT: sending SIGHUP\n\n");
       kill(pid,SIGHUP);
       sleep(3); /* pause for 3 secs */
       printf("\nPARENT: sending SIGINT\n\n");
       kill(pid,SIGINT);
       sleep(3); /* pause for 3 secs */
       printf("\nPARENT: sending SIGQUIT\n\n");
       kill(pid,SIGQUIT);
       sleep(3);
     }
}

void sighup()

{  signal(SIGHUP,sighup); /* reset signal */
   printf("CHILD: I have received a SIGHUP\n");
}

void sigint()

{  signal(SIGINT,sigint); /* reset signal */
   printf("CHILD: I have received a SIGINT\n");
}

void sigquit()

{ printf("My DADDY has Killed me!!!\n");
  exit(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷清清 2024-10-11 06:29:25

因为有时父进程会在子进程运行之前被安排,有时反之亦然。如果孩子在传递信号时尚未设置处理程序,您将看不到消息。

Because sometimes the parent is scheduled before the child runs and sometimes vice-versa. If the child hasn't set the handlers when the signals are delivered you don't see your messages.

审判长 2024-10-11 06:29:25

您可以在子级设置了所有句柄后向父级发送信号

 if (pid == 0)
 { /* child */
   signal(SIGHUP,sighup); /* set function calls */
   signal(SIGINT,sigint);
   signal(SIGQUIT, sigquit);
   kill(getppid() , SOMESIGNAL);
   for(;;); /* loop for ever */
 }

,并确保父级将使用暂停()等待信号

   pause();
   printf("\nPARENT: sending SIGHUP\n\n");
   kill(pid,SIGHUP);
   sleep(3); /* pause for 3 secs */    
   printf("\nPARENT: sending SIGINT\n\n");
   kill(pid,SIGINT);
   sleep(3); /* pause for 3 secs */
   printf("\nPARENT: sending SIGQUIT\n\n");
   kill(pid,SIGQUIT);
   sleep(3);

您处理 SOMESIGNAL 的处理程序可以像这样实现

 void sig_somesignal()
 {
    return;
  }

You can send parent a signal after child has set all of his handles

 if (pid == 0)
 { /* child */
   signal(SIGHUP,sighup); /* set function calls */
   signal(SIGINT,sigint);
   signal(SIGQUIT, sigquit);
   kill(getppid() , SOMESIGNAL);
   for(;;); /* loop for ever */
 }

and make sure that the parent will wait for the signal using pause()

   pause();
   printf("\nPARENT: sending SIGHUP\n\n");
   kill(pid,SIGHUP);
   sleep(3); /* pause for 3 secs */    
   printf("\nPARENT: sending SIGINT\n\n");
   kill(pid,SIGINT);
   sleep(3); /* pause for 3 secs */
   printf("\nPARENT: sending SIGQUIT\n\n");
   kill(pid,SIGQUIT);
   sleep(3);

Your handler that handles SOMESIGNAL can implemented like this

 void sig_somesignal()
 {
    return;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文