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. processing学习整理---Input(输入)

    1.鼠标1D. 左右移动鼠标可移动天平. “mouseX”变量用于控制矩形的大小和颜色. void setup(){ size(640,360); noStroke(); colorMode(RGB, ...

  2. html里id和name的异同

    id与name的作用,作为标签的标识符,基本上是一样的. name是老方法,id是在name基础上发明的,比name“现代化”一点,用的范围广一点 <...>中的name原来(刚发明时)就 ...

  3. JMeter报错 ERROR o.a.j.t.JMeterThread: Test failed!

    第一次用JMeter,然后跟着教程走,发现进行测试的时候直接报错 显示如下 反复测试依然报错,网上搜索也没什么结果,自己测试了一下才发现问题. 左边创建了CSV DATA 但是并没有进行设置  导致报 ...

  4. WebUI 常用

    //鼠标移动显示div //position:absolute这个是绝对定位:是相对于浏览器的定位.比如:position:absolute:left:20px;top:80px; 这个容器始终位于距 ...

  5. chromedriver驱动的浏览器和真实浏览器之间的差异

    一. 打印百度首页底部的声明 如图,想打印@2018 Baidu...后面的一长串文字,可以通过class name定位的形式 可以看出,只有一个class name是"copyright- ...

  6. 《Advanced Bash-scripting Guide》学习(四):一个显示时间日期登录用户的脚本

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者杨春敏 黄毅 编写一个脚本,显示时间和日期,列出所有的登录用户,显示系统的更新时间.然后这个脚本 ...

  7. 算法练习1---桶排序java版

    今天复习了桶排序. 例如现在有满分为10分的试卷,学生得分分别为2,8,5,3,5,7,现在要给这些分数按照从大到小输出,使用桶排序的思想:有11个桶,每个桶有一个编号,编号从0-10,每出现一个分数 ...

  8. Eclipse 快捷键大全(群里共享的,留下来以后兴许会用到)

    Eclipse快捷键大全Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行 ...

  9. 《精通.NET企业项目开发》 - 书摘精要

    (P7) 处于任何逻辑层面上的类,对于同一层面上的其他类应该是可重用的:对于在同等范围内其他所有需要该数据的类而言,提供数据的类应该是可以被调用的: (P9) 大多数企业系统都是用平台无关的技术构建的 ...

  10. python在字符串中查找字符

    两类函数: find(),rfind() index(),rindex() 找到了都返回下标. find找不到返回-1,index找不到抛出ValueError. 带r的表示从右向左找. 都可以使用第 ...