我有一個C文件看起來像這樣:控制福克斯用C
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t child_pid;
printf ("The PID is %d\n", (int) getpid());
child_pid = fork();
if (child_pid != 0)
{
printf ("this is the parent process, with PID %d\n",
(int)getpid());
printf ("the child's PID is %d\n", (int) child_pid);
}
else
printf ("this is the child process, with PID %d\n",
(int)getpid());
return 0;
}
我需要修改它來產生AA層次結構,看起來像
parent (0)
|
+---> child (1)
|
+---> child (2)
|
+----> child (3)
|
+----> child (4)
|
+----> child (5)
|
基本上是一個樹形結構,其中每個第二孩子讓兩個新的孩子。據我瞭解,當我的一個進程中,每個進程都會同時運行。在if
語句中添加一個fork()
似乎可以正常工作並正確創建進程0到2,因爲只有父級會創建新的分支。但我不知道如何使流程2分叉,而不是1.任何想法?
你有沒有考慮過這個過程的侷限性?何時會停止,爲什麼?它會對操作系統做什麼? (在unix衍生系統上,請考慮閱讀'ulimit'的手冊頁。)只是試圖提供幫助。 – dmckee 2009-08-06 13:28:55