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. java基础(4)-数组(1)

    数组:存储同一种数据类型的多个元素的容器数组初始化: 元素类型[] 数组名 = new 元素类型[数组长度]int [] arr = new int[5] 元素类型[] 数组名 = new 元素类型[ ...

  2. Anton and School - 2 (组合数学)

    题意:给你一串只有‘(’与‘)’的字符串,问你多少对括号,括号一定是左边一半的‘(’,右边一半是‘)’ )(()()   答案是:6 题解:枚举每个‘(’,此时设左括号左边有n个‘(’,它右边有m个‘ ...

  3. thinkphp中composer方式安装的插件如何使用

    thinkphp中composer方式安装的插件如何使用 一.总结 一句话总结:任何东西都是可以百度或者google搜到的,要自己先想怎么解决,解决不出来迅速百度,不要自己臆断 1.在tp5框架的什么 ...

  4. 按键(ESC ,F1,F2等)——wpf的命令处理方法

    WPF窗体的命令绑定   方法一:使用代码 <WpfUI:View.CommandBindings> <CommandBinding Command="Help" ...

  5. 选择排序的php实现 Selection Sort

    选择排序Selection Sort的PHP实现,安全按照算法所写. 同一排序算法下,需要趟数最多的数列是什么数列呢?思考中. 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序 ...

  6. LVM MBR分区(装载)

    必须有至少一个主分区(P),主分区个数+扩展分区个数<= 4个. 创建完主分区,可以创建扩展分区(E),扩展分区可以有1个,或者没有(扩展分区). 主分区(Primary  Partion)可以 ...

  7. angular复选框式js树形菜单(二)

    删除(过滤)树形结构某一个子节点: function filterTreeData(treeData){ angular.forEach(treeData,function(item){ if (it ...

  8. Linux-压缩与解压缩命令

    常用的压缩格式:.zip   .gz   .bz2   .tar.gz   .tar.bz2 1.Zip 压缩文件:zip 压缩文件名  源文件 压缩目录:zip -r 压缩文件名 源文件 解压缩.z ...

  9. python 计数器类Counter的用法

    简单操作: import collections A=['a','b','b','c','d','b','a'] count=collections.Counter(A) print(count) C ...

  10. MySQL实战 | 05 如何设计高性能的索引?

    原文链接:MySQL | 05 如何设计高性能的索引? 上回我们主要研究了为什么使用索引,以及索引的数据结构.今天带你了解如何设计高性能的索引. 其中,有这么一个点,说的是 InnoDB 引擎中使用的 ...