为什么叉程序的printf执行更多的时间 [英] why does fork program printf execute more time

查看:158
本文介绍了为什么叉程序的printf执行更多的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从叉我读叉创建父进程重复的手册页。但无法理解为什么下面的程序的printf 执行 8 倍。我在linux读叉工作()链接也。

 的#include<&stdio.h中GT;
诠释的main()
{
    叉子();
    叉子();
    叉子();
    的printf(进程\\ n);
}


解决方案
在这个

在一般为 N 方式将执行下一个语句(在这种情况下,的printf 2 ^ N 倍。
方法如下:

  |
+ -fork()---------------------------------- +
| |
+ -fork()------------- + + -fork()------------- +
| | | |
+ -fork()--- + + -fork()--- + + -fork()--- + + -fork()--- +
| | | | | | | |
打印()打印()打印()打印()打印()打印()打印()的print()

from the man page of fork i read fork create duplicate of parent process. But not able to understand why below program printf execute 8 times. I read Working of fork() in linux link also.

#include <stdio.h>
int main()
{
    fork();
    fork();
    fork();
    printf("process\n");
}

解决方案

In general for n forks in this manner will execute the next statements (in this case printf) 2^n times. Here is how:

|
+-fork()----------------------------------+
|                                         |
+-fork()-------------+                    +-fork()-------------+
|                    |                    |                    |
+-fork()---+         +-fork()---+         +-fork()---+         +-fork()---+
|          |         |          |         |          |         |          |
print()    print()   print()    print()   print()    print()   print()    print()

这篇关于为什么叉程序的printf执行更多的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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