2016-04-29 111 views
1

我想知道是否有辦法暫停進程,但繼續程序。如何暫停進程並繼續程序?

我需要這樣的創建進程:
enter image description here

但是,當我打P4它結束,模具,然後P2創建P5。然後P2死亡和P1創建P3,同樣的事情發生在那裏。

我需要在進程開始死亡之前創建整個'樹'。
不能使用waitwaitpid(),因爲它只是針對它的兒子。

有沒有一種方法來暫停P4,並繼續從它的父親嗎?
如果我不能如何實現我的目標?

我目前的代碼:它創建順序如下:

P1 - > P2 - > P4 - > P4模頭 - > P5 - > P5模頭 - > P2模頭 - > P3 - > P6 - > P6模頭 - > P7 - > P7模頭 - > P3模頭 - > P1 DIES

#include <stdio.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <sys/wait.h> 
#include <time.h> 


int main(){ 
    clock_t t; 
    double time_taken; 
    t = clock(); 
    int status; 
    pid_t idProcesso, pai; 
    printf("P1 created: %d\n", getpid()); 

    idProcesso = fork(); 
    switch(idProcesso) 
    { 
     case -1: exit(1); 

     case 0: //P2 
      printf("P2 created: %d - son of P1: %d\n", getpid(), getppid()); 
      idProcesso = fork(); 
      switch(idProcesso) 
      { 
       case -1: exit(1); 

       case 0: //P4 
        printf("P4 created: %d - son of P2: %d\n", getpid(), getppid()); 
        sleep(1); 
        break; 
       default://P2 
        idProcesso = fork(); 
        switch(idProcesso) 
        { 
         case -1: exit(1); 

         case 0://P5 
          printf("P5 created: %d - son of P2: %d\n", getpid(), getppid()); 

          break; 
         default://P2 
          sleep(1); 
          break; 
        } 

        break; 
      } 
      break; 
     default: 

      idProcesso = fork(); 
      switch(idProcesso) 
      { 
       case -1: exit(1); 

       case 0://P3 
        printf("P3 created: %d - son of P1: %d\n", getpid(), getppid()); 
        idProcesso = fork(); 
        switch(idProcesso) 
        { 
         case -1: exit(1); 

         case 0://P6 
          printf("P6 created: %d - son of P3: %d\n", getpid(), getppid()); 
          sleep(1); 
          break; 
         default://P3 
          idProcesso = fork(); 
          switch(idProcesso) 
          { 
           case -1: exit(1); 

           case 0://P7 
            printf("P7 created: %d - son of P3: %d\n", getpid(), getppid()); 
            break; 
           default://P3 

            break; 
          } 
          sleep(1); 
         break; 
        } 
        break; 

       default: 
        sleep(4); 
        break; 
      } 
      break; 
    } 



    printf("Process id: %d terminated\n", getpid()); 
    exit(0); 
} 
+0

在最近的linux上你可以重新父進程:http://stackoverflow.com/questions/6476452/process-re-parenting-controlling-who-is-the-new-parent –

+0

你需要有全部7個進程在同一時間運行,或者連續地建造樹,當你走的時候,根/枝會死掉? p7應該是最後一個進程? –

+0

而..爲什麼你不能只使用一些傳統的同步機制?像管子一樣。 –

回答

1

過程相互獨立運行,所以你只需要:

  1. 讓您的子進程保持足夠長的時間,以便創建所有進程,這很容易使用sleep();和

  2. 不要啓動wait()荷蘭國際集團任何您的孩子的過程,直到你已經創建了他們所有。

下面是一個例子:

#define _POSIX_C_SOURCE 200809L 

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/wait.h> 

int main(void) 
{ 
    pid_t pids[8] = {0}; 
    printf("P1 created (%lu).\n", (unsigned long) getpid()); 

    for (size_t i = 1; i < 3; ++i) { 
     pids[i] = fork(); 

     if (pids[i] == -1) { 
      perror("fork() error"); 
      exit(EXIT_FAILURE); 
     } 
     else if (pids[i] == 0) { 
      printf("P%zu created (%lu).\n", i + 1, (unsigned long) getpid()); 

      for (size_t j = 1 + i * 2; j < 3 + i * 2; ++j) { 
       pids[j] = fork(); 

       if (pids[j] == -1) { 
        perror("fork() error"); 
        exit(EXIT_FAILURE); 
       } 
       else if (pids[j] == 0) { 
        printf("P%zu created (%lu).\n", j + 1, 
          (unsigned long) getpid()); 
        sleep(8 - j); 
        printf("P%zu exiting.\n", j + 1); 
        exit(EXIT_SUCCESS); 
       } 
      } 

      for (size_t j = 2 + i * 2; j >= 1 + i * 2; --j) { 
       if (waitpid(pids[j], NULL, 0) == -1) { 
        perror("waitpid() error"); 
        exit(EXIT_FAILURE); 
       } 
       printf("Waited for P%zu (%lu).\n", j + 1, 
         (unsigned long) pids[j]); 
      } 

      printf("P%zu exiting.\n", i + 1); 
      exit(EXIT_SUCCESS); 
     } 
    } 

    for (size_t i = 2; i > 0; --i) { 
     if (waitpid(pids[i], NULL, 0) == -1) { 
      perror("waitpid() error"); 
      exit(EXIT_FAILURE); 
     } 
     printf("Waited for P%zu (%lu).\n", i + 1, (unsigned long) pids[i]); 
    } 

    printf("P1 exiting.\n"); 

    return 0; 
} 

與輸出:

[email protected]:~/src/sandbox$ ./procs 
P1 created (27206). 
P2 created (27207). 
P3 created (27208). 
P4 created (27209). 
P5 created (27210). 
P6 created (27211). 
P7 created (27212). 
P7 exiting. 
Waited for P7 (27212). 
P6 exiting. 
Waited for P6 (27211). 
P3 exiting. 
Waited for P3 (27208). 
P5 exiting. 
Waited for P5 (27210). 
P4 exiting. 
Waited for P4 (27209). 
P2 exiting. 
Waited for P2 (27207). 
P1 exiting. 
[email protected]:~/src/sandbox$ 

注意在哪些進程正在運行的順序本質上是不可預測的,所以每次你運行它,你可以得到與上述結果略有不同。我沒有試圖讓他們按順序創建或退出,除了在四個葉子進程中對sleep()進行了半心半意的嘗試,否則這只是爲了讓他們活得足夠長。您幾乎可以通過對sleep()的戰略性呼叫保證執行和終止訂單,或者保證它使用某種形式的進程間通信。作爲一般規則,你不應該關心這個順序,而是關心你的實際工作是否按順序完成。

但是,它滿足保持所有進程活着的標準之前,任何人開始死亡。

+0

謝謝你。編譯你的代碼我得到了這個:'P1(3808),P3(3810),P2(3809)' - >不同的訂單,但我們可以看到因爲Pids而沒問題。但現在我有:'P6(3811)P4(3813)P7(3812)P5(3814)'。由於Pids的順序不同,這是不是意味着它是以錯誤的順序創建的? – PlayHardGoPro

+1

@PlayHardGoPro:我將你的這部分內容轉給我:「請注意,進程運行的順序本質上是不可預測的」。 –

+0

你搖滾!完美的工作。但我無法理解那些for循環的原因。我更新了我的代碼,它創建了很好的BUT進程並沒有按照正確的順序死去。 (父母之前的孩子)。你能快速看一下嗎? – PlayHardGoPro