【问题标题】:How many processes (including the parent) will be created assuming each fork successfully executes假设每个 fork 成功执行,将创建多少个进程(包括父进程)
【发布时间】:2017-12-13 01:45:54
【问题描述】:

我相信这会创建 5 个进程;但是,我需要验证。这些问题常常难倒我。谢谢您的帮助!

void forktest()
{
   printf("L0\n");
   if (fork() != 0) 
   { 
       printf("L1\n");
       if (fork() != 0)
       {
           printf("L2\n");
           fork();
       }
   }
   printf("Bye\n");
}

【问题讨论】:

    标签: operating-system fork


    【解决方案1】:
    main
       |
       |
    fork  --- (first child)
       |
       |
    fork  --- (second child)
       |
       |
    fork  --- (third child)
    

    void forktest()
    {
       printf("L0\n");
       if (fork() != 0) <----- first child, only parent goes inside the if
       { 
           printf("L1\n");
           if (fork() != 0) <------ second child, only parent goes inside the if
           {
               printf("L2\n"); 
               fork();       <------ third child
           }
       }
       printf("Bye\n");
    }
    

    即main创建的3个进程。

    【讨论】:

      猜你喜欢
      • 2017-02-25
      • 2023-03-15
      • 2015-01-05
      • 1970-01-01
      • 2021-01-18
      • 2014-02-14
      • 2020-10-19
      • 2013-10-07
      • 2014-07-20
      相关资源
      最近更新 更多