An operating system is software that manages a computer’s hardware. It also provides a basis for application programs and acts as an intermediary between the computer user and the computer hardware. An amazing aspect of operating systems is how they vary in accomplishing these tasks in a wide variety of computing environments. Operating systems are everywhere, from cars and home appliances that include “Internet of Things” devices, to smart phones, personal computers, enterprise computers, and cloud computing envi- ronments.
In order to explore the role of an operating system in a modern computing environment, it is important first to understand the organization and architec- ture of computer hardware. This includes the CPU, memory, and I/O devices, as well as storage. A fundamental responsibility of an operating system is to allocate these resources to programs.
Because an operating system is large and complex, it must be created piece by piece. Each of these pieces should be a well-delineated portion of the system, with carefully defined inputs, outputs, and functions. In this chapter, we provide a general overview of the major components of a contemporary computer system as well as the functions provided by the operating system. Additionally, we cover several topics to help set the stage for the remainder of the text: data structures used in operating systems, computing environments, and open-source and free operating systems.
1. Computer System Architecture
A. von Neumann Architecture

- Instruction and Data 存在同一個 memory unit, 並透過同一個 bus 傳送
B.Harvard Architecture

- Instruction and Data 用不同的 bus
現代電腦多使用 von Neumann Architecture, 但在 instuction and data 的快取架構則類似 Harvard Architecture
2. Type of Operating System
A. Stand Alone (Uni-Proccessor) Operating System

(1) Monolithic Structure (MS-DOS, early UNIX)

- Large kernel that handle everything
- 比較小的 communication overhead (因為每個 part 都綁在一起)
(2) Layer Design (Linux)

- 每個 layer 使用上一個 layer 提供的 service 並提供 new service 給下一個 layer
- Eazy to modify/update (一次 compile 一個 layer 就好)
- 分工方便, 工程師只要會 implement 自己的 layer 跟理解上一層就好
(3) Micro-Kernel Architecture

- 盡可能縮小 kernel 的 size
- 只提供「非要不可」的服務,其他服務放到 user mode 裡(application program, file system, …)
- 當呼叫 system call 或需要 communicate with other components 時,會短暫進入 kernel mode (兩次 mode switch -> overhead)
- 優點:開機快速、在 usermode 的 service 可以在不 reboot 的情況下進行更動
(4) Modules
- Many OS implement loadable kernel modules (LKMs)
- 只有當需要時才 load 某個 module
大部分 OS 使用 Hybrid System 的方法設計,如:Linux/Solaris - monolithic + LKMs, Max OS - microkernel + LKMs
B. Distributed Operating System

- Each computing unit has its own hardware (including memory, CPU, and IO devices), but all the computers should be homogeneous (use same processor and architechture)
- Looks like a uni-processor operating system but operates on multiple independent computing units
C. Network Operating System

- Each computer has its own operating system.
- The distributed applications request services from multiple computers (different OS) to accomplish its assignment

D. Middleware

DOS v.s. NOS

- DOS: 高容錯(OS 互通,磁碟壞掉可以搬到別台電腦)、低自主性、成本高(需 homogeneous)
- NOS: 低容錯、高自主性
3. Interface with Operating System

- System Service 是由 System Calls build 出來的
- OS 透過 Harware Interrupt 與 hardware 溝通 (Signal - Software Interrupt)
A. System Calls

- Typically, a number is associated with each system call
- System-call interface maintains a table indexed according to these numbers
- System call 傳遞參數的方式:
- 存在 register 裡,方便&快速但參數的數目受限
- 存在某一塊 memory 裡,並傳遞該塊 memory 的 pointer
- 把 parameters push 到 stack 裡,再由 kernel pop 出來
4. Interrupts
- 每個 interrupt 有自己的 number,並對到一個 ISR (Interrupt Service Routine)
- 在 interrupt vector 裡,第 i 號位置存第 i 個 interrupt 的 ISR code 的「位址」
- trap and exception 是由 software 產生的 interrupt
- Interrupt Timeline :

- 這張圖裡面也有 Asychronous / Sychronous IO 的概念:
- Asychronous: 雖然 User 已經覺得自己成功寫完了(printf returned),但 kernel 很可能只是「接到」(handle)了這個 trap,還沒有真的去執行寫到 memory 的動作 (IO request)
- Sychrnous: User mode 裡的函式會等到相應的 system call, IO 皆 return 才 return
- Asychronous 的好處是不同的 device 可以同時進行工作,不用等對方 (e.g. IO and CPU computation at the same time)
5. Functional Components in Operating Systems
A. Storage Sub-system
- Primary Storage (main memory)
- Random access and volatile(需要插電資料才會留著)
- Dynamic Random-access memory (DRAM) 資料會慢慢消失,需要定期回寫(用電容實作)
- Static Random-access memory (SRAM) 資料不會消失
- Secondary Storage
- Hard Disk Drives (HDD)
- Non-volatile memory (NVM) 不用插電
- Flash (SSD)

- 階層式設計是為了避免 impedance mismatch(讀寫速度相差太多),也就是避免互相等的情況
B. I/O Subsystem
- Much slower than CPU computation
- 在 Unix 裡,會以 file 的形式呈現