Operating System 操作系统 / 作业系统 重要问答笔记(陆续更新中,2024/04/18周四,已更新)

  • 2024/02/28周三,已更新

Q1: What is the purpose of an interrupt in an operating system?
Ans: An interrupt is a signal emitted by a device attached to a computer or from a program within the computer. It requires the operating system (OS) to stop and figure out what to do next. An interrupt temporarily stops or terminates a service or a current process. Interrupts in an operating system serve several important purposes. They allow the operating system to respond to asynchronous events, such as hardware signals or software exceptions, without requiring constant polling. This helps improve system efficiency and responsiveness. Interrupts also enable the operating system to handle multitasking by allowing it to temporarily suspend the execution of one process in favor of another. Additionally, interrupts facilitate communication between the operating system and hardware devices, allowing for efficient data transfer and control. Overall, interrupts play a crucial role in the functioning of an operating system by enabling it to manage and respond to various events and tasks in a timely manner.

问1:操作系统中中断的目的是什么?
答: 中断是由连接到计算机的设备或计算机内的程序发出的信号。它要求操作系统 (OS) 停下来并弄清楚下一步该做什么。中断会暂时停止或终止服务或当前进程。操作系统中的中断有几个重要的用途。它们允许操作系统响应异步事件,例如硬件信号或软件异常,而无需不断轮询。这有助于提高系统效率和响应能力。中断还允许操作系统暂时挂起一个进程的执行以支持另一个进程,从而使操作系统能够处理多任务。此外,中断促进操作系统和硬件设备之间的通信,从而实现高效的数据传输和控制。总的来说,中断在操作系统的功能中起着至关重要的作用,它使操作系统能够及时管理和响应各种事件和任务。

Q2:How does an interrupt differ from a trap?
Ans: There are two kinds of events: traps and interrupts. The difference between a trap and an interrupt is that a trap is triggered by a user program to invoke OS functionality. Still, an interrupt is triggered by a hardware device to allow the processor to execute the corresponding interrupt handler routine.

问2:中断与陷阱有何不同?
答: 有两种事件:陷阱和中断。陷阱和中断之间的区别在于陷阱是由用户程序触发以调用操作系统功能的。尽管如此,中断还是由硬件设备触发,以允许处理器执行相应的中断处理程序例程。

Q3: Can traps be generated intentionally by a user program?if so,for what purpose?
Ans: A trap can be generated intentionally by a user program. It can be used to call operating system routines or to catch arithmetic errors.

问3:用户程序是否可以故意生成陷阱?如果是,其目的是什么?
答: 用户程序可以有意生成陷阱。它可用于调用操作系统例程或捕获算术错误。

Q4: Some computer systems do not provide a privileged mode of operation in hardware. Is it possible to construct a secure operating system for these computer systems? Give arguments both that it is and that it is not possible.
Ans: By a secure operating system, we mean that a user program is not able to corrupt the kernel, prevent it from running, crash the system, violate memory protection, etc.

There are several possible approaches for implementing a secure operating system. One is to write a emulator for a dual-mode processor (one with a privileged mode) and to run the operating system on top of this emulator. A slight variant of this is to run user code only in an emulator; this is the approach you use in Nachos.

Another is to rely upon safe languages. Only executing user programs written in a safe language such as Java will work, since the language properties guarantee that arbitrary writes to memory aren’t allowed and the instruction set is limited. Note here that the user doesn’t get to choose arbitrary machine code to run—the code run is the result of just-in-time compiling the Java code. Similar to this is only running code which has been produced by a trusted compiler which is known to check memory addresses and not output privileged instructions.

It is not enough to have the kernel scan the user code for bad instructions before executing it, if the code may have been produced by an arbitrary compiler. First, bad instructions may not be easy to identify—for example, they might be a write instruction which happens to overwrite critical data, but which is not easily identified as such. Directly executing user code is also dangerous because the user code may do something indirect such as overwriting its code or writing out new code to a new page of memory and them jumping to it, and this code would then not be checked for security.

Similarly, asking the processor to check with the OS before executing each instruction doesn’t quite work. I don’t know of a processor which has a mode quite like this. Even if one did, that would essentially amount to having a mode bit—there has to be some way for the processor to know to check user code but not the operating system code itself. To make this work, you can use a full emulator (as described earlier).

问4:一些计算机系统不提供硬件的特权操作模式。是否有可能为这些计算机构建一个安全的操作系统?给出可行和不可能的论据。
答: 安全操作系统是指用户程序无法破坏内核、阻止其运行、使系统崩溃、违反内存保护等。

有几种可能的方法来实现安全操作系统。一种是为双模式处理器(一种具有特权模式)编写一个模拟器,并在该模拟器之上运行操作系统。一个细微的变化是仅在模拟器中运行用户代码;这是您在玉米片中使用的方法。

另一个是依赖安全语言。只有执行用 Java 等安全语言编写的用户程序才能工作,因为语言属性保证不允许对内存进行任意写入,并且指令集是有限的。这里请注意,用户无法选择要运行的任意机器代码 - 代码运行是即时编译 Java 代码的结果。与此类似的是,仅运行由可信编译器生成的代码,已知该编译器会检查内存地址而不输出特权指令。

如果代码可能是由任意编译器生成的,那么在执行用户代码之前让内核扫描用户代码是否有错误指令是不够的。首先,坏指令可能不容易识别,例如,它们可能是碰巧覆盖关键数据的写指令,但不容易识别。直接执行用户代码也很危险,因为用户代码可能会做一些间接的事情,例如覆盖其代码或将新代码写入新的内存页面并跳转到该页面,然后不会检查该代码的安全性。

同样,要求处理器在执行每条指令之前检查操作系统也不太有效。我不知道有哪个处理器具有这样的模式。即使这样做,本质上也相当于拥有一个模式位——处理器必须有某种方式知道检查用户代码而不是操作系统代码本身。要实现此目的,您可以使用完整的模拟器(如前所述)。

  • 2024/02/29周四,已更新

Q5: How do hardware and OS work together to handle interrupts? When an interrupt happens, what tasks are handled by the hardware and what are handled by the OS?
Ans: When an interrupt happens (e.g., a network device receives a packet), The CPU stops current operation, switches to the kernel mode, saves machine state on the kernel stack. The CPU then reads address from interrupt table indexed by interrupt number, jumps to the address of the interrupt handle. The OS handles the interrupt (by running the interrupt handler function). Upon completion, CPU restores saved state from stack and returns to user mode. The application that got interrupted (when the interrupt happened) continues from where it stopped.

问5:硬件和操作系统如何协同处理中断?当中断发生时,哪些任务由硬件处理,哪些任务由操作系统处理?
答: 当中断发生时(例如,网络设备接收到封包),CPU停止目前操作,切换到内核模式,将机器状态保存在核心堆栈上。然后CPU从中断表中读取中断号索引的地址,跳到中断句柄的地址。操作系统处理中断(透过执行中断处理函数)。完成后,CPU 从堆栈中恢复已储存的状态并返回用户模式。被中断的应用程序(中断发生时)从停止的地方继续。

Q6: Which of the following instructions should be privileged? (Also give a one-sentence explanation for why)
a) Set value of timer
b) Read the clock
c) Clear memory
d) Turn off interrupts
e) Switch from user to monitor mode

Ans:
a) Set value of timer: Yes, otherwise the user program can manipulate it such that the OS never gains control.
b) Read the Clock: No, as a user can’t really do anything harmful by simply reading the clock.
c) Clear Memory: Yes, since a user program shouldn’t be able to clear arbitrary memory.
(Exception: No, if interpreted as simply clearing memory belonging to the process.)
d) Turn off interrupts: Yes, same reasoning as for setting the value of the timer.
e) Switch from user to monitor mode: Yes, since otherwise a user program could simply switch to kernel mode to execute instructions it wouldn’t otherwise be able to, and defeat security.

问6:下列哪一条指令应享有特权?(并用一句话解释原因)
a) 设定定时器的值
b) 读取时钟
c) 清除内存
d) 关闭中断
e) 从用户模式切换到监控模式

答:
a) 定时器的设定值:是,否则用户程序可以操纵它,使操作系统永远无法获得控制权
b) 读取时钟:不,因为使用者不能透过简单地读取时钟来真正做任何有害的事情。
c) 清除内存:是,因为用户程序不应该能够清除任意内存。
例外:否,如果解释为简单地清除属于该进程的内存。)
d) 关闭中断:是,与设定定时器值的道理相同。
e) 从使用者模式切换到监视模式:是,因为否则用户程序可以简单地切换到内核模式来执行它本来无法执行的指令,并破坏安全性。

  • 2024/04/17周三,已更新

Q7: Please describe five performance criteria to compare CPUscheduling algorithms and express the aim of performance criteria is to maximize or minimize respectively.

Ans:
Maximize: CPU Utilization、Throughput
Minimize: Turnaround Time、Waiting Time、Response Time

问7:请描述五个效能标准来比较CPU调度算法,并分别表达效能标准的目标是最大化还是最小化。

答:
最大化: CPU利用率、吞吐量
最小化: 週轉時間、等待時間、反應時間

Q8: Please briefly describe the Earliest Deadline First (EDF) and the Least Slack Time First (LSTF) scheduling algorithms? When there is not enough time for everything, what is the possible phenomenon for the two scheduling?

Ans:
EDF: The process with earliest deadline would be run first, and preemption happens when a process with earlier deadline arrives.
LSTF: The process with the smallest slack (deadline – remaining computation) would be run first, and preemption happens when a process with smaller slack time arrives.

问8:请简单描述一下最早截止时间优先(EDF)和最小松弛时间优先(LSTF)调度算法? 当没有足够时间时,两种调度各自会出现什么现象?

答:
最早截止时间优先(EDF): 截止日期最早的进程将首先运行,当截止日期更早的进程到达时,会发生抢占。
最小松弛时间优先(LSTF): 具有最小松弛时间(截止时间 - 剩余计算)的进程将首先运行,且当具有较小松弛时间的进程到达时,会发生抢占。

Q9: Please describe when priority inversion will occurs? How to solve it?

Ans:
(a) It can occur in a multi-tasking environment when a higher-priority task is waiting for a resource (such as a lock or a semaphore) that is currently held by a lower-priority task.
(b) Priority Inheritance may be used to avoid priority inversion. It increases the priority of a thread that is holding a lock to the maximum priority of all waiting threads for the associated lock. When the lock is released, the priority goes back to its normal level.

问9:请描述什么时候会发生优先级反转? 如何解决?

答:
(a) 优先级反转: 在多任务环境中,当较高优先级任务正在等待当前由较低优先级任务持有的资源(例如锁或信号量)时,可能会发生这种情况。
(b) 如何解决: 优先级继承可用于避免优先级反转。它将持有锁的线程的优先级(暂时)增加到所有等待关联锁的线程的最大优先级。 当锁被释放时,其优先级需恢复到正常水平。

Q10: Please briefly describe the Direct Memory Access (DMA)?

Ans: Direct Memory Access (DMA) is a technique used by computers to transfer data between devices without involving the CPU. DMA allows devices like hard drives, sound cards, and network cards to read or write data directly to or from the main memory, without the need for the CPU to intervene in every transfer, which would waste a significant amount of CPU time. DMA controllers can access the memory independently of the CPU and transfer large amounts of data at high speed, thereby reducing CPU usage and improving system performance.

  • 2024/04/18周四,已更新

问10:请简单描述一下直接内存访问?

答: 直接内存访问 (DMA) 是计算机用来在设备之间传输数据而无需 CPU参与的技术。DMA允许硬盘、声卡和网卡等设备直接从主内存读取或写入数据,而不需要CPU干预每次传输,这会浪费大量的CPU时间。DMA控制器可以独立于CPU访问内存并高速传输大量数据,从而降低CPU使用率并提高系统性能。

  • 2024/00/00周x,未更新
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没意思不好玩我不玩了

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值