叉()是如何工作的,当孩子叉? [英] How is fork() working when children fork?

查看:148
本文介绍了叉()是如何工作的,当孩子叉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经执行code块。而且它,如下所示:

I have executed a block of code. And it is as shown below:

#include<stdio.h>

main() {
int i=0;
fork();
printf("The value of i is:%d\n",++i);
fork();
printf("The value of j is:%d\n",++i);
fork();
wait();
}

和我有以下的输出:

The value of i is:1
The value of j is:2
The value of i is:1
The value of j is:2
The value of j is:2
pckoders@ubuntu:~$ The value of j is:2

谁能给我解释一下什么样的角色叉()和wait()函数在这里玩?

Can anyone explain to me what role fork() and wait() functions play here?

推荐答案

该程序生成的进程树。在每一个,两本树枝。如果你抢到了一张纸,它不是很难得出这棵树;这是很难的唯一的事情是获得 I 值正确,由于您使用preFIX的 ++ 。如果让每个进程睡觉在结束了几秒钟,你还可以观察使用树中的 pstree 程序。

The program generates a tree of processes. At every fork, this tree branches in two. If you grab a piece of paper, it's not very hard to draw this tree; the only thing that's hard is getting the i values right due to your use of prefix ++. If you let each of the process sleep for a few seconds at the end, you can also observe the tree using the pstree program.

流程中的每一个然后运行系统调用,它的等待其子的任何一个(在这个过程中树子节点)处理来完成。

Each one of the processes then runs the wait system call, which waits for any one of its child processes (child nodes in the process tree) to finish.

这篇关于叉()是如何工作的,当孩子叉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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