3.2 Including the initial parent process, how many processes are created by the program shown in Figure?

答案: 会创建8个进程。

解析:相关知识: fork函数的执行原理

  fork函数执行一次, 返回两次,父进程返回子进程的PID, 子进程返回0(至于返回值为什么不同, 这与do_fork函数有关,具体是怎么一回事我还不太清楚)。程序中可利用此性质来区别程序的父进程和子进程。

  当一个进程执行fork函数时, 其新建的子进程得到一份父进程用户级虚拟内存空间的拷贝, 包括文本、数据和bbs段、堆和用户栈, 子进程会得到一个和父进程相同的PCB。因为程序在操作系统中运行的时候,程序执行指令是由program counter(程序计数器)来控制的, 因为子进程PCB中的program counter信息和父进程PCB的program counter信息相同, 所以子进程会继续向后执行指令, 而不会执行父进程fork之前的指令。执行如下图显示的程序,fork出来的子进程不会执行fork之前的printf语句。

程序的运行结果:p hello

        p world

  现在来讲解一下3.2题的程序。画进程图。 假设在return 0之前printf一个hello

  当程序运行到第一个fork时, 创建一个子进程c1, 在上面的进程图中, 用垂直的箭头表示父进程创建了一个子进程, 与垂直箭头相连的线代表新建的子进程的进程线。 在子进程c1中, 会继续向下执行剩余的两个fork, 在执行剩余两个fork中的第一个时, 又会创建一个子进程c2, 而子进程c2会继续向下执行剩余的最后一个fork, c2又会创建一个新的子进程c3, c3执行printf, 之后return 0,c3进程结束, c2之后也会执行printf并return 0结束。 图中的进程都是按照程序运行的逻辑画出来的。 所以, 包含最初的父进程在内, 一共有8个进程。

[Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?的更多相关文章

  1. [Chapter 3 Process]Practice 3.12 Including the initial parent process, how many processes are created by the program shown in Figure 3.32?

    3.12 Including the initial parent process, how many processes are created by the program shown in Fi ...

  2. [Chapter 3 Process]Practice 3.5 When a process creates a new process using the fork() operation

    3.5 When a process creates a new process using the fork() operation, which of the following state is ...

  3. [Chapter 3 Process]Practice 3.3 Discuss three major complications that concurrent processing adds to an operating system.

    3.3  Original version of Apple's mobile iOS operating system provied no means of concurrent processi ...

  4. [Chapter 3 Process]Practice 3.9 Describe the actions token by a kernel to content-switch between processes.

    3.9 Describe the actions token by a kernel to content-switch between processes. 答案: 内核在进行进程上下文切换时, 首 ...

  5. [Chapter 3 Process]Practice 3.8: Describe the differences among short-term, medium-term, long-term scheduling

    3.8 Describe the differences among short-term, medium-term, and longterm scheduling. 答案: 长期调度决定哪些进程进 ...

  6. [Chapter 3 Process]Practice 3.4 Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets.

    3.4 The Sun UltraSPARC processor has multiple register sets. Describe what happens when a context sw ...

  7. [Chapter 3 Process]Practice 3.1 相关知识:进程创建、fork函数

    3.1 Using the program shown in the Figure3.30, explain what the output will be at LINE A 答案:LINE A 处 ...

  8. Linux process authority、the security risks in running process with high authority

    catalog . Linux进程权限原理 . 最小权限原则 - 进程降权运行最佳实践 . 进程权限控制包含的攻防向量 . 进程高权限安全风险检查技术方案 1. Linux进程权限原理 我们知道,Li ...

  9. 查看进程id, 父进程id _How do I get the parent process ID of a given child process?

    How to get parent pid from a given children pid? I know I can mannully check it under /proc, I am wo ...

随机推荐

  1. 【转】一次完整的HTTP请求所经历的7个步骤

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该 ...

  2. dp4--codeVs1043 方格取数

    dp4--codeVs1043 方格取数 一.心得 二.题目 1043 方格取数 2000年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Dia ...

  3. 262. Trips and Users

    问题描述 解决方案 -- case when 的效率比if的效率高 -- select Trips.Request_at as 'Day', -- round(sum(case Trips.Statu ...

  4. poj1523割顶-点双联通

    题意:求出所有的割顶,而且还有输出该割顶连接了几个点双连通分量 题解:直接tarjan求点双联通分量就好了,可以在加入边的时候记录加入次数,大于1的都是桥,输入输出很恶心,注意格式 #include& ...

  5. hzau 1205 Sequence Number(二分)

    G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...

  6. 条款47:请使用traits class表示类型信息

    在stl的算法中,我们的希望往往是根据不同的迭代器类型进行不同的更有效率的操作: template<typename IterT, typename DistT> void advance ...

  7. poj3268 Silver Cow Party (SPFA求最短路)

    其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...

  8. CentOS X64上64位Oracle 11gR2 静默安装

    CentOS 6.2 X64上64位Oracle 11gR2 静默安装 www.linuxidc.com/Linux/2012-03/56606p4.htm HP-UX静默安装oracle11g过程 ...

  9. 作业派NABCD的特点分析

    Need:根据我们用户的调查,我们发现用户希望在作业派获取一些课本上的答案等类似的东西,以方便及时解决课本的问题. Approach:但是仅仅靠管理员来上传文件时园不能解决用户的问题.所以我们想让我们 ...

  10. 洛谷 P1655 小朋友的球

    题目描述 @发源于 小朋友最近特别喜欢球.有一天他脑子抽了,从口袋里拿出了N个不同的球,想把它们放到M个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法,于是尝试编程实现,但由于他天天 ...