操作系统:该程序创建了多少个进程? [英] OS: How many process are created by the program?

查看:315
本文介绍了操作系统:该程序创建了多少个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<unistd.h>

int main()
{
    int i;
    for(i=0;i<4;i++)
        fork();
    return 0;
}

我的问题:包括最初的父进程,该程序创建了多少个进程?

my question:Include the initial parent process, how many processes created by the program?

我认为这个答案是1 + 4 = 5,1个父进程和4个子进程,对吗?

I think this answer is 1+4=5 , 1 parent process and 4 child process ,am i right?

谢谢

推荐答案

for(i=0;i<4;i++)
    fork();

这段代码等效于:

fork();
fork();
fork();
fork();

让我们说这个过程从p0开始.流程图如下所示:

Lets say that process start with p0. The process graph will look like:

上面的图片说明了一切,当第一个fork()被执行时,它会创建一个新进程,剩下三个fork()可以执行.现在,这里有一个父线程和一个子线程来执行另外三个fork()语句.因此,子进程p1将创建p5,p6和p7,而父进程将创建p2,p3和p4. 在创建了p2之后,还需要对此子线程p2执行两个新的fork(),该子线程p2是在执行第二个fork()语句之后创建的. 这样,将创建此父子进程.

The above image says it all, when first fork() gets executed it creates a new process and three fork() are left to execute. Now, here exists a parent thread and a child thread to execute three more fork() statments. So, child process p1 will create p5,p6 and p7 and parent process will create p2, p3 and p4. After p2 has been created two more fork() needs to be executed for this child thread p2 which was created after execution of 2nd fork() statment. In this way, this parent-child processes are created.

这篇关于操作系统:该程序创建了多少个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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