## Chapter 1: Introduction 1. Which of the following is an advantage of DMA (Direct Memory Access)? ( ) A. It requires the CPU to be constantly involved in data transfer, ensuring data accuracy. B. It reduces the number of interrupts generated during data transfer. C. It can only transfer data in small chunks, which is more flexible. D. It increases the load on the CPU during data transfer. - 正确:B;错选:D - **解析**:DMA的优点是设备控制器可直接将数据块传输到主内存,无需CPU频繁干预,减少了中断次数,提高了数据传输速度,选项B正确。它不需要CPU一直参与数据传输,A错误;可以传输数据块,并非只能小数据块传输,C错误;减轻了CPU在数据传输时的负载,D错误。 2. **Based on page 13, I/O devices can work concurrently with the CPU mainly because ( )** A. of continuous CPU intervention B. the device controller can operate independently to some extent C. other processes stop during I/O D. data transfer relies solely on the memory - Answer**: B ** - Explanation: The device controller's ability to operate with some independence allows I/O devices to work concurrently with the CPU, as per the content on page 13. ### 针对第一题 1. 以下关于软件中断的描述,正确的是?( ) A. 软件中断只能由错误引起 B. 软件中断只能由用户请求(系统调用)引起 C. 软件中断由错误或用户请求(系统调用)引起 D. 软件中断与硬件中断没有区别] - 答案:C - 知识点:Software: A trapor exceptionis a software-generated interrupt caused either by an error or a user request (system call) 2. 中断驱动的I/O循环中,控制返回到用户程序的方式有几种?( ) A. 1种 B. 2种 C. 3种 D. 4种 - 答案:B - 知识点:课件中提到“Interrupt - Driven I/O Cycle: Two Methods”,明确表明中断驱动的I/O循环有两种控制返回用户程序的方式 。一种是“**After I/O starts, control returns to user program only upon I/O completion**”,即I/O完成后控制返回用户程序;另一种是“**After I/O starts, control returns to user program without waiting for I/O completion**”,也就是I/O开始后不等待I/O完成控制就返回用户程序 。 ### 针对第二题 补充知识点:Cache design consideration - size - content replacement policy ### 针对第三题 1. 在双重模式(Dual - mode)系统中,若内核模式下的操作出现错误,可能会( ) - **答案**:A. 导致整个系统崩溃 错选:B - **解析**: - 内核在操作系统中处于核心地位,负责管理系统的各种关键资源,如硬件设备、内存、进程等。内核模式下的操作出现错误,很可能影响到系统关键功能的正常运行。 - 例如,如果内核在管理内存时出现错误,可能导致内存分配混乱,使得其他依赖正确内存管理的进程无法正常工作;若内核在处理硬件中断时出错,可能导致硬件设备无法正常响应,进而影响整个系统的运行。由于内核掌控着整个系统的运行,所以一旦出错,极有可能导致整个系统崩溃。 - **B选项“仅影响当前运行的用户程序”不符合实际情况,因为内核错误影响的范围远不止当前用户程序。** - C选项“自动切换回用户模式继续运行”,内核错误往往比较严重,不是简单切换到用户模式就能继续正常运行的,因为错误可能已经破坏了系统的关键状态和资源。 - D选项“忽略错误并继续执行”,内核级别的错误通常不能被轻易忽略,否则可能引发一系列更严重的问题,甚至导致系统不稳定或无法使用 2. 以下关于用户模式和内核模式下程序执行的描述,错误的是( ) - **答案**:D. 内核模式下程序不能执行普通的算术运算指令 错选:B - **解析**: - A选项“用户模式下程序不能直接访问硬件寄存器”,这是正确的。为了保护系统的安全性和稳定性,在用户模式下,程序的权限受到限制,不能直接访问硬件寄存器,因为直接访问可能会导致硬件状态混乱或系统不稳定。 - B选项“内核模式下程序可以访问所有内存空间”,内核需要管理整个系统的内存资源,为了实现内存分配、回收等功能,内核模式下的程序必须能够访问所有内存空间,所以该选项正确。 - C选项“用户模式下程序执行的指令集是受限的”,这是为了防止用户程序执行可能破坏系统的指令,例如特权指令只能在内核模式下执行,所以用户模式下程序执行的指令集是受限的,该选项正确。 - D选项“内核模式下程序不能执行普通的算术运算指令”,内核模式下程序需要进行各种复杂的操作,包括数据处理、计算等,必然可以执行普通的算术运算指令,所以该选项描述错误。 ## Chapter 2: OS Structure 1. **Question**: Which of the following is a disadvantage of the module structure? A. Lack of flexibility B. Difficulty in customizing the system C. Compatibility issues in module interfaces D. High performance degradation - **Answer**: C. Compatibility issues in module interfaces 错选 D - **Explanation**: In a module - structured operating system, different modules need to interact with each other. Since these modules can be developed independently to some extent, ensuring compatibility between their interfaces can be a significant challenge. For example, a new version of one module might expect a different format of data from another module it interacts with, leading to errors. Option A is incorrect because the module structure is known for its flexibility as modules can be added or removed. Option B is wrong as the ability to customize the system by adding or removing relevant modules is one of its advantages. Option D is also incorrect as the module structure doesn't inherently cause high performance degradation; in fact, it can improve performance in some cases by only loading necessary modules. 2. **Question**: A disadvantage of the monolithic structure is: A disadvantage of the monolithic structure is: A. Low efficiency in resource management B. High code maintainability C. Difficulty in adding new functions without major kernel changes D. Lack of direct interaction between kernel parts - **Answer**: C. Difficulty in adding new functions without major kernel changes 错选B - **Explanation**: In a monolithic structure, all kernel functions are at the same level and intertwined. When adding a new function, it often requires significant changes to the existing kernel code because of the lack of clear modular boundaries. Since the entire kernel is a large, non - layered entity, new code may need to be integrated into multiple existing parts of the kernel. Option A is incorrect as monolithic structures can be highly efficient in resource management due to direct interactions between kernel components. **Option B is wrong because high code maintainability is not a characteristic of monolithic structures;** rather, the intertwined nature of the code makes maintenance difficult. Option D is incorrect as monolithic structures have high levels of direct interaction between kernel parts. - The lack of good module division in a monolithic operating - system structure directly contributes to poor code maintainability, which is a significant disadvantage. When new functions need to be added, developers face challenges due to the highly intertwined nature of the code. Since there are no distinct, well - defined modules with clear interfaces, changes in one part of the code can have unforeseen impacts on other parts of the kernel. This makes it difficult to isolate the effects of new code implementation and increases the risk of introducing bugs throughout the system. So, your understanding about this aspect of the monolithic structure is spot - on. It further emphasizes why option C (Difficulty in adding new functions without major kernel changes) is the correct answer for question 18. ### 针对第四题 1. 传统UNIX操作系统由哪两个可分离部分组成? A. 应用程序和系统程序 B. 系统程序和内核 C. 内核和硬件 D. 硬件和应用程序 - 答案:B;错选:A - **知识点:** The UNIX OS consists of two separable parts: **Systems program / The kernel** 2. 传统UNIX操作系统中,系统程序的作用不包括? A. 提供程序开发环境 B. 执行用户应用程序 C. 实现内核功能 D. 提供程序执行环境 - 答案:C;错选:A - 错因:看错题 ## Chapter 3: Process ### Multiple Choices 1. Question 3: Which of the following statements about the Process Control Block (PCB) is incorrect? A. Each process has a corresponding unique PCB. B. The PCB is a data structure accessible to user processes. C. The PCB is stored in the memory reserved for the kernel. D. When a process terminates, the kernel deletes its PCB. - Answer: B; 错选:C - PCB是内核数据结构。它存储在内核保留的内存中,进程本身不可见。用户进程无法直接访问PCB。只有内核可以访问和修改PCB。 - PCB存储在内核保留的内存中,只能由内核访问和修改。 2. Question 14: In the inter - process communication model of message passing, which ( ) operations are controlled by the kernel? A. Sending messages (send) and receiving messages (receive) B. Allocation of shared memory C. Process creation D. Process termination. - Answer: A;错选:B - A:在消息传递进程间通信模型中,发送消息(send)和接收消息(receive)操作由内核控制。内核负责确保消息在进程之间正确传递,处理诸如消息缓冲、同步和安全等问题。 - B:共享内存的分配与共享内存进程间通信模型相关,而不是消息传递模型。在消息传递中,进程之间不存在与共享内存模型相同意义上的直接内存共享。所以,该选项错误。 ### 针对第五题 1. 上下文切换时,系统必须保存的旧进程关键信息不包括以下哪一项( )A. 程序计数器(Program Counter, PC)的值 B. 进程的优先级 C. 堆(heap)中动态分配的内存数据 D. CPU寄存器(CPU registers)的值 - **答案**:C. 堆(heap)中动态分配的内存数据;错选:B - 解析:上下文切换时需要保存程序计数器(PC)的值,以便知道旧进程下次从何处继续执行;保存CPU寄存器的值,保证进程恢复执行时数据状态一致;进程优先级属于进程调度信息,也需保存以便调度算法使用。而堆中动态分配的内存数据,其内存空间在进程切换时依然存在,只要进程未结束,不需要在上下文切换时专门保存这部分数据。 2. 某操作系统在上下文切换时,需要保存和恢复大量的CPU寄存器状态,这表明该操作系统( ) A. 上下文切换效率较高 B. 对硬件资源的管理较为简单 C. 上下文切换时间可能较长 D. 不支持多进程并发执行 - **答案**:C. 上下文切换时间可能较长;错选:B - **解析**:保存和恢复大量CPU寄存器状态需要花费时间,这会增加上下文切换的时间开销,所以上下文切换时间可能较长。A选项保存恢复大量寄存器状态会降低效率,不是效率高;**B选项说明对硬件资源管理复杂,而非简单;**D选项多进程并发执行与保存寄存器状态多少并无直接关联,且一般操作系统都支持多进程并发 ### 针对第六题 1. 命名管道(Named pipes)与普通管道相比,以下哪项是命名管道特有的优势( ) A. 支持生产者 - 消费者模式通信 B. 可用于进程间双向通信 C. 只能在同一台计算机上使用 D. 通信时需要创建文件描述符 - **答案**:B. 可用于进程间双向通信;错选A - 普通管道同样支持生产者 - 消费者模式通信,B选项双向通信是命名管道优势;C选项两者都只能在同一台计算机上使用;D选项两者通信都需要创建文件描述符。命名管道特有的优势在于无需父子关系即可通信,答案A有误,应选B(双向通信)。 2. 命名管道可以被多个进程同时访问,当多个进程同时向命名管道写入数据时,可能会出现什么问题( ) A. 数据会自动按顺序写入 B. 数据可能会相互覆盖 C. 操作系统会阻止多个进程同时写入 D. 会创建多个管道副本分别写入 - 答案:B - **解析**:**命名管道没有内置机制确保多个进程写入数据的顺序和完整性**,多个进程同时写入可能导致数据相互覆盖,操作系统不会自动阻止,所以B正确。 ### 针对第九题 1. **题目**:以下哪一项不属于进程控制块(PCB)所包含的信息( ) A. 进程的优先级(Priority) B. 进程所执行程序的源代码 C. 程序计数器(Program counter)的位置 D. 已分配给进程的I/O设备信息 - **选项A**:进程的优先级(Priority)是CPU调度信息的一部分,PCB中会包含进程优先级,用于决定进程在调度队列中的顺序,所以A选项属于PCB包含的信息。 - **选项B**:进程控制块主要包含与进程运行控制和资源管理相关的信息,并不包含进程所执行程序的源代码。**程序代码通常存储在内存的代码段(text section),所以B选项不属于PCB包含的信息,该题应选B。** - **选项C**:程序计数器(Program counter)的位置对于记录进程执行位置至关重要,它指示了下一条要执行的指令的位置,是PCB的重要组成部分,所以C选项属于PCB包含的信息。 - **选项D**:已分配给进程的I/O设备信息属于I/O状态信息,PCB需要记录这些信息以便操作系统管理进程的I/O操作,所以D选项属于PCB包含的信息。 2. **题目**:当一个新进程被创建时,操作系统会为其执行以下哪个操作与进程控制块(PCB)相关( ) A. 从磁盘读取PCB模板并初始化 B. 在用户空间创建一个PCB C. 在内核中创建一个新的PCB D. 复制父进程的PCB并修改部分信息 - **选项A**:PCB是内核数据结构,并非从磁盘读取模板并初始化,所以A选项错误。 - **选项B**:PCB存储在内核保留的内存空间,不是在用户空间创建,所以B选项错误。 - **选项C:课件明确提到“When a new process is created, the kernel creates a new PCB for it”,即当新进程创建时,内核会为其在内核中创建一个新的PCB,所以C选项正确。** - **选项D**:新进程创建时,是内核新创建PCB,不是复制父进程的PCB并修改部分信息,**虽然进程创建可能涉及复制父进程某些资源,但PCB是新创建的,所以D选项错误。** 3. **题目**:进程控制块(PCB)中的程序计数器(Program counter)主要用于( ) A. 记录进程已执行的指令数量 B. 指示下一条要执行的指令的位置 C. 存储正在执行的指令 D. 统计进程执行所花费的时间 - **选项A**:程序计数器不是用于记录进程已执行的指令数量,所以A选项错误。 - **选项B**:**程序计数器的主要作用是指示下一条要执行的指令的位置,这样CPU才能按照正确的顺序执行程序指令,所以B选项正确。** - **选项C**:程序计数器并不存储正在执行的指令,指令通常存储在内存的代码段,由CPU从内存中读取,所以C选项错误。 - **选项D**:程序计数器与统计进程执行所花费的时间无关,所以D选项错误。 4. **题目**:假设系统中有两个进程A和B,进程A正在运行,此时进程B被调度到CPU上执行,这个过程涉及到上下文切换。在上下文切换过程中,关于进程A的PCB,以下说法正确的是( ) A. PCB中的信息不会发生改变 B. 仅程序计数器(Program counter)和CPU寄存器的值会被保存到PCB C. PCB中的所有信息会被保存,以便进程A下次能继续正确执行 D. PCB会被暂时删除,当进程A再次被调度时重新创建 - **选项A**:上下文切换时,为了能让进程A下次继续正确执行,PCB中的信息需要被保存和恢复,并非不会发生改变,所以A选项错误。 - **选项B**:上下文切换不仅保存程序计数器(Program counter)和CPU寄存器的值,还包括其他与进程相关的重要信息,如进程状态等,所以B选项错误。 - **选项C**:在上下文切换时,**为了保证进程A后续能继续正确执行,PCB中的所有信息会被保存,当进程A再次被调度时,依据保存的PCB信息恢复其运行状态,所以C选项正确。** - **选项D**:上下文切换时,进程A的PCB不会被删除,只是其状态等信息被保存,以便后续恢复,所以D选项错误。 ### 针对第十题 B,D,B,A,D ,D,A,B,A,A 1/2/5/6/9 知识点: `fork() `system call creates new process, create a duplicate of the parent - `exec()`system call used after a fork()to replace the process memory space with a new program - Parent process calls `wait()` for the child to terminate 1. **题目**:在进程创建过程中,父进程(Parent process)和子进程(Child process)共享资源的方式不包括以下哪种( ) A. 父进程和子进程共享所有资源 B. 子进程共享父进程的部分资源 C. 父进程和子进程共享部分代码段,但数据段独立 D. 父进程和子进程不共享任何资源 - 错选:B;错因:看错题 - **选项A**:课件中明确提到“Parent and children share all resources”,即父进程和子进程可以共享所有资源,所以A选项是可能的共享方式。 - **选项B**:“Children share subset of parent’s resources”表明子进程可以共享父进程的部分资源,B选项也是一种共享方式。 - **选项C:课件中只提及了共享所有资源、部分资源以及不共享资源这三种方式,并没有专门指出共享部分代码段但数据段独立这种情况,所以C选项不属于常见的父子进程共享资源方式,该题选C。** - **选项D**:“Parent and child share no resources”说明父进程和子进程可以不共享任何资源,D选项属于共享资源的一种描述方式。 2. **题目**:当父进程创建子进程后,关于父子进程的执行选项,以下说法**错误**的是( ) A. 父进程和子进程可以并发执行(execute concurrently) B. 父进程可以等待子进程终止(wait until children terminate) C. 子进程不能先于父进程执行 D. 父进程和子进程的执行顺序取决于调度算法 - 错选:C;错因:看错题 - **选项A**:“Parent and children execute concurrently”表明父进程和子进程可以并发执行,这是常见的执行方式,所以A选项说法正确。 - **选项B**:“Parent waits until children terminate”说明父进程可以等待子进程终止后再继续执行,B选项说法正确。 - **选项C**:实际上,子进程和父进程的执行顺序取决于调度算法,子进程完全有可能先于父进程执行,所以C选项说法错误,该题选C。 - **选项D**:在多进程系统中,进程的执行顺序是由调度算法决定的,父子进程也不例外,D选项说法正确。 3. **题目**:在UNIX/Linux系统中,fork()系统调用返回后,子进程通常会调用exec()系统调用,其目的是( ) A. 继续执行与父进程相同的代码 B. 替换自身的内存空间,加载一个新的程序 C. 等待父进程结束 D. 将自身的资源归还给父进程 - 错选:D;错因:`exec()` 是执行一个程序,而不是退出 - **选项A**:子进程调用exec()就是为了改变执行的程序,不会继续执行与父进程相同的代码,所以A选项错误。 - **选项B:课件中提到“exec()system call used after a fork()to replace the process’memory space with a new program”,即fork()后子进程调用exec()是为了用一个新程序替换自身的内存空间,加载并执行新的程序,B选项正确。** - **选项C**:等待父进程结束是父进程调用wait()的作用,而不是子进程调用exec()的目的,C选项错误。 - **选项D**:子进程调用exec()与归还自身资源给父进程没有关系,D选项错误。 4. **题目**:假设在一个程序中,父进程调用fork()创建子进程后,子进程又调用了exec()加载新程序。如果exec()调用失败,会发生什么情况( ) A. 子进程立即终止 B. 子进程继续执行父进程的代码 C. 子进程进入等待状态,直到exec()成功 D. 父进程会收到一个错误通 - 错选:D; - **选项A**:**课件中提到“The only exception to this is if the exec system call fails (usually because Program 2 does not exist). In that case the exec system call returns and Process 2 continues executing Program 1.”,说明exec()调用失败时,子进程不会立即终止,而是继续执行原来的程序,即父进程的代码,所以A选项错误。** - **选项B**:如上述课件内容所述,exec()调用失败后,子进程继续执行父进程的代码,B选项正确。 - **选项C**:子进程不会进入等待状态,而是继续执行原程序,C选项错误。 - **选项D**:父进程不会收到关于子进程exec()调用失败的错误通知,只有子进程自身继续执行原程序,D选项错误。 5. **题目**:如果一个父进程创建了多个子进程,并且希望在所有子进程都结束后再继续执行自身的后续代码,它应该怎么做( ) A. 在每个fork()调用后立即调用wait() B. 在所有fork()调用之后,统一调用一次wait() C. 使用一个循环,对每个子进程的PID调用wait() D. 不需要额外操作,父进程会自动等待所有子进程结束 - 错选:A - **选项A**:在每个fork()调用后立即调用wait(),这样只能等待一个子进程结束,而不是所有子进程,当有多个子进程时,这种方式无法满足需求,所以A选项错误。 - **选项B**:在所有fork()调用之后统一调用一次wait(),只能等待一个子进程结束,不能确保等待所有子进程结束,B选项错误。 - **选项C**:**使用一个循环,对每个子进程的PID调用wait(),这样可以逐个等待每个子进程结束,确保所有子进程都结束后,父进程再继续执行后续代码,C选项正确。** - **选项D**:父进程不会自动等待所有子进程结束,需要显式调用wait()函数来等待,D选项错误。 ### Short Essay Problem 1. Please write a short essay addressing the following aspects based on the content on page 39 of chapter 3: 1. **Describe the two models of inter - process communication (IPC)**: - Explain the shared - memory model. Discuss who controls this model and what the major issue is that needs to be addressed when using it. Provide an example of a problem that can be solved using the shared - memory model in the context of cooperating processes. - Explain the message - passing model. Discuss who controls this model and the two main operations involved. Also, mention some of the implementation issues that need to be considered when establishing a communication link in this model. 2. **Compare the two models**: - Highlight the differences between the shared - memory and message - passing models in terms of control, synchronization mechanisms, and performance implications. For example, consider how synchronization is achieved in each model and how it impacts the overall communication efficiency. 3. **Practical considerations**: - In a real - world software development scenario, when might you choose to use the shared - memory model over the message - passing model? And vice versa? Provide some practical examples or scenarios from different application domains (such as operating systems, distributed systems, or mobile applications) to support your answer. **Answer guide**: 1. **Two models of IPC**: - **Shared - memory model**: - **Control**: User processes control this model. They can directly access and modify the shared memory area. - **Major issue**: Synchronization is the major issue. Since multiple processes can access the shared memory simultaneously, proper synchronization mechanisms (e.g., mutexes, semaphores, which will be discussed in chapters 6 & 7) are needed to avoid data races and ensure data consistency. - **Example**: The producer - consumer problem can be solved using the shared - memory model. Producers and consumers can communicate through a shared buffer in the shared memory. The producer writes data into the buffer, and the consumer reads from it. - **Message - passing model**: - **Control**: The kernel controls this model. - **Operations**: The two main operations are send(message) and receive(message). - **Implementation issues**: When establishing a communication link, we need to consider how links are established (physically through shared memory, hardware bus, network; and logically through direct or indirect communication), whether a link can be associated with more than two processes, how many links can exist between pairs of communicating processes, the capacity of a link, whether the message size is fixed or variable, and whether the link is unidirectional or bi - directional. 2. **Comparison**: - **Control**: In the shared - memory model, user processes have more direct control over the communication medium. In the message - passing model, the kernel has more control as it manages the message - passing operations. - **Synchronization mechanisms**: In the shared - memory model, synchronization is a user - implemented concern to prevent concurrent access issues. In the message - passing model, synchronization can be achieved through blocking or non - blocking send and receive operations. For example, a blocking send blocks the sender until the message is received, providing a form of synchronization. - **Performance implications**: Shared - memory can be faster for communication as there is no need for kernel - mediated message passing. However, the complexity of synchronization can lead to performance bottlenecks. Message - passing, on the other hand, has the overhead of kernel intervention but can be more suitable for distributed systems where direct shared memory is not possible. 3. **Practical considerations**: - **Shared - memory model**: - **When to use**: In applications where high - speed data sharing is required within a single system, such as in - memory databases or some high - performance computing applications running on a multi - core system. For example, in a real - time graphics rendering application, different components of the rendering pipeline (e.g., vertex processing and fragment processing) can share data in a shared - memory area to avoid the overhead of frequent message passing. - **Message - passing model**: - **When to use**: In distributed systems where processes are running on different hosts. For example, in a client - server web application, the client and server communicate through message passing. Also, in mobile applications where different app components (e.g., activities and services in Android) may communicate using a form of message passing for better isolation and security. 2. **Question**: Explain the role of the Process Control Block (PCB) in an operating system. - **Answer**: The PCB stores process - related information like process state, program counter, CPU registers, scheduling and memory - management info. Each process has a unique PCB in kernel - reserved memory. The kernel uses it to manage processes, creating a new PCB on process creation and deleting it on termination. 3. **Question**: Compare named pipes and ordinary (anonymous) pipes. - **Answer**: Ordinary pipes are unidirectional, require a parent - child relationship, and can't be accessed outside the creating process. Named pipes are bidirectional, don't need a parent - child relationship, and can be accessed by multiple processes. Both can be used for inter - process communication on the same computer. 4. **Question**: Describe the steps in a Remote Procedure Call (RPC). - **Answer**: First, the client - side stub locates the server, marshals parameters, and sends them to the server - side stub. Then, the server - side stub unpacks parameters, performs the procedure call, marshals the result, and sends it back. Finally, the client - side stub receives and unpacks the result and returns it to the client. ## Chapter 5: CPU Scheduling ### 针对第八题 1. 题目:在多级队列(Multilevel Queue)调度中,若有前台(foreground)和后台(background)两个队列,前台队列采用时间片轮转(Round - Robin, RR)调度算法,后台队列采用先来先服务(First - Come, First - Served, FCFS)调度算法。现有一个前台进程P1和一个后台进程P2同时到达,以下说法正确的是( ) A. P1会先执行,因为前台队列优先级高于后台队列 B. P2会先执行,因为FCFS算法先到先执行 C. P1和P2会交替执行,按照RR算法的时间片分配 D. 系统会根据进程的突发时间(burst time)决定谁先执行 - **答案:A. P1会先执行,因为前台队列优先级高于后台队列** - 解析:在多级队列调度中,前台队列的优先级高于后台队列。即使进程P1(前台)和P2(后台)同时到达,调度器会优先处理高优先级队列中的进程。前台队列采用时间片轮转(RR)调度,因此P1会被立即调度执行。只有当P1完成或主动释放CPU(如时间片用完但未结束,重新排队后仍属于前台队列),且前台队列为空时,后台队列的进程P2才会被调度执行。由于前台队列始终优先,P1会先执行,而非交替执行或依赖突发时间。选项B、C、D均不符合多级队列调度规则。 2. 题目:假设在多级队列调度系统中有三个队列Q1、Q2、Q3,优先级依次降低。Q1采用RR调度算法,时间片为5ms;Q2采用SJF调度算法;Q3采用FCFS调度算法。现有进程P1、P2、P3分别进入Q1、Q2、Q3队列。P1突发时间为10ms,P2突发时间为8ms,P3突发时间为15ms。在这种情况下,哪个进程最有可能先完成( ) A. P1 B. P2 C. P3 D. 无法确定,取决于进程到达时间 - 错选:B;错因:没有理解优先级高的 queue 如果非空要一直执行完,导致只算了一个时间片就跳到 Q2 去了 - 在多级队列调度系统中,队列的优先级决定了执行顺序。Q1(最高优先级)使用RR算法(时间片5ms),Q2使用SJF,Q3使用FCFS。三个进程P1(Q1,突发时间10ms)、P2(Q2,8ms)、P3(Q3,15ms)同时到达各自队列。 1. **Q1的调度过程**: - P1在Q1中执行第一个时间片(5ms),剩余突发时间5ms。 - 由于Q1非空且优先级最高,调度器继续执行Q1中的P1(第二个时间片5ms),P1在10ms时完成。此时Q1为空。 2. **Q2的调度过程**: - Q2采用SJF,且仅有P2(突发时间8ms)。Q1完成后,P2开始执行,耗时8ms,于18ms完成。 3. **Q3的调度过程**: - Q3采用FCFS,且仅有P3(突发时间15ms)。Q2完成后,P3开始执行,耗时15ms,于33ms完成。 **结论**:由于Q1优先级最高且P1的突发时间恰好为两个时间片(5ms×2),P1在10ms时率先完成。Q2和Q3的进程需等待Q1完全空闲后才能执行。因此,**P1最有可能先完成**。 **答案:A. P1** 3. 题目:题目:在多级队列调度中,若采用时间片(Time slice)调度策略,为每个队列分配CPU时间。假设有两个队列A和B,A队列分配到60%的CPU时间,B队列分配到40%的CPU时间。如果系统中只有A队列有进程在等待,且该进程需要100ms的CPU时间才能完成,那么该进程实际完成时间大约是( ) A. 100ms B. 120ms C. 167ms D. 250ms - 其实没有选错,但是错误的计算方式是这样:$0.6t \geq 100ms$ ,总时间 $T_0 = n \times 0.4T + T \geq 167ms$ - 混淆了 - 在多级队列调度中,当队列A和B分别分配60%和40%的CPU时间时,**即使B队列无进程,调度器仍可能严格按比例分配时间**。这意味着: 1. **时间片划分**:系统按比例将CPU时间分配给队列。例如,每单位时间(如100ms)中,A队列占用60ms,B队列占用40ms。若B队列无进程,其40ms时间将被**空闲**。 2. **进程执行时间计算**: - 进程需要100ms的CPU时间,但实际仅在A队列分配的60%时间内运行。 - 实际完成时间满足:**总时间 × 60% = 100ms**,即总时间 ≈ **167ms**(100ms ÷ 0.6 ≈ 166.67ms,四舍五入)。 3. **关键逻辑**: - 调度器未将B队列的空闲时间分配给A队列(题目未明确说明允许抢占空闲时间)。 - 因此,进程需等待B队列的“无效”时间片,导致总时间延长。 **答案:C. 167ms** 4. 题目:题目:已知多级队列调度系统中有两个队列,高优先级队列采用RR调度算法,低优先级队列采用FCFS调度算法。现有进程P1、P2、P3,P1进入高优先级队列,P2和P3进入低优先级队列。P1时间片为4ms,突发时间为12ms;P2突发时间为8ms;P3突发时间为6ms。假设P1先运行,在P1运行一个时间片后,以下说法正确的是( ) A. P1回到高优先级队列末尾,等待再次调度 B. P1进入低优先级队列,因为其突发时间较长 C. P2开始运行,因为低优先级队列采用FCFS D. P3开始运行,因为其突发时间最短 - 错因:与第一题相同 - 在多级队列调度中,高优先级队列(RR调度)的进程始终优先于低优先级队列(FCFS调度)。具体分析如下: 1. **P1的运行过程**: - P1首先运行一个时间片(4ms),剩余突发时间为8ms。 - 根据RR规则,P1会被**放回高优先级队列末尾**(选项A正确)。此时高优先级队列中仍有P1,因此调度器继续从该队列调度。 2. **低优先级队列的状态**: - 只要高优先级队列非空,低优先级队列(P2和P3)**不会被调度**(选项C和D错误)。 - 题目未提及进程降级机制,因此P1不会因突发时间长而进入低优先级队列(选项B错误)。 3. **最终执行顺序**: - P1需要运行3个时间片(4ms × 3 = 12ms),总耗时12ms。在此期间,低优先级队列始终处于等待状态。 - 只有P1完成后,调度器才会处理低优先级队列中的P2和P3。 **答案:A. P1回到高优先级队列末尾,等待再次调度** Loading... ## Chapter 1: Introduction 1. Which of the following is an advantage of DMA (Direct Memory Access)? ( ) A. It requires the CPU to be constantly involved in data transfer, ensuring data accuracy. B. It reduces the number of interrupts generated during data transfer. C. It can only transfer data in small chunks, which is more flexible. D. It increases the load on the CPU during data transfer. - 正确:B;错选:D - **解析**:DMA的优点是设备控制器可直接将数据块传输到主内存,无需CPU频繁干预,减少了中断次数,提高了数据传输速度,选项B正确。它不需要CPU一直参与数据传输,A错误;可以传输数据块,并非只能小数据块传输,C错误;减轻了CPU在数据传输时的负载,D错误。 2. **Based on page 13, I/O devices can work concurrently with the CPU mainly because ( )** A. of continuous CPU intervention B. the device controller can operate independently to some extent C. other processes stop during I/O D. data transfer relies solely on the memory - Answer**: B ** - Explanation: The device controller's ability to operate with some independence allows I/O devices to work concurrently with the CPU, as per the content on page 13. ### 针对第一题 1. 以下关于软件中断的描述,正确的是?( ) A. 软件中断只能由错误引起 B. 软件中断只能由用户请求(系统调用)引起 C. 软件中断由错误或用户请求(系统调用)引起 D. 软件中断与硬件中断没有区别] - 答案:C - 知识点:Software: A trapor exceptionis a software-generated interrupt caused either by an error or a user request (system call) 2. 中断驱动的I/O循环中,控制返回到用户程序的方式有几种?( ) A. 1种 B. 2种 C. 3种 D. 4种 - 答案:B - 知识点:课件中提到“Interrupt - Driven I/O Cycle: Two Methods”,明确表明中断驱动的I/O循环有两种控制返回用户程序的方式 。一种是“**After I/O starts, control returns to user program only upon I/O completion**”,即I/O完成后控制返回用户程序;另一种是“**After I/O starts, control returns to user program without waiting for I/O completion**”,也就是I/O开始后不等待I/O完成控制就返回用户程序 。 ### 针对第二题 补充知识点:Cache design consideration - size - content replacement policy ### 针对第三题 1. 在双重模式(Dual - mode)系统中,若内核模式下的操作出现错误,可能会( ) - **答案**:A. 导致整个系统崩溃 错选:B - **解析**: - 内核在操作系统中处于核心地位,负责管理系统的各种关键资源,如硬件设备、内存、进程等。内核模式下的操作出现错误,很可能影响到系统关键功能的正常运行。 - 例如,如果内核在管理内存时出现错误,可能导致内存分配混乱,使得其他依赖正确内存管理的进程无法正常工作;若内核在处理硬件中断时出错,可能导致硬件设备无法正常响应,进而影响整个系统的运行。由于内核掌控着整个系统的运行,所以一旦出错,极有可能导致整个系统崩溃。 - **B选项“仅影响当前运行的用户程序”不符合实际情况,因为内核错误影响的范围远不止当前用户程序。** - C选项“自动切换回用户模式继续运行”,内核错误往往比较严重,不是简单切换到用户模式就能继续正常运行的,因为错误可能已经破坏了系统的关键状态和资源。 - D选项“忽略错误并继续执行”,内核级别的错误通常不能被轻易忽略,否则可能引发一系列更严重的问题,甚至导致系统不稳定或无法使用 2. 以下关于用户模式和内核模式下程序执行的描述,错误的是( ) - **答案**:D. 内核模式下程序不能执行普通的算术运算指令 错选:B - **解析**: - A选项“用户模式下程序不能直接访问硬件寄存器”,这是正确的。为了保护系统的安全性和稳定性,在用户模式下,程序的权限受到限制,不能直接访问硬件寄存器,因为直接访问可能会导致硬件状态混乱或系统不稳定。 - B选项“内核模式下程序可以访问所有内存空间”,内核需要管理整个系统的内存资源,为了实现内存分配、回收等功能,内核模式下的程序必须能够访问所有内存空间,所以该选项正确。 - C选项“用户模式下程序执行的指令集是受限的”,这是为了防止用户程序执行可能破坏系统的指令,例如特权指令只能在内核模式下执行,所以用户模式下程序执行的指令集是受限的,该选项正确。 - D选项“内核模式下程序不能执行普通的算术运算指令”,内核模式下程序需要进行各种复杂的操作,包括数据处理、计算等,必然可以执行普通的算术运算指令,所以该选项描述错误。 ## Chapter 2: OS Structure 1. **Question**: Which of the following is a disadvantage of the module structure? A. Lack of flexibility B. Difficulty in customizing the system C. Compatibility issues in module interfaces D. High performance degradation - **Answer**: C. Compatibility issues in module interfaces 错选 D - **Explanation**: In a module - structured operating system, different modules need to interact with each other. Since these modules can be developed independently to some extent, ensuring compatibility between their interfaces can be a significant challenge. For example, a new version of one module might expect a different format of data from another module it interacts with, leading to errors. Option A is incorrect because the module structure is known for its flexibility as modules can be added or removed. Option B is wrong as the ability to customize the system by adding or removing relevant modules is one of its advantages. Option D is also incorrect as the module structure doesn't inherently cause high performance degradation; in fact, it can improve performance in some cases by only loading necessary modules. 2. **Question**: A disadvantage of the monolithic structure is: A disadvantage of the monolithic structure is: A. Low efficiency in resource management B. High code maintainability C. Difficulty in adding new functions without major kernel changes D. Lack of direct interaction between kernel parts - **Answer**: C. Difficulty in adding new functions without major kernel changes 错选B - **Explanation**: In a monolithic structure, all kernel functions are at the same level and intertwined. When adding a new function, it often requires significant changes to the existing kernel code because of the lack of clear modular boundaries. Since the entire kernel is a large, non - layered entity, new code may need to be integrated into multiple existing parts of the kernel. Option A is incorrect as monolithic structures can be highly efficient in resource management due to direct interactions between kernel components. **Option B is wrong because high code maintainability is not a characteristic of monolithic structures;** rather, the intertwined nature of the code makes maintenance difficult. Option D is incorrect as monolithic structures have high levels of direct interaction between kernel parts. - The lack of good module division in a monolithic operating - system structure directly contributes to poor code maintainability, which is a significant disadvantage. When new functions need to be added, developers face challenges due to the highly intertwined nature of the code. Since there are no distinct, well - defined modules with clear interfaces, changes in one part of the code can have unforeseen impacts on other parts of the kernel. This makes it difficult to isolate the effects of new code implementation and increases the risk of introducing bugs throughout the system. So, your understanding about this aspect of the monolithic structure is spot - on. It further emphasizes why option C (Difficulty in adding new functions without major kernel changes) is the correct answer for question 18. ### 针对第四题 1. 传统UNIX操作系统由哪两个可分离部分组成? A. 应用程序和系统程序 B. 系统程序和内核 C. 内核和硬件 D. 硬件和应用程序 - 答案:B;错选:A - **知识点:** The UNIX OS consists of two separable parts: **Systems program / The kernel** 2. 传统UNIX操作系统中,系统程序的作用不包括? A. 提供程序开发环境 B. 执行用户应用程序 C. 实现内核功能 D. 提供程序执行环境 - 答案:C;错选:A - 错因:看错题 ## Chapter 3: Process ### Multiple Choices 1. Question 3: Which of the following statements about the Process Control Block (PCB) is incorrect? A. Each process has a corresponding unique PCB. B. The PCB is a data structure accessible to user processes. C. The PCB is stored in the memory reserved for the kernel. D. When a process terminates, the kernel deletes its PCB. - Answer: B; 错选:C - PCB是内核数据结构。它存储在内核保留的内存中,进程本身不可见。用户进程无法直接访问PCB。只有内核可以访问和修改PCB。 - PCB存储在内核保留的内存中,只能由内核访问和修改。 2. Question 14: In the inter - process communication model of message passing, which ( ) operations are controlled by the kernel? A. Sending messages (send) and receiving messages (receive) B. Allocation of shared memory C. Process creation D. Process termination. - Answer: A;错选:B - A:在消息传递进程间通信模型中,发送消息(send)和接收消息(receive)操作由内核控制。内核负责确保消息在进程之间正确传递,处理诸如消息缓冲、同步和安全等问题。 - B:共享内存的分配与共享内存进程间通信模型相关,而不是消息传递模型。在消息传递中,进程之间不存在与共享内存模型相同意义上的直接内存共享。所以,该选项错误。 ### 针对第五题 1. 上下文切换时,系统必须保存的旧进程关键信息不包括以下哪一项( )A. 程序计数器(Program Counter, PC)的值 B. 进程的优先级 C. 堆(heap)中动态分配的内存数据 D. CPU寄存器(CPU registers)的值 - **答案**:C. 堆(heap)中动态分配的内存数据;错选:B - 解析:上下文切换时需要保存程序计数器(PC)的值,以便知道旧进程下次从何处继续执行;保存CPU寄存器的值,保证进程恢复执行时数据状态一致;进程优先级属于进程调度信息,也需保存以便调度算法使用。而堆中动态分配的内存数据,其内存空间在进程切换时依然存在,只要进程未结束,不需要在上下文切换时专门保存这部分数据。 2. 某操作系统在上下文切换时,需要保存和恢复大量的CPU寄存器状态,这表明该操作系统( ) A. 上下文切换效率较高 B. 对硬件资源的管理较为简单 C. 上下文切换时间可能较长 D. 不支持多进程并发执行 - **答案**:C. 上下文切换时间可能较长;错选:B - **解析**:保存和恢复大量CPU寄存器状态需要花费时间,这会增加上下文切换的时间开销,所以上下文切换时间可能较长。A选项保存恢复大量寄存器状态会降低效率,不是效率高;**B选项说明对硬件资源管理复杂,而非简单;**D选项多进程并发执行与保存寄存器状态多少并无直接关联,且一般操作系统都支持多进程并发 ### 针对第六题 1. 命名管道(Named pipes)与普通管道相比,以下哪项是命名管道特有的优势( ) A. 支持生产者 - 消费者模式通信 B. 可用于进程间双向通信 C. 只能在同一台计算机上使用 D. 通信时需要创建文件描述符 - **答案**:B. 可用于进程间双向通信;错选A - 普通管道同样支持生产者 - 消费者模式通信,B选项双向通信是命名管道优势;C选项两者都只能在同一台计算机上使用;D选项两者通信都需要创建文件描述符。命名管道特有的优势在于无需父子关系即可通信,答案A有误,应选B(双向通信)。 2. 命名管道可以被多个进程同时访问,当多个进程同时向命名管道写入数据时,可能会出现什么问题( ) A. 数据会自动按顺序写入 B. 数据可能会相互覆盖 C. 操作系统会阻止多个进程同时写入 D. 会创建多个管道副本分别写入 - 答案:B - **解析**:**命名管道没有内置机制确保多个进程写入数据的顺序和完整性**,多个进程同时写入可能导致数据相互覆盖,操作系统不会自动阻止,所以B正确。 ### 针对第九题 1. **题目**:以下哪一项不属于进程控制块(PCB)所包含的信息( ) A. 进程的优先级(Priority) B. 进程所执行程序的源代码 C. 程序计数器(Program counter)的位置 D. 已分配给进程的I/O设备信息 - **选项A**:进程的优先级(Priority)是CPU调度信息的一部分,PCB中会包含进程优先级,用于决定进程在调度队列中的顺序,所以A选项属于PCB包含的信息。 - **选项B**:进程控制块主要包含与进程运行控制和资源管理相关的信息,并不包含进程所执行程序的源代码。**程序代码通常存储在内存的代码段(text section),所以B选项不属于PCB包含的信息,该题应选B。** - **选项C**:程序计数器(Program counter)的位置对于记录进程执行位置至关重要,它指示了下一条要执行的指令的位置,是PCB的重要组成部分,所以C选项属于PCB包含的信息。 - **选项D**:已分配给进程的I/O设备信息属于I/O状态信息,PCB需要记录这些信息以便操作系统管理进程的I/O操作,所以D选项属于PCB包含的信息。 2. **题目**:当一个新进程被创建时,操作系统会为其执行以下哪个操作与进程控制块(PCB)相关( ) A. 从磁盘读取PCB模板并初始化 B. 在用户空间创建一个PCB C. 在内核中创建一个新的PCB D. 复制父进程的PCB并修改部分信息 - **选项A**:PCB是内核数据结构,并非从磁盘读取模板并初始化,所以A选项错误。 - **选项B**:PCB存储在内核保留的内存空间,不是在用户空间创建,所以B选项错误。 - **选项C:课件明确提到“When a new process is created, the kernel creates a new PCB for it”,即当新进程创建时,内核会为其在内核中创建一个新的PCB,所以C选项正确。** - **选项D**:新进程创建时,是内核新创建PCB,不是复制父进程的PCB并修改部分信息,**虽然进程创建可能涉及复制父进程某些资源,但PCB是新创建的,所以D选项错误。** 3. **题目**:进程控制块(PCB)中的程序计数器(Program counter)主要用于( ) A. 记录进程已执行的指令数量 B. 指示下一条要执行的指令的位置 C. 存储正在执行的指令 D. 统计进程执行所花费的时间 - **选项A**:程序计数器不是用于记录进程已执行的指令数量,所以A选项错误。 - **选项B**:**程序计数器的主要作用是指示下一条要执行的指令的位置,这样CPU才能按照正确的顺序执行程序指令,所以B选项正确。** - **选项C**:程序计数器并不存储正在执行的指令,指令通常存储在内存的代码段,由CPU从内存中读取,所以C选项错误。 - **选项D**:程序计数器与统计进程执行所花费的时间无关,所以D选项错误。 4. **题目**:假设系统中有两个进程A和B,进程A正在运行,此时进程B被调度到CPU上执行,这个过程涉及到上下文切换。在上下文切换过程中,关于进程A的PCB,以下说法正确的是( ) A. PCB中的信息不会发生改变 B. 仅程序计数器(Program counter)和CPU寄存器的值会被保存到PCB C. PCB中的所有信息会被保存,以便进程A下次能继续正确执行 D. PCB会被暂时删除,当进程A再次被调度时重新创建 - **选项A**:上下文切换时,为了能让进程A下次继续正确执行,PCB中的信息需要被保存和恢复,并非不会发生改变,所以A选项错误。 - **选项B**:上下文切换不仅保存程序计数器(Program counter)和CPU寄存器的值,还包括其他与进程相关的重要信息,如进程状态等,所以B选项错误。 - **选项C**:在上下文切换时,**为了保证进程A后续能继续正确执行,PCB中的所有信息会被保存,当进程A再次被调度时,依据保存的PCB信息恢复其运行状态,所以C选项正确。** - **选项D**:上下文切换时,进程A的PCB不会被删除,只是其状态等信息被保存,以便后续恢复,所以D选项错误。 ### 针对第十题 B,D,B,A,D ,D,A,B,A,A 1/2/5/6/9 知识点: `fork() `system call creates new process, create a duplicate of the parent - `exec()`system call used after a fork()to replace the process memory space with a new program - Parent process calls `wait()` for the child to terminate 1. **题目**:在进程创建过程中,父进程(Parent process)和子进程(Child process)共享资源的方式不包括以下哪种( ) A. 父进程和子进程共享所有资源 B. 子进程共享父进程的部分资源 C. 父进程和子进程共享部分代码段,但数据段独立 D. 父进程和子进程不共享任何资源 - 错选:B;错因:看错题 - **选项A**:课件中明确提到“Parent and children share all resources”,即父进程和子进程可以共享所有资源,所以A选项是可能的共享方式。 - **选项B**:“Children share subset of parent’s resources”表明子进程可以共享父进程的部分资源,B选项也是一种共享方式。 - **选项C:课件中只提及了共享所有资源、部分资源以及不共享资源这三种方式,并没有专门指出共享部分代码段但数据段独立这种情况,所以C选项不属于常见的父子进程共享资源方式,该题选C。** - **选项D**:“Parent and child share no resources”说明父进程和子进程可以不共享任何资源,D选项属于共享资源的一种描述方式。 2. **题目**:当父进程创建子进程后,关于父子进程的执行选项,以下说法**错误**的是( ) A. 父进程和子进程可以并发执行(execute concurrently) B. 父进程可以等待子进程终止(wait until children terminate) C. 子进程不能先于父进程执行 D. 父进程和子进程的执行顺序取决于调度算法 - 错选:C;错因:看错题 - **选项A**:“Parent and children execute concurrently”表明父进程和子进程可以并发执行,这是常见的执行方式,所以A选项说法正确。 - **选项B**:“Parent waits until children terminate”说明父进程可以等待子进程终止后再继续执行,B选项说法正确。 - **选项C**:实际上,子进程和父进程的执行顺序取决于调度算法,子进程完全有可能先于父进程执行,所以C选项说法错误,该题选C。 - **选项D**:在多进程系统中,进程的执行顺序是由调度算法决定的,父子进程也不例外,D选项说法正确。 3. **题目**:在UNIX/Linux系统中,fork()系统调用返回后,子进程通常会调用exec()系统调用,其目的是( ) A. 继续执行与父进程相同的代码 B. 替换自身的内存空间,加载一个新的程序 C. 等待父进程结束 D. 将自身的资源归还给父进程 - 错选:D;错因:`exec()` 是执行一个程序,而不是退出 - **选项A**:子进程调用exec()就是为了改变执行的程序,不会继续执行与父进程相同的代码,所以A选项错误。 - **选项B:课件中提到“exec()system call used after a fork()to replace the process’memory space with a new program”,即fork()后子进程调用exec()是为了用一个新程序替换自身的内存空间,加载并执行新的程序,B选项正确。** - **选项C**:等待父进程结束是父进程调用wait()的作用,而不是子进程调用exec()的目的,C选项错误。 - **选项D**:子进程调用exec()与归还自身资源给父进程没有关系,D选项错误。 4. **题目**:假设在一个程序中,父进程调用fork()创建子进程后,子进程又调用了exec()加载新程序。如果exec()调用失败,会发生什么情况( ) A. 子进程立即终止 B. 子进程继续执行父进程的代码 C. 子进程进入等待状态,直到exec()成功 D. 父进程会收到一个错误通 - 错选:D; - **选项A**:**课件中提到“The only exception to this is if the exec system call fails (usually because Program 2 does not exist). In that case the exec system call returns and Process 2 continues executing Program 1.”,说明exec()调用失败时,子进程不会立即终止,而是继续执行原来的程序,即父进程的代码,所以A选项错误。** - **选项B**:如上述课件内容所述,exec()调用失败后,子进程继续执行父进程的代码,B选项正确。 - **选项C**:子进程不会进入等待状态,而是继续执行原程序,C选项错误。 - **选项D**:父进程不会收到关于子进程exec()调用失败的错误通知,只有子进程自身继续执行原程序,D选项错误。 5. **题目**:如果一个父进程创建了多个子进程,并且希望在所有子进程都结束后再继续执行自身的后续代码,它应该怎么做( ) A. 在每个fork()调用后立即调用wait() B. 在所有fork()调用之后,统一调用一次wait() C. 使用一个循环,对每个子进程的PID调用wait() D. 不需要额外操作,父进程会自动等待所有子进程结束 - 错选:A - **选项A**:在每个fork()调用后立即调用wait(),这样只能等待一个子进程结束,而不是所有子进程,当有多个子进程时,这种方式无法满足需求,所以A选项错误。 - **选项B**:在所有fork()调用之后统一调用一次wait(),只能等待一个子进程结束,不能确保等待所有子进程结束,B选项错误。 - **选项C**:**使用一个循环,对每个子进程的PID调用wait(),这样可以逐个等待每个子进程结束,确保所有子进程都结束后,父进程再继续执行后续代码,C选项正确。** - **选项D**:父进程不会自动等待所有子进程结束,需要显式调用wait()函数来等待,D选项错误。 ### Short Essay Problem 1. Please write a short essay addressing the following aspects based on the content on page 39 of chapter 3: 1. **Describe the two models of inter - process communication (IPC)**: - Explain the shared - memory model. Discuss who controls this model and what the major issue is that needs to be addressed when using it. Provide an example of a problem that can be solved using the shared - memory model in the context of cooperating processes. - Explain the message - passing model. Discuss who controls this model and the two main operations involved. Also, mention some of the implementation issues that need to be considered when establishing a communication link in this model. 2. **Compare the two models**: - Highlight the differences between the shared - memory and message - passing models in terms of control, synchronization mechanisms, and performance implications. For example, consider how synchronization is achieved in each model and how it impacts the overall communication efficiency. 3. **Practical considerations**: - In a real - world software development scenario, when might you choose to use the shared - memory model over the message - passing model? And vice versa? Provide some practical examples or scenarios from different application domains (such as operating systems, distributed systems, or mobile applications) to support your answer. **Answer guide**: 1. **Two models of IPC**: - **Shared - memory model**: - **Control**: User processes control this model. They can directly access and modify the shared memory area. - **Major issue**: Synchronization is the major issue. Since multiple processes can access the shared memory simultaneously, proper synchronization mechanisms (e.g., mutexes, semaphores, which will be discussed in chapters 6 & 7) are needed to avoid data races and ensure data consistency. - **Example**: The producer - consumer problem can be solved using the shared - memory model. Producers and consumers can communicate through a shared buffer in the shared memory. The producer writes data into the buffer, and the consumer reads from it. - **Message - passing model**: - **Control**: The kernel controls this model. - **Operations**: The two main operations are send(message) and receive(message). - **Implementation issues**: When establishing a communication link, we need to consider how links are established (physically through shared memory, hardware bus, network; and logically through direct or indirect communication), whether a link can be associated with more than two processes, how many links can exist between pairs of communicating processes, the capacity of a link, whether the message size is fixed or variable, and whether the link is unidirectional or bi - directional. 2. **Comparison**: - **Control**: In the shared - memory model, user processes have more direct control over the communication medium. In the message - passing model, the kernel has more control as it manages the message - passing operations. - **Synchronization mechanisms**: In the shared - memory model, synchronization is a user - implemented concern to prevent concurrent access issues. In the message - passing model, synchronization can be achieved through blocking or non - blocking send and receive operations. For example, a blocking send blocks the sender until the message is received, providing a form of synchronization. - **Performance implications**: Shared - memory can be faster for communication as there is no need for kernel - mediated message passing. However, the complexity of synchronization can lead to performance bottlenecks. Message - passing, on the other hand, has the overhead of kernel intervention but can be more suitable for distributed systems where direct shared memory is not possible. 3. **Practical considerations**: - **Shared - memory model**: - **When to use**: In applications where high - speed data sharing is required within a single system, such as in - memory databases or some high - performance computing applications running on a multi - core system. For example, in a real - time graphics rendering application, different components of the rendering pipeline (e.g., vertex processing and fragment processing) can share data in a shared - memory area to avoid the overhead of frequent message passing. - **Message - passing model**: - **When to use**: In distributed systems where processes are running on different hosts. For example, in a client - server web application, the client and server communicate through message passing. Also, in mobile applications where different app components (e.g., activities and services in Android) may communicate using a form of message passing for better isolation and security. 2. **Question**: Explain the role of the Process Control Block (PCB) in an operating system. - **Answer**: The PCB stores process - related information like process state, program counter, CPU registers, scheduling and memory - management info. Each process has a unique PCB in kernel - reserved memory. The kernel uses it to manage processes, creating a new PCB on process creation and deleting it on termination. 3. **Question**: Compare named pipes and ordinary (anonymous) pipes. - **Answer**: Ordinary pipes are unidirectional, require a parent - child relationship, and can't be accessed outside the creating process. Named pipes are bidirectional, don't need a parent - child relationship, and can be accessed by multiple processes. Both can be used for inter - process communication on the same computer. 4. **Question**: Describe the steps in a Remote Procedure Call (RPC). - **Answer**: First, the client - side stub locates the server, marshals parameters, and sends them to the server - side stub. Then, the server - side stub unpacks parameters, performs the procedure call, marshals the result, and sends it back. Finally, the client - side stub receives and unpacks the result and returns it to the client. ## Chapter 5: CPU Scheduling ### 针对第八题 1. 题目:在多级队列(Multilevel Queue)调度中,若有前台(foreground)和后台(background)两个队列,前台队列采用时间片轮转(Round - Robin, RR)调度算法,后台队列采用先来先服务(First - Come, First - Served, FCFS)调度算法。现有一个前台进程P1和一个后台进程P2同时到达,以下说法正确的是( ) A. P1会先执行,因为前台队列优先级高于后台队列 B. P2会先执行,因为FCFS算法先到先执行 C. P1和P2会交替执行,按照RR算法的时间片分配 D. 系统会根据进程的突发时间(burst time)决定谁先执行 - **答案:A. P1会先执行,因为前台队列优先级高于后台队列** - 解析:在多级队列调度中,前台队列的优先级高于后台队列。即使进程P1(前台)和P2(后台)同时到达,调度器会优先处理高优先级队列中的进程。前台队列采用时间片轮转(RR)调度,因此P1会被立即调度执行。只有当P1完成或主动释放CPU(如时间片用完但未结束,重新排队后仍属于前台队列),且前台队列为空时,后台队列的进程P2才会被调度执行。由于前台队列始终优先,P1会先执行,而非交替执行或依赖突发时间。选项B、C、D均不符合多级队列调度规则。 2. 题目:假设在多级队列调度系统中有三个队列Q1、Q2、Q3,优先级依次降低。Q1采用RR调度算法,时间片为5ms;Q2采用SJF调度算法;Q3采用FCFS调度算法。现有进程P1、P2、P3分别进入Q1、Q2、Q3队列。P1突发时间为10ms,P2突发时间为8ms,P3突发时间为15ms。在这种情况下,哪个进程最有可能先完成( ) A. P1 B. P2 C. P3 D. 无法确定,取决于进程到达时间 - 错选:B;错因:没有理解优先级高的 queue 如果非空要一直执行完,导致只算了一个时间片就跳到 Q2 去了 - 在多级队列调度系统中,队列的优先级决定了执行顺序。Q1(最高优先级)使用RR算法(时间片5ms),Q2使用SJF,Q3使用FCFS。三个进程P1(Q1,突发时间10ms)、P2(Q2,8ms)、P3(Q3,15ms)同时到达各自队列。 1. **Q1的调度过程**: - P1在Q1中执行第一个时间片(5ms),剩余突发时间5ms。 - 由于Q1非空且优先级最高,调度器继续执行Q1中的P1(第二个时间片5ms),P1在10ms时完成。此时Q1为空。 2. **Q2的调度过程**: - Q2采用SJF,且仅有P2(突发时间8ms)。Q1完成后,P2开始执行,耗时8ms,于18ms完成。 3. **Q3的调度过程**: - Q3采用FCFS,且仅有P3(突发时间15ms)。Q2完成后,P3开始执行,耗时15ms,于33ms完成。 **结论**:由于Q1优先级最高且P1的突发时间恰好为两个时间片(5ms×2),P1在10ms时率先完成。Q2和Q3的进程需等待Q1完全空闲后才能执行。因此,**P1最有可能先完成**。 **答案:A. P1** 3. 题目:题目:在多级队列调度中,若采用时间片(Time slice)调度策略,为每个队列分配CPU时间。假设有两个队列A和B,A队列分配到60%的CPU时间,B队列分配到40%的CPU时间。如果系统中只有A队列有进程在等待,且该进程需要100ms的CPU时间才能完成,那么该进程实际完成时间大约是( ) A. 100ms B. 120ms C. 167ms D. 250ms - 其实没有选错,但是错误的计算方式是这样:$0.6t \geq 100ms$ ,总时间 $T_0 = n \times 0.4T + T \geq 167ms$ - 混淆了 - 在多级队列调度中,当队列A和B分别分配60%和40%的CPU时间时,**即使B队列无进程,调度器仍可能严格按比例分配时间**。这意味着: 1. **时间片划分**:系统按比例将CPU时间分配给队列。例如,每单位时间(如100ms)中,A队列占用60ms,B队列占用40ms。若B队列无进程,其40ms时间将被**空闲**。 2. **进程执行时间计算**: - 进程需要100ms的CPU时间,但实际仅在A队列分配的60%时间内运行。 - 实际完成时间满足:**总时间 × 60% = 100ms**,即总时间 ≈ **167ms**(100ms ÷ 0.6 ≈ 166.67ms,四舍五入)。 3. **关键逻辑**: - 调度器未将B队列的空闲时间分配给A队列(题目未明确说明允许抢占空闲时间)。 - 因此,进程需等待B队列的“无效”时间片,导致总时间延长。 **答案:C. 167ms** 4. 题目:题目:已知多级队列调度系统中有两个队列,高优先级队列采用RR调度算法,低优先级队列采用FCFS调度算法。现有进程P1、P2、P3,P1进入高优先级队列,P2和P3进入低优先级队列。P1时间片为4ms,突发时间为12ms;P2突发时间为8ms;P3突发时间为6ms。假设P1先运行,在P1运行一个时间片后,以下说法正确的是( ) A. P1回到高优先级队列末尾,等待再次调度 B. P1进入低优先级队列,因为其突发时间较长 C. P2开始运行,因为低优先级队列采用FCFS D. P3开始运行,因为其突发时间最短 - 错因:与第一题相同 - 在多级队列调度中,高优先级队列(RR调度)的进程始终优先于低优先级队列(FCFS调度)。具体分析如下: 1. **P1的运行过程**: - P1首先运行一个时间片(4ms),剩余突发时间为8ms。 - 根据RR规则,P1会被**放回高优先级队列末尾**(选项A正确)。此时高优先级队列中仍有P1,因此调度器继续从该队列调度。 2. **低优先级队列的状态**: - 只要高优先级队列非空,低优先级队列(P2和P3)**不会被调度**(选项C和D错误)。 - 题目未提及进程降级机制,因此P1不会因突发时间长而进入低优先级队列(选项B错误)。 3. **最终执行顺序**: - P1需要运行3个时间片(4ms × 3 = 12ms),总耗时12ms。在此期间,低优先级队列始终处于等待状态。 - 只有P1完成后,调度器才会处理低优先级队列中的P2和P3。 **答案:A. P1回到高优先级队列末尾,等待再次调度** 最后修改:2025 年 03 月 24 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏