多个分叉的输出问题



大家好,我有这个示例代码,但我不明白它的输出是什么,你们中的任何人都可以为我解释一下吗?

TNX提前

int main(){
 int i, j;
 i = j = 0;
 fork();
 j++;
 printf("i = %d, j = %d n", i, j);
 if(fork())
 {
    i += 5;
    fork();
    j *= 3;
    printf("i = %d, j = %d n", i, j);
}
else
{
    i = j;
    printf("i = %d, j = %d n", i, j);
    i--;
    if(fork() == 0)
        printf("i = %d, j = %d n", i, j);  
}}

每次代码调用 fork () 函数时,它都会创建一个与自身重复的进程。

fork () 将子进程的进程 ID 返回给父进程,将零返回给子进程。

 if (fork ())
 {
     // Code executes in the parent process but not the child process.
  }
 else
 {
     // Code executes in the child process but not in the parent process.
 }

if (fork () == 0)
 // Code executes in the child process

然后,您可以绘制一棵小树,显示新流程的创建和输出的内容。

最新更新