叉()系统调用在C [英] fork() system call in c

查看:173
本文介绍了叉()系统调用在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
    #包括LT&;&unistd.h中GT;
    诠释的main()
    {
       叉子();
       叉()及&放大器;叉()||叉子();
       叉子();     的printf(分叉\\ n);
     返回0;
    }

这会造成困难,了解如何计算的执行程序后产生的进程数?
帮我找出来。

平台--UBUNTU 10.04


解决方案

让我们跟随叉树,假设没有叉失败


  

叉();


现在我们有两个过程,至今也无所谓谁的孩​​子,谁父母,给他们打电话p1和p2


  

叉()


这两项进程产生另一个孩子,所以我们有4个流程,其中两个(P3,P4)的结果为零,其他两个(P1和P2)是非零的


 &放大器;&安培;叉子()


p1和p2叉再次,给P5和P6,六道工序总数。在P1和P2的&放大器;&安培; 的值为true,这样他们就不会在这一行再次分叉。对于P3,P4,P5,P6,在&放大器;&安培; 计算结果为假,所以他们叉


  ||叉子();


这里,产卵四个新的工艺,得到共6 + 4 = 10


  

叉();


每一次的10个进程叉,使20。

    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
       fork();
       fork() && fork() || fork();
       fork();

     printf("forked\n");
     return 0;
    }

It cause difficulty to understand how to calculate number of processes spawned after executing the program? Help me to find out.

Platform --UBUNTU 10.04

解决方案

Let's follow the fork-tree, assuming none of the forks fails

fork();

Now we have two processes, so far it doesn't matter who's child and who parent, call them p1 and p2

fork()

Both of those processes spawn another child, so we have 4 processes, for two of them (p3, p4) the result is zero, for the other two (p1 and p2) it's nonzero

   && fork()

p1 and p2 fork again, giving p5 and p6, six processes total. In p1 and p2, the && evaluates to true, so they don't fork again in this line. For p3, p4, p5, p6, the && evaluates to false, so they fork

              || fork();

here, spawning four new processes, giving a total of 6 + 4 = 10.

fork();

each of the 10 processes forks again, makes 20.

这篇关于叉()系统调用在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆