我有一個父進程,我想從中創建4個子進程,而2個子進程每個都有一個子進程。父進程有多個子進程和2個子進程有一個子進程
我能夠從父級創建4個子進程。但是當我嘗試創建2子進程的子進程時,我有一個無限循環。 我用循環來創建4個進程並添加了一個條件,即創建第二個子進程和第四個子進程時,爲每個進程創建一個子進程。
- 家長有(child1 chil2 child3孩子4)
- CHILD2有(孩子5 child6)
來源:
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc, char **argv)
{
int i;
for(i = 0; i <= 3; i++)
{
if(fork() == 0)
{
printf("process %d and his parent is %d \n",
getpid(), getppid());
if(i = 1)
{
if(fork() == 0)
{
printf("process %d and his parent is %d \n",
getpid(), getppid());
break;
}
}
}
}
return 0;
}
發佈最小代碼。無法看到你做錯了,否則。 –
的#include 的#include 的#include 的#include #包括在 INT主(INT的argc,焦炭** argv的) { INT I;對於(i = 0; i <= 3; i ++) if(fork()== 0) printf(「process%d and their parent is%d \ n」,getpid(),getppid ()); \t \t如果(I = 1){ \t \t \t如果(叉()== 0){ \t \t \t的printf( 「進程%d和他的父母是%d \ n」 個,GETPID(),getppid( )); \t \t \t \t break; \t \t \t} \t \t \t} } } 返回0; } –
Kazaloco
系統正常工作!現在您已經發布了兩個重新投票的代碼。在評論中不低於:) –