Framist's Little House

◇ 自顶而下 - 面向未来 ◇

0%

【操作系统概念 - 作业 1】Introduction

Operating System Concepts Exercises 1

Introduction

操作系统作业 1

  • 1.1, 1.5, 1.6
  • 1.12, 1.15, 1.19, 1.22, 1.23, 1.27

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

Practice Exercises

1.1 What are the three main purposes of an operating system?

操作系统的三个主要目的是什么?

答:

  1. 作为系统资源的管理者
  2. 向上层提供方便易用的服务
  3. 作为最接近硬件的层次

参考自其他中文教材

The three main purposes are:

  1. To provide an environment for a computer user to execute programs on computer hardware in a convenient and efficient manner.为计算机用户提供一个以方便和高效的方式在计算机硬件上执行程序的环境

  2. To allocate the separate resources of the computer as needed to solve the problem given. The allocation process should be as fair and efficient as possible.根据需要分配计算机的单独资源来解决给出的问题。分配过程应是尽可能的公平和有效的。

  3. As a control program, it serves two major functions: (1) supervision of the execution of user programs to prevent errors and improper use of the computer, and (2) management of the operation and control of I/O devices.作为一个控制程序,它主要提供两个功能:(1)监督用户程序的执行,以防止错误和不当使用计算机,以及对 I/O 设备的操作和控制的(2)管理输入输出设备的运行和控制的管理

1.5 How does the distinction between kernel mode and user mode function as a rudimentary form of protection (security) system?

内核模式和用户模式作为一种初级的保护(安全)系统,其区别是什么?

答:

我们普通程序 员写的程序就是“应用程序”。微软、苹果有一帮人负责实现操作系统,他们写的是“内核程序”。由很多内 核程序组成了“操作系统内核”,或简称“内核 (Kernel)”。内核是操作系统最重要最核心的部分,也是最接 近硬件的部分。甚至可以说,一个操作系统只要有内核就够了

Answer:

Certain instructions (privileged instructions) could be executed only when the CPU is in kernel mode.

If any attempt is made to execute a Privileged Instruction in User Mode, then it will not be executed and treated as an illegal instruction. The Hardware traps it to the Operating System.

只有当 CPU 处于内核模式时,才能执行某些指令(特权指令)。

如果尝试在用户模式下执行特权指令,则不会执行并视为非法指令。硬件将其陷阱到操作系统。

Various examples of Privileged Instructions include:

  • l I/O instructions and Halt instructions
  • l Turn off all Interrupts
  • l Set the Timer
  • l Context Switching
  • ……

Various examples of Non-Privileged Instructions include:

  • l Reading the status of Processor
  • l Reading the System Time
  • l Generate any Trap Instruction
  • ……

Ref:

https://www.geeksforgeeks.org/privileged-and-non-privileged-instructions-in-operating-system/

1.6 Which of the following instructions should be privileged?

下面哪个指令应该被赋予特权?

答:

  • a. Set value of timer.
  • b. Read the clock.
  • c. Clear memory.
  • d. Issue a trap instruction.
  • e. Turn off interrupts.
  • f. Modify entries in device-status table.
  • g. Switch from user to kernel mode.
  • h. Access I/O device.

Set value of timer, clear memory, turn off interrupts, modify entries in device-status table, Switch from user to kernel mode, access I/O device.

The rest can be performed in user mode.

Exercises

1.12 In a multiprogramming and time-sharing environment, several users share the system simultaneously. This situation can result in various security problems.

在多道程序设计和分时环境中,几个用户同时共享系统。这种情况会导致各种安全问题。

a. What are two such problems?

这两个问题是什么?

答:

:Stealing or copying one’s programs or data; using system resources (CPU, memory, disk space, peripherals) without proper accounting.

b. Can we ensure the same degree of security in a time-shared machine as in a dedicated machine? Explain your answer.

我们能否确保分时机器与专用机器具有相同程度的安全性?解释你的答案

答:

:Probably not, since any protection scheme devised by humans can inevitably be broken by a human, and the more complex the scheme, the more difficult it is to feel confident of its correct implementation.

Answer:

a. Stealing or copying a user’s files; 窃取或复制用户的文件

writing over another program’s (belonging to another user or to the OS) area in memory; 写入内存中的另一个程序(属于其他用户或操作系统)的区域

using system resources (CPU, disk space) without proper accounting;

causing the printer to mix output by sending data while some other user’s file is printing.不使用系统资源(CPU、磁盘空间);

在其他用户的文件正在打印时,通过发送数据,使打印机混合输出。

b. Probably not, since any protection scheme for time-sharing environment devised by a human can also be broken (可能不会,因为任何由人类设计的分时环境保护方案都可能被破坏)– and the more complex the scheme is, the more difficult it is to be confident of its correct implementation.(该方案越复杂,就越难对其正确实施有信心。)

1.15 Describe the differences between symmetric and asymmetric multiprocessing. What are three advantages and one disadvantage of multiprocessor systems?

描述对称和非对称多处理之间的区别。多处理器系统的三个优点和一个缺点是什么?

答:

three main advantages:

Multiprocessor systems have three main advantages:

  1. Increased throughput. By increasing the number of processors, we expect to get more work done in less time. The speed-up ratio with N processors is not N, however; rather, it is less than N. When multiple processors cooperate on a task, a certain amount of overhead is incurred in keeping all the parts working correctly. This overhead, plus contention for shared resources, lowers the expected gain from additional processors. Similarly, N programmers working closely together do not produce N times the amount of work a single programmer would produce.
  2. Economy of scale. Multiprocessor systems can cost less than equivalent multiple single-processor systems, because they can share peripherals, mass storage, and power supplies. If several programs operate on the same set of data, it is cheaper to store those data on one disk and to have all the processors share them than to have many computers with local disks and many copies of the data.
  3. Increased reliability. If functions can be distributed properly among several processors, then the failure of one processor will not halt the system, only slow it down. If we have ten processors and one fails, then each of the remaining nine processors can pick up a share of the work of the failed processor. Thus, the entire system runs only 10 percent slower, rather than failing altogether.

one disadvantage:

需考虑体系结构,如缓存,内存及总线竞争

Answer

Symmetric processing treats all processors as equals; I/O can be processed on any of them. Process can be scheduled on any of them.对称处理将所有处理器平等处理,其中任何一个都可以处理 I/O。进程可以在其中的任何一个进程上进行安排。

Asymmetric processing designates one CPU as the master, which is the only one capable of performing I/O; the master distributes computational work among the other CPUs (worker).不对称处理指定一个 CPU 作为主计算机,这是唯一能够执行 I/O 的主计算机;主计算机将在其他 CPU 之间分配计算工作。

Advantages:

Multiprocessor systems can save money, by sharing power supplies and peripherals. 多处理器系统可以通过共享电源和外设来省钱。

Can execute programs more quickly (parallel) 可以更快地执行程序(并行)

can have increased reliability.可以提高可靠性。

Disadvantages:

Multiprocessor systems are more complex in both hardware and software. Additional CPU cycles are required to manage the cooperation, so per-CPU efficiency goes down.多处理器系统在硬件和软件上都更加复杂。需要额外的 CPU 周期来管理协作,因此每个 CPU 的效率会下降 100

1.19 What is the purpose of interrupts? How does an interrupt differ from a trap? Can traps be generated intentionally by a user program? If so, for what purpose?

中断的目的是什么?中断与陷阱有何不同?陷阱可以由用户程序故意产生吗?如果有,是为了什么目的?

答:

An interrupt is a hardware-generated change of flow within the system. An interrupt handler is summoned to deal with the cause of the interrupt; control is then returned to the interrupted context and instruction. A trap is a software-generated interrupt.
An interrupt can be used to signal the completion of an I/O to obviate the need for device polling. A trap can be used to call operating system routines or to catch arithmetic errors
中断是系统中由硬件产生的流变化。调用中断处理程序来解决造成中断的原因;控制返回到
中断的上下文和指令处。陷阱是由软件产生的中断,中断可以用来发出 I/O 完成的信号,以消除
对设备轮询。陷阱可以用来调用操作系统例程或捕获算数错误。

Answer:

An interrupt is a hardware-generated signal that changes the flow within the system. An interrupt can be used to signal the completion of I/O so that the CPU doesn’t have to spend cycles polling the device. 中断是一种硬件生成的信号,可以改变系统内部的流量。一个中断可以用来发出 I/O 的完成信号,这样 CPU 就不必花费一个周期来轮询设备了。

A trap is a software-generated interrupt. A trap can be used to invoke call system calls.(陷阱是一种由软件生成的中断。一个陷阱可以用于调用呼叫系统调用。)

Exception is raised when an abnormal condition occurs during execution. (For example, Memory protection violation, page fault, illegal instruction) 在执行过程中出现异常情况时,会引发异常。(例如,内存保护违规、页面故障、非法指令)

1.22 Many SMP systems have different levels of caches; one level is local to each processing core, and another level is shared among all processing cores. Why are caching systems designed this way?

许多 SMP 系统有不同级别的缓存;一个级别是每个处理核心的本地级别,另一个级别是所有处理核心之间共享的。为什么缓存系统是这样设计的?

答:

平衡各方优势

If the CPU has an integrated memory controller, then adding CPUs can also increase the amount of memory addressable in the system. Either way, multiprocessing can cause a system to change its memory access model from uniform memory access (UMA) to non-uniform memory access (NUMA). UMA is defined as the situation in which access to any RAM from any CPU takes the same amount of time. With NUMA, some parts of memory may take longer to access than other parts, creating a performance penalty. Operating systems can minimize the NUMA penalty through resource management, as discussed in Section 9.5.4.

如果 CPU 有一个集成的内存控制器,那么增加 CPU 也可以增加系统中可寻址的内存数量。无论哪种方式,多处理都会导致系统改变其内存访问模式,从统一内存访问(UMA)变为非统一内存访问(NUMA)。UMA 被定义为从任何 CPU 访问任何 RAM 都需要相同的时间。在 NUMA 的情况下,内存的某些部分可能需要比其他部分更长的时间来访问,从而造成性能损失。如第 9.5.4 节所述,操作系统可以通过资源管理将 NUMA 惩罚降到最低。

Answer:

The different levels are based on access speed as well as size. In general, the closer the cache is to the CPU, the faster the access. However, faster caches are typically more costly. Therefore, smaller and faster caches are placed local to each CPU, and shared caches that are larger, yet slower, are shared among several different processors.不同的级别是基于访问速度和大小的。一般来说,缓存越接近 CPU,访问速度就越快。然而,更快的缓存通常会更昂贵。因此,较小、更快的缓存被放在每个 CPU 的本地,而更大但较慢的共享缓存则在几个不同的处理器之间共享。

1.23 Consider an SMP system similar to the one shown in Figure 1.6. Illustrate with an example how data residing in memory could in fact have a different value in each of the local caches.

考虑一个类似于图 1.6 所示的 SMP 系统。用示例说明内存中的数据在每个本地缓存中具有不同的值。

image-20210318111753265

答:

Say processor 1 reads data A with value 5 from main memory into its local cache.
Similarly, processor 2 reads data A into its local cache as well. Processor 1 then
updates A to 10. However, since A resides in processor 1’s local cache, the update only
occurs there and not in the local cache for processor 2.
处理器 1 从主内存读取 A 的值是 5,保存到本地缓存中。相似的,处理器 2 从主内存也读取 A
的值。处理器 1 然后将 A 更改成 10.然而,因为 A 在处理器 1 的本地缓存中,更新只在这里而不再
处理器 2 的本地缓存。(这里实际上有两个问题,一个是处理器 1 更新时再自己本地缓存更新,
一个是处理器 2 的值就是旧值,得不到最新的 A 值。大家可以阅读缓存一致性的文章,但是还不
保证变量的可见性,详见 Java volatile)

1.27 Describe some of the challenges of designing operating systems for mobile devices compared with designing operating systems for traditional PCs.

请描述为移动设备设计操作系统与为传统 pc 设计操作系统的一些挑战。

答:

  • 低功耗
  • 易于使用,用户友好,学习简单
  • 适于触摸语音等交互方式
  • 反应快。待机和使用状态转变快
  • 适应长期驻于后台的多功能设计
  • 安全性高

Answer:

The greatest challenges in designing mobile operating systems include:

  • l Less storage capacity means the operating system must manage memory carefully.(存储容量的减少意味着操作系统必须小心地管理内存。)
  • l The operating system must also manage power consumption carefully.(操作系统还必须仔细管理功耗。电池)
  • l Less processing power plus fewer processors mean the operating system must carefully apportion processors to applications.(更少的处理能力加上更少的处理器意味着操作系统必须小心地将处理程序分配到应用程序。)

注:

翻译:deepl

参考资料:

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