OS 筆記 - CH3 OS Develop (開發)

CH3 OS 開發

此章節介紹

  • OS 提供的服務
  • API 與 System Call 的關係
  • OS Structure 種類
  • VM 介紹
  • Policy and Mechanism

基本架構

Operating System Services 通常會有這幾樣 :

  1. User Interface : 包含 UI, CLI, GUI, Batch
  2. Program execution : system 要能夠 load program and execute
  3. I/O operation
  4. File-system manipulation(操作) : 讀寫檔案、權限管理
  5. Comunications : process 可能會交換資訊 (share memory, massage passing)、
    或者是不同 PC 之間透過網路傳資料
  6. Error detection : CPU, memory, I/O, user program 都可能發生

跟效率有關的

  1. Resource allocation : mutiple users, multiple jobs running concurrently(同時)
  2. Accounting : 追蹤使用者使用哪些資源
  3. Protection and Security
    • Protection 要確保每個 access 到 system 資源,都是被控制中的
    • Security 要確認 authentication, 防止企圖從非法途徑的外來 I/O

Command Interpreter

def

user 與 OS 之間的溝通介面,我個人解釋為像是 command line 這樣的存在。

  1. 接收 input command
  2. 判斷命令正確與否,正確的話就跑副程式

設計難點

  1. Command Interpreter 是否要包含 Command service routines ?
    • 包含 : 啟動快 (皆已載入 memory, 要用的時候直皆 jump),
      但是 Command 數目限制於 Interpreter 大小,也不易於增減
    • 不包含 : 與上面相反
  2. Command Interpreter 與 OS kernel module 的關連程度 ?
    • Tightly : 直覺,但 Command Interpreter 就不易變更,user 不好客製化
    • Loosely : 例如 UNIX,UNIX 的 Shell 與 kernel 是獨立的,可以任意改 Shell

API

全名 Application Program Interface,算是一個命令集合體。
給參數給好給滿,事情幫你做好好,易於使用者使用。
若有需要特權指令,仍會做 System Call

  • 像是 malloc() 和 free() 都會幫 call brk()

Why API ? [周]

  • Simplicity : API is defined for applications
  • Protability : API is an unified(統一) defined interface
  • Efficiency : not all functions require OS services or involve kernel
    • 能不經過 OS 就別浪費時間

常見類型 :

  1. Win32 API : Windows
  2. POSIX API : POSIX-based systems,像是 UNIX, Linux, Mac OS X
  3. Java API : JVM

System Call

Request OS Services. (with trap to OS)

大約有以下類型 (概述) [周]

  • Process control : abort, create, allocate, free
  • File managment : create, delete
  • Device management : read, write, reposition(復位) device
  • Information maintenance : get time
  • Communications : send, receive

順序 : user program trap -> OS 代入 Syscall ID + 參數 -> 執行 -> return result
其中,在傳遞參數上有幾種方式 :

  1. 全丟 reg,快,但可能不夠用
  2. 用 memory 做 table,再用 reg 紀錄起始位置,慢但是大空間
  3. Push 進 Stack,一樣可以 大空間,指令操作也只有 push pop

System Structure

User goal : easy to use, reliable, safe
System goal : easy to design, implement, maintain, reliable, error-free, efficient

Simple OS Architecture

  • only one or two levels of code
  • 優點 : 任意存取硬體
  • 缺點 : Un-safe, 很難加強

MS-DOS

Non-Simple Structure

  • more complex than simple
  • 少老師提及
  • ex : UNIX (limited by HW functionality)
  • 主要有兩個 part
    • System Program
    • Kernel

Layer OS Architecture

  • red 上下層 independent
  • 外層可以往內層 access (一層層往下 call),反之不能 (最外層為 user interface)
  • 優點 : 方便 debug、維護 (內到外,buttom-up)
  • 缺點 : Layer 劃分實在困難,缺乏效率

Microkernel OS

  • Moves as much from kernel into user space.
  • Kernel 只負責溝通,全外包啦
  • Communicating : Message passing
  • 優點 :
    • easier to extending
    • easier to port OS to new architectures ( kernel 小)
    • Reliable (less code is running in kernel mode)( crash is "process")
    • more secure
  • 缺點 : 充斥著各種 massage passing between user and kernel space,效能瓶頸
  • virtual memory 和 I/O 都不是 kernel 提供的
  • 補充 : Monoithic kernel 與 micro 相反,所有服務都在 kernel space,優缺點也相反

Modular OS Structure

  • Most mordern OS implement kernel modules

    • OO approach
    • each core component is separate
    • each talk to the others over known interfaces
    • each is loadable as needed within the kernel (Dynamic,需要才載入)
  • 有點像 Layers 設計但又更加彈性

Virtual Machine

  • def :

    • A virtual machine takes the layered approach to its logical conclusion.
      (用分層的方法去實現他)
    • 把 hardware 和 OS kernel 都視為自己(VM)的 hardware
    • A virtual machine provides an interface identical to the underlying bare hardware
      (所提供的接口和一個基礎的 bare hardware 相同)
    • 難點在於 Critical instruction
      • 補充 : 一種在 user 和 kernel space 都能做,結果卻不一樣的指令。
        hardware 對此也幫不上忙,因為 user 可以做,禁止不了
  • 優點 :

    • 提供良好的保護 (病毒和 crash 都只在 VM 內)
    • 利於測試、和對 OS 進行研究與開發
    • 在雲端計算上可以有更好的利用度
    • ------- 以下補充 ---------
    • 節省成本
    • 擴展性 : Freeze, Suspend, Running, Clone VM
    • 模板化與移植性
  • 缺點 :

    • 不易製作
    • 實際 VM內效能比實體來的差
      • VM 執行 syscall 其實是 VMM 攔截 interrupt 再幫他跟 真正OS call 一次
    • 需要硬體支援 (Kernel mode, User mode, VM mode,現今大多 CPU 有支援第三 mode)

架構圖

補充 : 實作 VMM (Hypervisor)

作為管理的 VMM,究竟出現在哪層呢 ? 這根據不同的實作方式而有不同。

  1. Type 0 : 直接做到硬體上
    • HW-base : 直接放到 ROM 裡面了,舉例 : IBM LPARS, Oracle LDOMs
  2. Type 1 : 又細分幾種,基本上就是 OS 自帶的意思
    • OS-Like Software : 就是一個 OS 軟體,舉例 : VMware ESX
    • General purpose OS provide VMM servise,舉例 : Windows Server with Hyper V
  3. Type 3 : (Full-Virtualization)
    • App run on OS but provides VMM feature to guest OS ( 直接把 VMM做成軟體 )
      舉例 : 常見的 VM 都是,VMware Workstation, Parallel Desktop, Virtual Box
    • VMware 這類的特色就是 完全不知道自己是被模擬出來的
      底下有個 virtualization 作為下層,上面承載著各個獨立的小 VM,
      整個被 OS 視為 process
    • Type 3

其餘模擬化方式

Para-virtualization : Xen

  • 同時讓 OS 和 VM 在同個 hardware 上

  • [定義] : Presents guest with system similar but not identical to the guest’s preferred systems (Guest must be modified)
    "存在一個知道所有 VM 的 master 程式 (manager),且 guest OS 也需要被修改"

  • Hardware rather than OS and its devices are virtualized (Only one kernel installed)

  • Within a container (zone) processes thought they are the only processes on the system

  • EX : Solaris 10 : creates a virtual layer between OS and the applications.

  • 必須要修改OS Kernel,並且增加Hypercall

  • 由於某些Non-Virtualizable OS指令不能被hypervisor trap,所以 Guest OS 利用 Hypercall 呼叫 Hypervisor 對缺少的指令進行更換,這也是為何要修改 OS 的核心

  • Guest OS 知道自己活在虛擬化的環境下,Guest OS 知道有其他 Guest OS 的存在,並且看到的都是真實的硬體,Host OS 則不用模擬 CPU,Guest OS 自行互相調配

  • 而在Hypervisor之上,還跑著一個Host OS(Dom0 in Xen),是用來管理Hypervisor的,並且利用native OS的方法管理硬體上的所有I/O driver,所以hypervisor不作硬體模擬,而Guest OS不需要driver,可以直接找Host OS

Solaris,左邊就是 admin


XEN

KVM Architecture

  • 官方定義 : KVM (Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V).
  • 算是 **Hardware-assisted virtualization **
    KVM

模擬器

... 就是模擬器,你玩的 GBA 模擬器就是透過軟體來解析任天堂 instruction 然後轉成 intel instruction 或其他 OS 看得懂的。

Applocation Containment

ex : Solaris Zones, BSD Jails
感覺 docker 也有點像。

Java Vitual Machine

  • Compiled Java programs are platform-neutral bytecodes executed by a Java Virtual Machine (JVM)
    (將寫好的 Java 程式,透過 Java Compiler 編譯成 Java Bytecode,此檔案無法直接執行
    再透過 對應平台的 JVM 去翻成各平台的 Machine code。)
  • JVM 擁有
    • class loader
    • class verifier
    • runtime interpreter
  • 一行一行翻有點慢,可使用 Just In Time Compiler ( JIT )協助

JVM

Policy and Mechanism

  • Mechanism : How to do what,很少變,想像成副程式
  • Policy : What to do,經常改變, 想像成參數
  • 這兩個分開設計的優點 : 可以增加彈性 (flexibility)
  • 例子 :
    • Mechanism : 決定用 Timer 來當 CPU Protection 的機制
    • Policy : 制定 Time Quantum 的值
  • 例子2 :
    • Mechanism : 採用 Priority 排班
    • Policy : 定義每個工作單位的 Priority

Review

P.21

  • What are the two communication models provides by OS ? message passing, share memory
  • What is the relationship between system calls, API or C Library ? API 包含了多數集成指令,方便使用者呼叫,也有一些 API 會代為呼叫 Syscall。而 C Library 則是用來 implement API
  • Why use API rather than System call ? 簡單,interface 定義統一,更有效率(Not all functions require OS services or involve kernel)

P.34

  • What is the difference between the layer approach, the modular approach and microkernel? layer 已包含所有功能,階層式且由外往內 call;microkernel 是只留基礎在 kernel,其他所有服務統一在 userspace 另外呼叫;modular 是有需要那些功能再 loading 進 kernel,比 layered 還彈性,也比 microkernel 有效率(不用一直 call)
  • What are the advantages of using virtual machine? protect system resource, 利於測試與開發 OS, increase resource utilization in cloud computing

參考

Mage 大的筆記、延伸閱讀、薛至文教授 PPT、周至遠教授 PPT

關於 Trap 可以再看這篇

附註

這個章節可以牽扯到的東西很多,但實際上會不會考那麼深 ... 可能不會。
很多東西都是用補充的,其中也有很複雜但只有簡略提及的,有興趣再深入即可。


END

-----------------------------------

2019.11.01