# OS Homework Assignment #1
## 110590064 資工三 劉韶軒
### Written Exercises
#### Chap.1
##### 1.16
###### 題目
>1.16: Direct memory access is used for high-speed I/O devices in order to avoid increasing the CPU’s execution load.
>(a) How does the CPU interface with the device to coordinate the transfer?
>(b) How does the CPU know when the memory operations are complete?
>(c) The CPU is allowed to execute other programs while the DMA controller is transferring data. Does this process interfere with the execution of the user programs? If so, describe what forms of interference are caused.
###### Answer
(a) 在剛開始要啟動DMA傳輸時,CPU會將DMA的暫存器,就能直接使用DMA控制器直接對匯流排放置地址進行傳輸,而CPU不需要參與這個傳輸過程
(b) 當DMA完成工作,DMA控制器會通知CPU進行中斷,CPU根據回傳數據進行後處理
(C) CPU跟DMA都是使用匯流排猜做記憶體的主控,如果同時存取內存,會出現CPU跟DMA更新同一個位置的資料,會產生一致性問題,所以DMA使用時,應該暫時停止CPU訪問主記憶體
#### Chap.2
##### 2.15
###### 題目
> What are the two models of interprocess communication? What are the strengths and weakness of the two approaches?
###### 答案
1. Shared Memory Model(共享記憶體模型)
- 優點:直接讀取共享記憶體 通訊速度快 實現簡單
- 缺點:資源競爭 同步問題
3. Message Passing Model(消息傳遞模型)
- 優點:進程互相獨立,不會訪問對方內存,安全性增加
- 缺點:需要額外的系統調用和管理 增加複複雜度
##### 2.19
###### 題目
> 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?
###### 答案
1. 簡單化作業系統 只保留基礎功能 防止崩潰
1. 記憶體管理
1. 系統呼叫
1. 行程間通訊(IPC)
1. 執行緒管理
#### Chap.3
##### 3.12
###### 題目
> Describe the actions taken by a kernel to context-switch between processes.
###### 答案
内核在进行进程上下文切换时
首先将当前进程的上下文保存在内存中的PCB中
然后再将下一个进程在内存中的PCB里的上下文读取出来
上下文包含
1. CPU寄存器里的内容
2. 堆
3. 用户線程
4. 内存管理信息
5. 数据
6. 文本
##### 3.18
###### 題目
> Give an example of a situation in which ordinary pipes are more suitable than named pipes and an example of a situation in which named pipes are more suitable than ordinary pipes.
###### 答案
简单的通信可以使用普通管道。
例如,假设我们有一个計算加法进程.可以使用 一个普通管道
我們要產生的人只要存管道放入,需求方只要從另一方拿出就好
### Programming Problems
#### 2.24
: In Sec.2.3, we described a program that copies the contents of one file to a destination file.
This program works by first prompting the user for the name of the source and destination files.
Write this program using either POSIX or Windows API.
Be sure to include all necessary error checking, including ensuring that the source file exists.(… to be continued)
(… continued from the previous slide)
Once you have correctly designed and tested the program, if you used a system that supports it, run the program using a utility that traces system calls. (Assume the name of the executable file is FileCopy)
Linux systems provide the strace utility
(strace ./FileCopy)
Solaris and Mac OS X systems use the dtrace command
(sudo dtrace ./FileCopy)
As Windows systems do not provide such features, you will have to trace through the Windows version of this program using a debugger.