Framist's Little House

◇ 自顶而下 - 面向未来 ◇

0%

【操作系统概念 - 作业 2】Operating-System Structures

Operating System Concepts Exercises 2

Operating-System Structures

操作系统作业 2

  • 网上学习理解程序执行中的函数调用过程
  • 网上学习了解“Linux 启动过程”
  • 2.1、2.2、2.3、3.4、2.5、2.6、2.7、2.8、2.9
  • 2.12、2.13、2.15、2.16、2.17、2.18、2.19、2.21、2.22

每题最后一个引用块是老师提供的参考答案

网上学习理解程序执行中的函数调用过程

答:

程序是如何执行的(三)函数调用

网上学习了解“Linux 启动过程”

答:

计算机的启动过程

Practice Exercises

2.1, 2.2, 2.3, 3.4, 2.5, 2.6, 2.7, 2.8, 2.9

2.1 What is the purpose of system calls?

答:

让用户级进程可以请求操作系统所提供的服务

Answer:

System calls allow user-level processes to request services of the operating system.

2.2 What are the five major activities of an operating system with regard to process management?

进程管理方面,操作系统的五个主要活动是什么?

答:

  • 用户和系统进程地创建和删除
  • 进程的挂起和恢复
  • 提供进程同步的机制
  • 提供进程通信的机制
  • 提供死锁处理的机制

The five major activities are:

a. The creation and deletion of both user and system processes

b. The suspension and resumption of processes

c. The provision of mechanisms for process synchronization

d. The provision of mechanisms for process communication

e. The provision of mechanisms for deadlock handling

2.3 What are the three major activities of an operating system with regard to memory management?

内存管理方面,操作系统的三个主要活动是什么?

答:

  • 跟踪内存使用情况
  • 当内存空间可用的时候,确定将哪些进程调入内存
  • 按需要分配和回收内存

Answer:

The three major activities are:

a. Keep track of which parts of memory are currently being used and by whom.

b. Decide which processes are to be loaded into memory when memory space becomes available.

c. Allocate and deallocate memory space as needed.

2.4 What are the three major activities of an operating system with regard to secondary-storage management?

关于二级存储管理,操作系统的三个主要活动是什么?

答:

  • 剩余空间的管理
  • 存储分配
  • 磁盘调度

Answer:

The three major activities are:

• Free-space management.

• Storage allocation.

• Disk scheduling.

2.5 What is the purpose of the command interpreter? Why is it usually separate from the kernel?

命令解释器的目的是什么?

答:

读取命令 (从用户或者命令文件) 并执行 (转化成系统调用)

为什么它通常与内核分开?

答:

可能会经常改变

Answer:

It reads commands from the user or from a file of commands and creates processes to execute them, usually by turning them into one or more system calls. It is usually not part of the kernel since the command interpreter is subject to changes.

它从用户或命令文件中读取命令,并创建进程来执行这些命令,通常是把它们变成一个或多个系统调用。它通常不是内核的一部分,因为命令解释器是可以改变的。

2.6 What system calls have to be executed by a command interpreter or shell in order to start a new process?

为了启动一个新的进程,命令解释器或外壳必须执行哪些系统调用?

答:

Answer:

In Unix/Linux systems, a fork system call followed by an exec system call need to be performed to start a new process. The fork call clones the currently executing process, while the exec call overlays a new process based on a different executable over the calling process.

在 Unix/Linux 系统中,需要执行 fork 系统调用后 exec 系统调用来启动一个新进程。fork 调用会克隆当前执行的进程,而 exec 调用则会在调用进程上覆盖一个基于不同可执行文件的新进程。

2.7 What is the purpose of system programs?

答:

为程序开发和运行提供了方便的环境

给用户提供基本的公共功能函数,为用户在不用自己写代码的情况下解决公用问题

Answer:

System programs can be thought of as bundles of useful system calls. They provide basic system-level functionality to users so that users do not need to write their own programs to solve common problems.

2.8 What is the main advantage of the layered approach to system design? What are the disadvantages of the layered approach?

系统设计的分层方法的主要优点是什么?

答:

As in all cases of modular design, designing an operating system in a modular way has several advantages. The system is easier to debug and modify because changes affect only limited sections of the system rather than touching all sections of the operating system. Information is kept only where it is needed and is accessible only within a defined and restricted area, so any bugs affecting that data must be limited to a specific module or layer.

与模块化设计的所有情况一样,以模块化的方式设计操作系统有几个优点。该系统更容易调试和修改,因为更改只影响系统的有限部分,而不会影响操作系统的所有部分。信息只保存在需要的地方,并且只能在定义和限制的区域内访问,因此任何影响该数据的 bug 都必须限制在特定的模块或层中。

1、开发人员可以只关注整个结构中的其中某一层;
2、可以很容易的用新的实现来替换原有层次的实现;
3、可以降低层与层之间的依赖;
4、有利于标准化;
5、利于各层逻辑的复用。

[3]

分层方法的缺点是什么?

答:

1、降低了系统的性能。这是不言而喻的。如果不采用分层式结构,很多业务可以直接造访数据库,以此获取相应的数据,如今却必须通过中间层来完成。
2、有时会导致级联的修改。这种修改尤其体现在自上而下的方向。如果在表示层中需要增加一个功能,为保证其设计符合分层式结构,可能需要在相应的业务逻辑层和数据访问层中都增加相应的代码。
[3]

Answer:

The main advantage of the layered approach is modularity. The layered approach is selected such that each layer uses function and services of only lower level layers. This approach simplifies debugging and system verification.

The problem is deciding what order in which to place the layers, as no layer can call upon the services of any higher layer, and so many chicken-and-egg situations may arise.

Layered approaches can also be less efficient, as a request for service from a higher layer has to filter through all lower layers before it reaches the HW, possibly with significant processing at each step.

分层方法的主要优点是模块化。分层方法的选择是,每一层都只使用较低层次的功能和服务。这种方法简化了调试和系统验证。

问题是决定以什么顺序放置这些层,因为没有一个层可以调用任何高层的服务,所以可能会出现许多鸡生蛋蛋生鸡的情况。

分层方法也可能效率较低,因为来自上层的服务请求在到达 HW 之前必须经过所有下层的过滤,可能在每一步都要进行大量的处理。

2.9 List five services provided by an operating system, and explain how each creates convenience for users. In which cases would it be impossible for user-level programs to provide these services? Explain your answer.

列出操作系统提供的五种服务,并解释每种服务如何为用户提供便利。在哪种情况下,用户级程序不可能提供这些服务?解释你的答案。

答:

执行程序
操作系统加载文件的目录到内存中并开始执行。在用户程序不能合理分配 CPU 时间的情况下不能提供该项服务。

I/O 操作

程序运行过程中需要 I/O 设备上的数据时通过 I/O 命令或 I/O 指令,请求操作统的服务。

文件系统操作

文件系统让用户按文件名创建,读写,修改,删除文件,使用方便,安全可靠。当设计多用户访问或共享文件时,操作系统还提供信息保护机制。

通信服务

在很多情况下,一个进程要与另外的进程交换信息,进程通信可以借助共享內存实
现,也可使用消息消息传送技术实现。

错误检测

操作系统可以不做和处理各种硬件和软件造成的差镨和异常,并让他们造成的影响缩小在最小范围内

Answer:

The five services are:

a. Program execution. The operating system loads the contents (or sections) of an executable file into memory and begins its execution. A user-level program could not be trusted to properly allocate CPU time.

b. I/O operations. Disks, tapes, serial lines, and other devices must be communicated with at a very low level. The user need only specify the device and the operation to perform on it, while the system converts that request into device-specific or controller-specific commands. User-level programs cannot be trusted to access only devices which they should have access to and to access them only when they are otherwise unused.

c. File-system manipulation. There are many details in file creation, deletion, allocation, and naming that users should not have to perform. Blocks of disk space are used by files and must be tracked. Deleting a file requires removing the name file information and freeing the allocated blocks. Protections must also be checked to assure proper file access. User programs could neither ensure adherence to protection methods nor be trusted to allocate only free blocks and deallocate blocks on file deletion.

d. Communications. Message passing between systems requires messages to be turned into packets of information, sent to the network controller, transmitted across a communications medium, and reassembled by the destination system. Packet ordering and data correction must take place. Again, user programs might not coordinate access to the network device, or they might receive packets destined for other processes.

e. Error detection. Error detection occurs at both the hardware and software levels. At the hardware level, all data transfers must be inspected to ensure that data have not been corrupted in transit. All data on media must be checked to be sure they have not changed since they were written to the media. At the software level, media must be checked for data consistency; for instance, whether the number of allocated and unallocated blocks of storage match the total number on the device. There, errors are frequently process-independent (for instance, the corruption of data on a disk), so there must be a global program (the operating system) that handles all types of errors. Also, by having errors processed by the operating system, processes need not contain code to catch and correct all the errors possible on a system.
a. 程序执行。操作系统将可执行文件的内容(或部分)加载到内存中并开始执行。不能相信一个用户级程序能正确分配 CPU 时间。
b. I/O 操作。磁盘、磁带、串行线和其他设备必须在一个非常低的水平上进行通信。用户只需要指定设备和要对其进行的操作,而系统则将该请求转换为特定于设备或控制器的命令。不能相信用户级程序只访问他们应该访问的设备,并且只在其他情况下不使用这些设备时才访问。
c. 文件系统的操作。在文件的创建、删除、分配和命名方面,有许多细节是用户不应该执行的。磁盘空间的块被文件使用,必须被跟踪。删除一个文件需要删除名称文件信息并释放分配的块。保护措施也必须被检查,以保证正确的文件访问。用户程序既不能确保遵守保护方法,也不能被信任为只分配空闲块,并在文件删除时取消分配块。
d. 通讯。系统之间的信息传递需要将信息变成信息包,发送到网络控制器,在通信介质上传输,并由目的地系统重新组装。数据包的排序和数据校正必须进行。同样,用户程序可能不会协调对网络设备的访问,或者他们可能会收到以其他进程为目的地的数据包。
e. 错误检测。错误检测同时发生在硬件和软件层面。在硬件层面上,所有的数据传输必须被检查,以确保数据在传输过程中没有被破坏。必须检查媒体上的所有数据,以确保它们在被写入媒体后没有改变。在软件层面,必须检查媒体的数据一致性;例如,已分配和未分配的存储块的数量是否与设备上的总数相符。在那里,错误经常是与进程无关的(例如,磁盘上的数据损坏),所以必须有一个全局程序(操作系统)来处理所有类型的错误。另外,通过由操作系统处理错误,进程不需要包含代码来捕捉和纠正系统上可能出现的所有错误。

Exercises

2.12、2.13、2.15、2.16、2.17、2.18、2.19、2.21、2.22

2.12 The services and functions provided by an operating system can be divided into two main categories. Briefly describe the two categories, and discuss how they differ.

操作系统提供的服务和功能可以分为两大类。简要描述这两个类别,并讨论它们的区别。

答:

操作系统提供的一类服务是在系统中不同并发运行的进程之间强制保护。进程只允许访问与它们关联的内存位置。此外,进程不允许损坏与其他进程相关联的文件。进程也不允许在没有操作系统介入的情况下直接访问设备。

操作系统提供的第二类服务是提供低层硬件不能直接支持的功能。虚拟内存和文件系统就是操作系统提供的两类服务的例子。

Answer

Functions and services which are directly helpful to the user (networking, computer sharing, error detection, consistent computing)

对用户有直接帮助的功能和服务(网络、计算机共享、错误检测、一致计算)。

Functions and resources which ensure efficient operation through resource sharing; resource allocation, user account tracking, and protection/security

通过资源共享确保有效运行的功能和资源;资源分配、用户账户跟踪和保护/安全

2.13 Describe three general methods for passing parameters to the operating system.

描述三种向操作系统传递参数的一般方法。

Answer:

a. Pass parameters in registers 寄存器传递参数

b. Registers pass starting addresses of blocks of parameters 寄存器传递参数的起始地址

c. Parameters can be placed, or pushed, onto the stack by the program, and popped off the stack by the operating system 参数可以由程序放置或推入堆栈,并由操作系统从堆栈中弹出。

2.15 What are the five major activities of an operating system with regard to file management?

在文件管理方面,操作系统的五个主要活动是什么?

Answer:

• The creation and deletion of files

创建和删除文件

• The creation and deletion of directories

目录的创建和删除

• The support of primitives for manipulating files and directories

支持操作文件和目录的原语

• The mapping of files onto secondary storage

文件到辅助存储的映射

• The backup of files on stable (nonvolatile) storage media

在稳定 (非易失性) 存储介质上备份文件

2.16 What are the advantages and disadvantages of using the same system-call interface for manipulating both files and devices?

使用同一个系统调用接口来操作文件和设备的优点和缺点是什么?

优点

可以像访问文件系统中的文件一样访问每个设备。因为大多数内核都是通过文件接口来处理设备的,所以相对容易通过实现具体硬件的代码来支持抽象的文件接口来增加一个新的设备驱动器。因此,这个有利于双方用户代码的发展,它可以以同样的方式来访问设备和文件,并且设备驱动代码可以支持一个定义明确的 API。

缺点

使用相同的接口的弊端在于它可能很难捕捉在文件访问 API 上下文中某些设备的功能,从而导致功能损失或性能损失。

Answer

Each device can be accessed as though it was a file in the file system. Since most of the kernel deals with devices through this file interface, it is relatively easy to add a new device driver by implementing the hardware-specific code to support this abstract file interface. Therefore, this benefits the development of both user program code, which can be written to access devices and files in the same manner, and device-driver code, which can be written to support a well-defined API.

The disadvantage with using the same interface is that it might be difficult to capture the functionality of certain devices within the context of the file access API, thereby resulting in either a loss of functionality or a loss of performance. Some of this could be overcomed by the use of the ioctl operation that provides a general-purpose interface for processes to invoke operations on devices.

每个设备都可以被访问,就好像它是文件系统中的一个文件。由于大部分内核都是通过这个文件接口来处理设备的,所以通过实现特定硬件的代码来支持这个抽象的文件接口,增加一个新的设备驱动程序是比较容易的。因此,这*有利于用户程序代码和设备驱动程序代码的开发,前者可以以同样的方式访问设备和文件,后者可以支持一个定义明确的 API。

使用相同接口的缺点是,可能很难在文件访问 API 的范围内捕获某些设备的功能,从而导致功能的损失或性能的损失。有些问题可以通过使用ioctl 操作来克服,该操作为进程提供了一个通用的接口来调用设备上的操作。

2.17 Would it be possible for the user to develop a new command interpreter using the system-call interface provided by the operating system?

用户是否可以使用操作系统提供的系统调用接口开发一个新的命令解释器?

答:

用户应该能够使用系统提供的系统调用开发新的命令解释器。命令解释器允许用户这样创建和管理进程并且确定它们的通信方式 (通过管道和文件)。由于所有这些功能都可以被用户级的程序进行系统调用,因此用户可以开发出一个新的命令行解释器。

Answer:

Yes.

A user should be able to develop a new command interpreter using the system-call interface provided by the operating system. The command interpreter allows a user to create and manage processes and also determine ways by which they communicate (such as through pipes and files). As all of this functionality could be accessed by a user level program using the system calls, it should be possible for the user to develop a new command-line interpreter.

2.18 What are the two models of interprocess communication?What are the strengths and weaknesses of the two approaches?

答:

进程间通信的两种模型是消息传递模型共享内存模型

消息传递对于交换小数量的消息非常有用,因为不需要避免冲突。它的实现要比共享内存简单。当它在一台计算机中发生的时候,共享内存允许最大速度并且方便通信,因为它可以在内存传输。然而,这种方法在共享内存的进程之间的保护和同步方面做出了妥协

Answer:

One is message-passing model and the other is shared-memory model.

一个是消息传递模型,另一个是共享内存模型

Shared-memory strengths and weaknesses: it allows higher speed and convenience of communication. However, in the areas of protection and synchronization between the processes some problems exist.

共享内存的优点和缺点:它允许更高的速度和便利的通信。然而,在进程之间的保护和同步方面,存在一些问题。

Message-passing strengths and weaknesses: message can be exchanged between the processes either directly or indirectly through a common mailbox. It is useful for exchanging smaller amounts of data and easier to implement for inter-computer communication. However, its speed is slower than shared-memory model.

消息传递的优点和缺点:消息可以在进程之间直接或间接地通过一个共同的邮箱进行交换。它对交换较小数量的数据很有用,对计算机间的通信也更容易实现。然而,它的速度比共享内存模型慢。

2.19 Why is the separation of mechanism and policy desirable?

答:

Answer:

Mechanism and policy must be separate to ensure that systems are easy to modify.

No two system installations are the same, so each installation may want to tune the operating system to suit its needs. With mechanism and policy separate, the policy may be changed at will while the mechanism stays unchanged. This arrangement provides a more flexible system.

机制和策略必须分开,以确保系统易于修改。
没有两个系统的安装是相同的,所以每个安装都可能想调整操作系统以适应其需求。在机制和策略分开的情况下,策略可以随意改变,而机制则保持不变。这种安排提供了一个更灵活的系统。

2.21 What is the main advantage of the microkernel approach to system design? How do user programs and system services interact in a microkernel architecture? What are the disadvantages of using the microkernel approach?

微内核方法在系统设计方面的主要优势是什么?

答:

(a) 添加新服务不需要修改内核

(b) 由于在用户模式下进行的操作比在内核模式下进行的操作多,因此更安全

(c) 更简单的内核设计和功能通常会带来更可靠的结果系统。

Benefits typically include the following:

(a) adding a new service does not require modifying the kernel,

(b) it is more secure as more operations are done in user mode than in kernel mode

(c) a simpler kernel design and functionality typically results in a more reliable operating system.

在微内核体系结构中,用户程序和系统服务如何交互?

答:

用户程序和系统服务在微内核架构中通常使用进程间通信机制例如消息传递来交互。这些消息由操作系统传递。

User programs and system services interact in a microkernel architecture by using inter process communication mechanisms such as messaging. These messages are conveyed by the operating system.

使用微内核方法的缺点是什么?

答:

微内核架构的主要缺点是与进程间通信相关的开销有关,并且频繁的使用操作系统消息传递功能来使用户进程和系统服务可以彼此交互。

The primary disadvantages of the microkernel architecture are the overheads associated with inter process communication and the frequent use of the operating system’s messaging functions in order to enable the user process and the system service to interact with each other (context switching).

2.22 What are the advantages of using loadable kernel modules?

使用**可加载内核模块 (LVM)**的优点是什么?

答:

LKM 与普通应用程序之间的区别主要体现在四个方面。

第一,也是最重要的区别,普通应用程序运行在用户空间,而 LKM 运行在内核空间。通过区分不同的运行空间,操作系统能够安全地保护操作系统中一些重要数据结构的内容不被普通应用程序所修改,达到保证操作系统正常运转的目的。

第二,普通应用程序的目标很明确,它们从头至尾都是为了完成某一项特定任务。而 LKM 是在内核中注册并为后续应用程序的请求提供服务的。

第三,普通应用程序可以调用并没有在其中定义的函数,但一个 LKM 是链接到内核上的,它所能调用的函数只有内核导出来的那些函数。

第四,普通应用程序和 LKM 处理错误的方式不同。当应用程序中出现错误时并不会给系统造成很大的伤害。LKM 则不然,在其中出现的错误对子系统来说通常是致命的,至少对于当前正在运行的进程而言。LKM 中的一个错误常常会导致整个系统崩溃。

[4]

Answer:

Without loadable kernel modules, an operating system would have to include all possible anticipated functionality already compiled directly into the base kernel. Much of that functionality would reside in memory without being used, wasting memory, and would require that users rebuild and reboot the base kernel every time they require new functionality. Most operating systems supporting loadable kernel modules will include modules to support most desired functionality, and dynamically load the modules as needed.

如果没有可加载的内核模块,操作系统将不得不包括所有可能的预期功能,这些功能已经被直接编译到基本内核中。许多功能将停留在内存中而不被使用,浪费了内存,并且需要用户在每次需要新功能时重建和重启基本内核。大多数支持可加载内核模块的操作系统将包括支持大多数所需功能的模块,并在需要时动态地加载这些模块。


注:

翻译:deepl

参考资料:

[1] https://blog.csdn.net/qq_32252957/article/details/84726541

[2] https://www.doc88.com/p-7334220872657.html

[3] https://blog.csdn.net/gengwei80/article/details/2141880/

[4] http://www.elecfans.com/emb/20180908772045.html

[5] Operating System Concepts – 9th Edition 及其答案