【问题标题】:whether a killed child process can be resumed?是否可以恢复被杀死的子进程?
【发布时间】:2016-05-14 11:06:37
【问题描述】:

我正在尝试为基本流程控制块编写程序,下面的代码显示了我在测试阶段所做的事情,我想知道我们是否可以恢复一个孩子一旦被杀死而不是再次分叉一个新孩子是的,我们该怎么做。 提前致谢!
代码:
`

    #include<sys/types.h> 
    #include<sys/wait.h>
    #include<unistd.h>
    #include<time.h>
    #include<stdio.h>
    #include<unistd.h>
    #include<time.h>
    #include<stdio.h>
    #include<stdlib.h>
int hell()
    {  
   int j;
   for(j=1;j<6;j++)
   printf("%d hello\n",j);` 
    }  
int hello(k)
    {    
int i
for(i=1;i<15;i++
printf("%5d hello \n", i);

    }  
void sigint()
    {  
    signal(SIGCONT,sigint); /* reset signal */
    printf("CHILD: I have received a SIGINT\n"); 
    }  
int main()
    {
 int i, status;
 pid_t childID, endID,end1id,parentid;
 time_t when;

label: if ((childID = fork()) == -1) {
    perror("fork error");
    exit(EXIT_FAILURE);
 }
else if (childID == 0) {
    time(&when);hell();
    printf("k value %d\n",hello(5));
    printf("Child process started at %s\n", ctime(&when));
    printf("child PID : %d \n",getpid());

    hello();
    sleep(10);
    //kill(childID, SIGKILL);                 

    exit(EXIT_SUCCESS);
 }
 }
else {
    time(&when);
    printf("Parent process started at %s", ctime(&when));
    printf("parent PID : %d\n",getpid());
            hell();
       parentid = getpid();
for(i = 0; i < 15; i++) {
       endID =waitpid(childID,&status,WNOHANG|WUNTRACED);     
    printf("endid: %d\n",endID);
       end1id = waitpid(parentid, &status, WNOHANG|WUNTRACED);    
       if (endID == -1) {  
          perror("waitpid error");
          exit(EXIT_FAILURE);
       }
       else if (endID == 0) {
          time(&when);
          printf("Parent waiting for child at %s", ctime(&when));
          sleep(1);
       }  
       else if (endID == childID) {

          if (WIFEXITED(status))
             printf("Child ended normally\n\n");
          else if (WIFSIGNALED(status)){
        printf("Child ended because of an uncaught signal\n");goto label;}
          else if (WIFSTOPPED(status)){
          printf("Child process has stopped\n");goto label;}
          exit(EXIT_SUCCESS);
       }  
}  
}  

【问题讨论】:

  • 什么?如果它可以继续生存,为什么称它“被杀死”?
  • 为人父母的第一条规则:在您确定不希望他们继续生活之前,不要杀死您的孩子。

标签: c process-management


【解决方案1】:

当您向子进程发出 kill 命令时,您可以使用 SIGSTOP 将其挂起而不使其死亡,然后使用 SIGCONT 将其从中断处恢复。

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

int main() {
   pid_t childId;

   if ( ( childId = fork() ) == -1 ) {
      printf( "child fork failed\n" );

   } else if ( childId == 0 ) {
      int i = 0;
      while ( 1 ) {
         printf( "infinite child loop ==> %d\n", ++i );
         sleep( 1 );
      }

   } else {
      printf( "parent: child started OK\n" );
      sleep( 5 );
      printf( "parent: stopping child - but letting it live\n" );
      kill( childId, SIGSTOP );
      sleep( 5 );
      printf( "parent: resuming child since it stopped but never really died\n" );
      kill( childId, SIGCONT );
      printf( "parent: child should be running again where it left off\n" );
      sleep( 5 );

   }
}

输出:

parent: child started OK
infinite child loop ==> 1
infinite child loop ==> 2
infinite child loop ==> 3
infinite child loop ==> 4
infinite child loop ==> 5
infinite child loop ==> 6
parent: stopping child - but letting it live
parent: resuming child since it stopped but never really died
parent: child should be running again where it left off
infinite child loop ==> 7
infinite child loop ==> 8
infinite child loop ==> 9
infinite child loop ==> 10
infinite child loop ==> 11
infinite child loop ==> 12

【讨论】:

  • 谢谢,我明白你想要表达的意思,但这又带来了另一个疑问……如果孩子突然被打断了怎么办?例如:我正在从一个终端执行此代码,而从另一个终端执行此代码,我使用带有子 pid 的 kill cmd 终止进程..?.. 我希望你明白我的意思..
  • kill 命令命名不当。它发送信号,接收信号可能会也可能不会导致进程退出。这取决于它是哪个信号。此外,您的进程可以处理一些信号,但不能处理其他信号,例如 SIGSTOP。
猜你喜欢
  • 2013-01-02
  • 1970-01-01
  • 2010-12-08
  • 2010-09-28
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多