# Chp 2 Operating System Structures
###### tags: `作業系統`
[TOC]
參考資料:[02. 作業系統結構 (System Structure)](https://sls.weco.net/node/21322)
* Simplified view

### User and OS Interface
* Command Interpreters (**shell**)
* **Hide low level system operation**, that is why it is called shell
* get and execute the next user-specified command
* implementation
1. **contain the code** to execute the command
2. **identify a system program to be loaded** into memory and executed
* ash, bash, csh, zsh等等,差異在於每種shell支援的指令多少有些不同
* GUI
- mouse, keyboard, monitor
* Touch-Screen Interface
* operated by gestures
### System Calls
* <font color=#ff0000>**考**</font> System calls is a programming **interface** to the service **provided by an OS**.
* Written in high-level language (C or C++)
* <font color=#ff0000>**考**</font> Mostly accessed by programs via a high-level API
- **APIs v.s. system calls**
+ A program using an API **can compile and run** on any system that supports the same API.
+ System calls can often be **more detailed and difficult** to work with.
* **System-call interface** maintains a table indexed according to these numbers
* <font color=#ff0000>**考**</font> System Call Parameter Passing:
1. Pass the **parameters** in registers
+ 優: 快速
+ 缺: 不適用於參數太多
2. Registers pass starting addresses of blocks of **parameters stored in memory**.
+ 優: 適用於參數太多
+ 缺: 速度較慢
>[color=#00ff00]
>
>main memory as "secondary storage" w.r.t. register
>[name=Omnom]
3. Parameters can be placed(or **pushed**) onto the stack by the program, **popped** off the stack by the operating system.
+ push 保存參數
+ pop 取出參數
* <font color=#ff0000>**考**</font> Types of System call :
1. Process control
+ **Debugger** for determining bugs, single step execution
+ **Locks** for managing access to shared data between processes
2. File management
+ create, delete, read, write file...
3. Device management
+ request, release device
+ get device attributes, set device attributes
4. Information maintenance
+ get system data, set system data
5. Communications
+ **Message passing model**: send, receive messages
- Pros: Faster
- Cons:
cache coherence issues, ensures no writing to the same location simultaneously
+ **Shared-memory model**: create and gain access to memory regions
- Pros:
useful for exchanging small amounts of data, no conflicts, easier to implement in a distributed system
- Cons: Slower
6. Protection
+ Control access to resources
>[color=#ff00ff]補充:在linux核心的share memory中沒有提供共享記憶體的保護機制,user需要自行添加如semaphore或是mutex之類的鎖,或是利用queue來避免不同的processes之間同時存取一個記憶體區塊。
>[name=Neko]
### OS Design and Implementation
* <font color=#ff0000>**考**</font> Policy vs Mechanism
- **Policy**: **What** will be done?
- **Mechanism**: **How** to do it?
<br/>
* Emulation can allow an OS to run on non-native hardware
>[color=#ff00ff]Emulation叫做仿真,simulation叫做模擬
>Emulation是連同硬體的行為一起模擬,Simulation則不在乎這個
>[name=Neko]
>[color=#00ff00]
>
>[這個](https://hwchen18546.wordpress.com/2014/03/06/vm-simulator-and-emulator/)
>[name=Omnom]
### Structures
1. **Monolithic Structure**
All functionality of the kernel place into a single, static binary file in a single address space .
Entire operating system is working in kernel space and is alone in supervisor mode.
- Pro
+ little overhead in the system-call interface
+ fast communication within the kernel
- Con: (**tightly coupled**)
+ difficult to implement and extend
2. <font color=#ff0000>**考**</font>**Layered Approach**
Higher layer invokes operations of lower layers (black boxes)
- <font color=#ff0000>Each layer is implemented only with operations provided by lower-level layers</font>
+ Difficulty: **Need careful planning** because a layer can only use the functions and services provided by lower-level layers.
- Pro
+ simplify construction and debugging (**hierarchical**) 每一層的錯誤改正後才會進入下一層。
+ use functions and services of only lower-level layers
- Con
+ traverse through multiple layers
+ Pro is also Con:
+ Needs careful planning, appropriately defining the various layers.

3. **Microkernels**
as it's called, simplify kernel
<font color=#ff0000>Move some services to user space.</font>
- Pro:
+ services in user space are **extendable**
+ less kernel modification
+ increase **security** (detail of services in kernel mode is hidden from user)
+ increase reliability (less code is running in kernel mode) ?
- Con: microkernel builds **user-level services communication**
+ messages must be copied between services
+ process switching for messages exchange
4. **Modules**
1. **Loadable Kernel Modules (LKMs)**
- **object-oriented** approach <br>
2. Each core component is separate
3. One of most modern operating system structure
- Pro
* No need to invoke message passing for communication (Each talks to the others over known **communication interfaces**)
* Loadable and removable
- Con
* **Memory fragmentation**. Usually base kernel module is loaded into a continuous area of real memory. When loading an LKM, real memory maybe fragmented. However, the LKM needs a continuous area, so Linux allocate a continuous vitual memory, which is probably not continuous in real memory. System needs more TLB entries to locate LKM, which leads to a performance penalty because of more TLB miss.
>[color=#00ff00]
>
>**Memory fragmentation**.
>Loading LKMs may fragmentate real memory .
>~~Base kernel module require continuous area of real memory.~~
>
>Linux allocate a continuous vitual memory.
>More TLB entries are used to locate LKM, which leads to more possible TLB miss.
>[name=Omnom]
>[color=#00ff00]combination of Layered and Microkernels to some degree
>
>[name=Omnom]
5. **Hybrid Systems**
1. Mixture of different structures
2. Attempts to combine aspects and benefits of microkernel and monolithic kernel
3. One of most modern operating system structure
### OS Debugging
* OS generate **log files** containing error information
* dump file
- **core dump** file:
1. by application failure
2. capturing memory of the process
- **crash dump** file:
1. by OS failure
2. containing kernel memory
* Performance tuning
- trace listings of activities
- Profiling : periodic sampling of instruction pointer
* Kernighan’s Law: Debugging is twice as hard as writing the code
### System Boot
### Quiz
1. describe what is the system call
- Ans: System calls is a programming interface to the service made available by an OS.
2. There are six types of system calls. Please list at least four types of system calls.
3. Please list two models of interprocess communication
- Ans: 1. Message-passing 2. Shared-memory
4. Please explain how layered approach simplifies debugging and system verification.
- Ans: 能將錯誤阻隔在同一層,出現錯誤只要解決該層的Bug,每一層的錯誤改正後才會進入下一層。