簡體   English   中英

用叉子打印

[英]Printing using fork

我無法理解輸出打印的順序......

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    int index;
    for (index = 1; index < 4; index++)
    {
        printf("HI\n"); 
        fork();
    }
    printf("Unix System Programming\n");
    exit(0);
}

很容易理解,當我只打印unix編程系統時,fork工作2 ^ n次...但是當我用它打印HI時...我不明白為什么這個序列? 輸出:

HI
HI
HI
HI
HI
Unix System Programming
HI
HI
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming

為了更好的可見性,我剛剛修改了代碼

int main(void)   
    {  
        int index;  
        for (index = 1; index < 4; index++)  
        {
            printf("We are in loop with  process id=%d\n",getpid());
            if(!fork()) { //Child process
               printf("Came inside chid with process id=%d\n",getpid());
              // _exit(0);
            } else {     //Parent process
              printf("Came inside parent with process id=%d\n",getpid());
           }
        }
        printf("We are out of loop with  process id=%d\n",getpid());
        exit(0);
    }

    [OP]   
    We are in loop with  process id=2258   -----> Main Parent   
    Came inside parent with process id=2258   
    We are in loop with  process id=2258  
    Came inside child with process id=2259  
    We are in loop with  process id=2259  
    Came inside parent with process id=2258  
    We are in loop with  process id=2258  
    Came inside parent with process id=2259  
    We are in loop with  process id=2259  
    Came inside parent with process id=2258  
    We are out of loop with  process id=2258 ----> "Outer block of loop referring main Parent  Here exit(0) will rip parent process of pid 2258 "      
    Came inside parent with process id=2259     
    We are out of loop with  process id=2259   
    Came inside child with process id=2263   
    We are out of loop with  process id=2263   
    Came inside child with process id=2261   
    We are in loop with  process id=2261   
    Came inside parent with process id=2261   
    We are out of loop with  process id=2261   
    Came inside child with process id=2264   
    We are out of loop with  process id=2264   
    Came inside child with process id=2262   
    Came inside child with process id=2260   
    We are out of loop with  process id=2262   
    We are in loop with  process id=2260   
    Came inside parent with process id=2260   
    We are out of loop with  process id=2260   
    Came inside child with process id=2265   
    We are out of loop with  process id=2265   

首先,我們必須考慮每次執行3次的循環,fork將生成1個子節點,並且肯定會有一個父節點,執行將完全依賴於調度可能是子節點或可能是父節點。

一旦你有更多的迭代值,這將很難理解。

現在讓孩子清理干凈

    int main(void)   
            {  
                int index;  
                for (index = 1; index < 4; index++)  
                {
                    printf("We are in loop with  process id=%d\n",getpid());
                    if(!fork()) { //Child process
                       printf("Came inside chid with process id=%d\n",getpid());
                       _exit(0);
                    } else {     //Parent process
                      printf("Came inside parent with process id=%d\n",getpid());
                   }
                }
                printf("We are out of loop with  process id=%d\n",getpid());
                exit(0);
            }

    We are in loop with  process id=2393
Came inside parent with process id=2393
We are in loop with  process id=2393
Came inside chid with process id=2394
Came inside parent with process id=2393
We are in loop with  process id=2393
Came inside parent with process id=2393
We are out of loop with  process id=2393
Came inside chid with process id=2396
Came inside chid with process id=2395

因此,OP現在更加明確三次父級執行和三次子執行。

無法保證計算機選擇調度進程的順序,一個進程可以在另一個進程執行單行之前完全執行。

輸出的一種可能的解釋:

列是進程,索引在括號中

“USP”是“Unix系統編程”
“FORK”是一個進程分叉的時候
“CREATE”是創建進程的時間

1      2      3      4      5      6      7      8
HI (1)
FORK   CREATE
HI (2)
FORK          CREATE
       HI (2)
       FORK          CREATE
HI (3)
FORK                        CREATE
                     HI (3)
                     FORK          CREATE
USP
              HI (3)
              FORK                        CREATE
       HI (3)
       FORK                                      CREATE
       USP
              USP
                     USP
                            USP
                                   USP
                                          USP
                                                 USP

以上不太可能是實際輸出,因為我們不知道計算機調度進程的順序。您可以向print語句添加進程ID以指示任何給定輸出行屬於哪個進程。

通過查詢每個進程的PID,可以獲得更詳細的視圖:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    int index;
    for (index = 1; index < 4; index++)
    {
        printf("HI: pid %d with i=%d will now fork\n", getpid(), index);
        if (0 == fork())
        {
                printf("I am a new process with i=%d and pid=%d\n", index, getpid());
        }
    }
    printf("Unix System Programming from pid %d\n", getpid());
    exit(0);
}

至於printf()s的序列,事實是產生的新進程在完全生成之前需要一個隨機時間(很小,幾乎為零,但是隨機),並且該時間與該時間具有相同的數量級。父進程生成進一步進程並終止所需的時間。

因此,您可能有:

process 1 prints "HI" and spawns process 2
process 2 prints "HI" and...
process 1 prints "HI" and spawns process 3
                             ... spawns process 4
...process 2 terminates ("Unix programming")
...process 3 terminates ("Unix programming")
...process 4 prints "HI"

並且下一次可能在處理3產生之前,處理2成功打印HI,依此類推。 如果需要確定性序列,則必須實現某種類型的進程同步。

暫無
暫無

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

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