簡體   English   中英

在 C 中創建子進程

[英]Creation of child processes in C

是否有可能幫助我創建一個簡短的程序,其中父進程產生 1 個子進程,子進程產生下一個子進程,隨后的子進程產生下一個子進程等等......這樣所有的孩子總共 16 個. 如何使用 pstree 命令,顯示這些進程的樹? 直到現在我都能寫出類似的東西,但我不確定它是否正確:

int main()
{
    for(int i = 0; i < 16; ++i)
    {
        pid_t pid = fork();
        if(pid != 0)
        {
            waitpid(pid, NULL, 0);
            return 0;
        }
    }

    sleep(5);
    return 0;
}

正如我在評論中指出的,添加診斷打印以幫助您了解正在發生的事情。 關鍵值是循環控制i 、過程的 PID 和 PPID 以及pid變量中的值。 您還想檢測waitpid()調用。

這導致了這樣的代碼:

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

int main(void)
{
    for (int i = 0; i < 16; ++i)
    {
        pid_t pid = fork();
        printf("%d: (PPID = %d) - post fork %d: pid = %d\n",
               (int)getpid(), (int)getppid(), i, (int)pid);
        fflush(0);
        if (pid != 0)
        {
            int status;
            int corpse = waitpid(pid, &status, 0);
            printf("%d: (PPID = %d) child %d exited with status 0x%.4X\n",
                   (int)getpid(), (int)getppid(), corpse, status);
            fflush(0);
            return 0;
        }
    }

    printf("%d: (PPID = %d) sleeping\n", (int)getpid(), (int)getppid());
    fflush(0);
    sleep(5);
    return 0;
}

如果將輸出運行到終端,則不需要fflush()調用。 如果您通過管道運行輸出(就像我在生成下面的輸出時所做的那樣),它們就變得至關重要。 另請參閱fork()之后的printf()異常 如果替換則return 0; ifreturn i; ,您還會獲得一些有趣的信息。

運行該程序會導致類似以下的輸出:

74830: (PPID = 72799) - post fork 0: pid = 74833
74833: (PPID = 74830) - post fork 0: pid = 0
74833: (PPID = 74830) - post fork 1: pid = 74834
74834: (PPID = 74833) - post fork 1: pid = 0
74834: (PPID = 74833) - post fork 2: pid = 74835
74835: (PPID = 74834) - post fork 2: pid = 0
74835: (PPID = 74834) - post fork 3: pid = 74836
74836: (PPID = 74835) - post fork 3: pid = 0
74836: (PPID = 74835) - post fork 4: pid = 74837
74837: (PPID = 74836) - post fork 4: pid = 0
74837: (PPID = 74836) - post fork 5: pid = 74838
74838: (PPID = 74837) - post fork 5: pid = 0
74838: (PPID = 74837) - post fork 6: pid = 74839
74839: (PPID = 74838) - post fork 6: pid = 0
74839: (PPID = 74838) - post fork 7: pid = 74840
74840: (PPID = 74839) - post fork 7: pid = 0
74840: (PPID = 74839) - post fork 8: pid = 74841
74841: (PPID = 74840) - post fork 8: pid = 0
74841: (PPID = 74840) - post fork 9: pid = 74842
74842: (PPID = 74841) - post fork 9: pid = 0
74842: (PPID = 74841) - post fork 10: pid = 74843
74843: (PPID = 74842) - post fork 10: pid = 0
74843: (PPID = 74842) - post fork 11: pid = 74844
74844: (PPID = 74843) - post fork 11: pid = 0
74844: (PPID = 74843) - post fork 12: pid = 74845
74845: (PPID = 74844) - post fork 12: pid = 0
74845: (PPID = 74844) - post fork 13: pid = 74846
74846: (PPID = 74845) - post fork 13: pid = 0
74846: (PPID = 74845) - post fork 14: pid = 74847
74847: (PPID = 74846) - post fork 14: pid = 0
74847: (PPID = 74846) - post fork 15: pid = 74848
74848: (PPID = 74847) - post fork 15: pid = 0
74848: (PPID = 74847) sleeping
74847: (PPID = 74846) child 74848 exited with status 0x0000
74846: (PPID = 74845) child 74847 exited with status 0x0000
74845: (PPID = 74844) child 74846 exited with status 0x0000
74844: (PPID = 74843) child 74845 exited with status 0x0000
74843: (PPID = 74842) child 74844 exited with status 0x0000
74842: (PPID = 74841) child 74843 exited with status 0x0000
74841: (PPID = 74840) child 74842 exited with status 0x0000
74840: (PPID = 74839) child 74841 exited with status 0x0000
74839: (PPID = 74838) child 74840 exited with status 0x0000
74838: (PPID = 74837) child 74839 exited with status 0x0000
74837: (PPID = 74836) child 74838 exited with status 0x0000
74836: (PPID = 74835) child 74837 exited with status 0x0000
74835: (PPID = 74834) child 74836 exited with status 0x0000
74834: (PPID = 74833) child 74835 exited with status 0x0000
74833: (PPID = 74830) child 74834 exited with status 0x0000
74830: (PPID = 72799) child 74833 exited with status 0x0000

如果您感覺更加挑剔,您可以為打印操作添加計時——您可能需要在微秒級別進行報告才能看到很大的差異。

暫無
暫無

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

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