2016-05-14 80 views
0

我想寫一個基本的過程控制塊的程序,下面的代碼顯示了我在測試階段做了什麼,我想知道如果是的話,我們是否可以恢復一次孩子而不是再次分岔一個新孩子我們如何做到這一點。 在此先感謝!
代碼:
`是否可以恢復被殺死的子進程?

#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); 
     } 
} 
} 
+1

什麼?如果它能活下去的話,那麼稱它爲「被殺」? –

+0

育兒的第一條規則:不要殺死你的孩子,除非你確定你不希望他們繼續生活。 –

回答

3

當您發出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 
+0

謝謝你,我明白你想表達什麼,但這又帶來了另一種懷疑......如果孩子在中斷時突然死亡怎麼辦?例如:我正在從一個終端執行此代碼,從另一個終端使用kill cmd與子pid一起終止進程..?..我希望你能明白我的觀點。 –

+0

kill命令命名很差。它發送信號,收到信號可能會或可能不會導致進程退出。這取決於它是哪個信號。另外,你的進程可以處理一些信號,但不能像SIGSTOP那樣。 –