---
title: Ch2:OS Structure
---
# 作業系統Ch2: OS Structure
###### tags: `Operating System` `Review` `Ch2`
## OS Services
- <font color = 'red'>User interface</font>
- CLI (Command Line Interface)
- 自使用者用抓取 command 且執行它
- Shell: Command-line interpreter (CSHELL, BASH)
- 根據使用者行為和偏好調整。
- GUI (Graphic User Interface)
- Program Execution
- I/O operations
- File-System manipulation
- <font color = 'red'>Communication</font>
- Communication 以 message passing (可跨電腦) 和 shared memory。

- message passing: 將 process A 的資料 copy 到 kernel,接著 process B 到 kernel 中把資料 copy 出來讀取。 (必須要 call system call)
- shared memory: 透過 OS 的System call 去規劃出一塊空間,讓 process A and B 能夠看得見且進行 R/W
- Error detection
- $\begin{cases}
Resource\ allocation \\
Accounting \\
Protection\ and\ Security \\
\end{cases} ensuring\ the\ efficient\ operation\ of\ system\ itself$
> accounting: 記錄哪些user用了哪些 resource
> protection: 確保系統內部資料的安全性,e.g., 非 root 不可讀取 /etc/shadow
> security: 保護系統不受到惡意攻擊的損害,ex,即使 root 讀取 /etc/shadow,這裡面的passwd也已經 hash 過,無法直接得到正確的密碼。
## OS-Application Interface
- System Calls:
- 正在運行的 program 的 OS interface
- 經由 software interrupt 產生對於 kernel 具體 request
- Process control - abort, create, terminate process, allocate/free memory
- File management - create, delete, open, close file
- Device management - read, write, reposition device
- Information maintainance - get time or date
- Communication - send/receive message
- 通常是用組合語言或者C語言的指令寫成的
- API (Application Program Interface)
- 大部分使用者寫的程式是對於 API 去進行而非 system call
- 常見透過於 language libraries 去實作,如 C library
- 一個 API call 可能牽扯到零個或多個 system call
- malloc() 和 free() 兩者都使用 system call brk()
- Math API functions, 像是 abs() 不需要 system call
- Three most common APIs:
- Win32 API for windows
- POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Max OSX)
- Java API for the Java virtual Machine (JVM).

```c=
//Interface vs. Library
// User program
printf("%d", exp2(int x, int y));
// Interface:
int exp2(int x, int y);
// i.e., return the value of x * 2 ^ y
// Library:
//imp1:
int exp2(int x, int y)
{
for (int i=0; i<y; i++)
x=x*2;
return x;
}
//imp2:
int exp2(int x, int y)
{
x = x << y;
return x;
}
int exp2(int x, int y)
{
return HW_EXP(x,y);
}
```
Example of Standard API
```c=
#include<unist.h>
ssize_t read(int fd, void *buf, size_t count)
// int fd - 待讀取的 file descriptor
// void *buf - 從 buffer 讀取出來的資料
// size_t count - 可以從 buffer 讀取出來的最大 bytes 數量
// 一旦成功讀取,讀到的 bytes 數量就會回傳,回傳值如果是 0,代表 end of file
// 如果產生錯誤,回傳值為 -1
```
### API – System Call – OS Relationship

Why use API?
- Simplicity - API 為應用程式而設計的
- Portability - API 是一個由統一定義的 interface
- Efficiency - 不是所有函數都需要 OS services 或者牽扯到 kernel。
### Systems Calls: Passing Parameters
三種普遍方法在正在跑的 program 和 OS 之間傳遞參數
- Pass parameters in <font color='red'>registers</font>
- Store the parameters in a <font color='red'>table</font> in memory, and the table address is passed as a parameter in a register
> 這裡 table 就好比自己定義一個 data structure,裡面放著需要的資料,然後用一個指標指向這個 data structure 並進行傳遞。
- Push (store) the parameters onto the <font color='red'>stack </font> by the program, and pop off the stack by operating system
## OS Structure
User goals: 易於使用和學習,且要可靠、安全和快速
System goals: 易於設計、實作和維護,且要可靠、無錯誤和有效率
- Simple OS Archtecture
- 只有一或兩層的程式碼 (driver 跟 OS 切開)。
- 缺點: 不安全,難以提升

- Layered OS Architecture
- 下層不仰賴上層
- $N_{th}$ layer 只可以存取由 $0\sim(N - 1)$ layer 提供
- Pros: 易於除錯/維護
- Cons: 缺乏效率、難以定義layers

- Microkernel OS
- 盡可能把東西 (如 I/O manager)從 kernel 搬到 user space
- 由 message passing 去溝通
- 易於擴展和移植
- 效率比分層更差

- Modular OS Architecture
- 大部分現代 OS 實作 kernel modules
- 使用物件導向方法
- 每個核心組件是獨立的
- 核心彼此之間透過 known interfaces 去溝通
- 在 kernel 內隨需要,每個都可 loadable
- 類似 layers 但是更具彈性。

- Virtual Machine
- 採用 layered approach
- 將 hardware and OS kernel 抽象化為虛擬硬體
- VM 提供介面等同於底層的 bare hardware。
- 不容易達成因為 critical instruction
> 不是一種特權指令,所以在 user space 也可以執行。這種指令在 user mode 假如操作是將兩數兩加 (A+B), 但是在 kernel mode時,執行會變成 (A+B+B),它們的行為是不一樣的。沒有 HW support,它不會被 catch,所以不會知道結果是對的還是錯,也沒有辦法透過 interrupt 去 handle。
Usage of Virtual Machine
- 提供系統資源保護
- 解決系統相容性問題的手段
- 作業系統開發和研究的工具
- 在雲端計算中增加資源使用率的手段
Virtual Machine Types
- Full Virtualization (VMware、 KVM)
- Run in user mode as an application on top of OS
- Virtual machine believe they are running on bare hardware but in fact are running inside a user-level application
> 可以灌到 host 的 OS 程式碼,不需要任何修改也可以灌到 guest OS上。

- Para-Virtualization (Xen)
- 硬體而非作業系統和它的裝置被虛擬化
- 在 container(zone) 裡, processes 認為它們是唯一在系統上的processes
> 可以灌到 host 的 OS 程式碼,**一定要修改**才可以灌到 guest OS 上
> 有一個 global zone 區塊可以知道所有 VM, 但是 VM 彼此間無法知道彼此存在。

- Java Virtual Machine
- 已編譯完成的 Java programs 是 platform-neutral bytecodes,由 JVM 執行。
- Just-In-Time (JIT) compilers increase performance
> 會記錄下來翻譯過的 instruction是什麼,再次遇到時直接 copy過去就好

## Ref
[1] [上課影片 link](https://www.youtube.com/playlist?list=PLS0SUwlYe8czigQPzgJTH2rJtwm0LXvDX)
[2] [投影片 link](https://ocw.nthu.edu.tw/ocw/index.php?page=course_news_content&cid=141&id=999)