<style>
.red {
color: red;
font-weight: bold;
}
.blue {
color: blue;
font-weight: bold;
}
</style>
# Chapter 1 - Introduction
- An **operating system** is software that manages a computer’s hardware.
- Provides a basis for application programs and acts as an intermediary between the computer **user** and the computer **hardware**.
- A fundamental responsibility of an OS is to allocate these **resources** to programs:
- CPU
- memory
- I/O devices
- storage
- <font class="blue">考點:電腦資源有哪些?</font>
## What Operating Systems Do
A computer system can be divided roughly into four
components:
- **hardware**
- provides the basic computing resources for the system
- **operating system**
- **application programs**
- define how to use these resources to solve user's problem
- **user**

### User view
The user’s view of the computer varies according to the interface being used, so the operating system is designed mostly for:
- **(主要目的)Ease of use**
- Performance
- Security
- (==不關心==)Resource utilization(資源利用率)
### System view
Two views:
- The operating system is the program **most intimately(密切地)involved with the hardware**. In this context, we can view an operating system as a **resource allocator**.
- Emphasizes the need to control the various I/O devices and user programs, viewed as a **control program**.
### Defining Operating Systems
- The operating system is the one program running at all times on the computer, usually called the **kernel**.
- Two other types of programs
- **System programs**
- associated with the operating system but are **not necessarily part** of the kernel
- Application programs
- include all programs not associated with the operation of the system
- Mobile operating systems often include not only a core kernel but also **middleware**(中介軟體,在作業系統與應用程式之間)
- a set of software frameworks that provide additional services to application developers
## Computer-System Organizing
- Bus
- 串接所有 device 的線路
- provides access between components and **shared memory**
- **Device controller**
- 跑在 device(硬體)上
- each device controller is in charge of a **specific** type of device
- moving the data between the **peripheral devices** that it controls and its **local buffer storage**
- **Device driver**
- 安裝在 OS 中,提供一個 interface,讓 OS 和 controller 溝通
- operating systems have a device driver for **each** device controller
- understands the device controller and provides the rest of the operating system with a **uniform interface** to the device
- <font class="blue">考點:比較 controller 跟 driver 的差別</font>

:::danger
**Device controller vs Device driver**
- **Similarities**
- Both facilitate communicate between computer system and hardware devices.
- Both enable the operating system to interact with specific hardware devices.
- Both play a crucial role in establishing communication between **software** and **hardware**.
- **Difference**
- *Device controller* is a **hardware** component, while *device driver* is a **software** component.
- *Device controller* manages the hardware device , while *device driver* provides a standardized interface for communicate.
- *Device controller* translates requests from the operating system, while *device driver* allows the operating system to communicate with the device.
:::
### Interrupts
The **controller** inform the **device driver** that is finished its operation.
#### Overview
- **Interrupt**
- 每個 interrupt 都有一個 number,用來分辨不同類型的 interrupt
- hardware may trigger an interrupt at any time by sending a signal to the CPU, usually by way of the system bus
- when the CPU is interrupted, it
1. stops what it is doing
2. immediately transfers execution to a fixed location (usually contains the **starting address of the service routine**)
3. the CPU resumes the interrupted computation when the service routine completed.
- **Service routine**
- ISR (Interrupt Service Routine)
- 根據不同硬體的中斷標記,執行事先設定好的一系列行為
- 可以想成每一個 ISR 都是一個 function
- 用來處理不同的 interrupt
- **Interrupt vector**
- 一個 table,裡面存有多個 ISR 的**起始位址**
- 根據不同中斷標記執行對應的 ISR
- 用 interrupt number 當作 index,一個 interrupt 直接對應一個 ISR
- Interrupt number 0 $\Rightarrow$ 執行 table 中第一個 element
- <font class="blue">考點: interrupt 時序圖</font>
<https://cihcih.medium.com/%E4%BD%9C%E6%A5%AD%E7%B3%BB%E7%B5%B1-interrupt-d2fced694da5>

#### Implementation
- **Interrupt-request line**
- a wire in CPU
- CPU senses it after executing every instruction
- when CPU detects controller's signal on the interrupt-request line, it jumps to the **interrupt-handler routine** by using the interrupt number in interrupt vector.
- **Interrupt-handler routine**
1. saves any state it will be changing during its operation
2. determines the cause of the interrupt
3. performs the necessary processing
4. performs a state restore
5. return the CPU to the execution state prior to the interrupt

- **Interrupt-controller hardware**
- In modern computer hardware, these three features are provided by the CPU and the interrupt-controller hardware.
- the ability to **defer interrupt handling** during critical processing
- an efficient way to **dispatch to the proper interrupt** handler for a device
- can distinguish between high- and low-priority interrupts and can respond with the appropriate degree of urgency for **multilevel interrupts**

- **Interrupt priority levels**
- Enable the CPU to **defer the handling of low-priority interrupts** and makes it possible for a high-priority interrupt to **preempt the execution** of a low-priority interrupt.
### Storage Structure
- **RAM** (random-access memory)
- Volatile(揮發性,斷電後資料消失)
- Example
- DRAM (dynamic random-access memory)
- SRAM (static random-access memory)
- **ROM** (read-only memory)
- PROM
- EPROM
- EEPROM
- **Bootstrap program**
- the first program to run on computer power-on
- wrote in ROM
- Firmware(韌體)
- Von Neumann architecture
- Instruction register
- Secondary storage
- Hard-disk (HDDs)
- Tertiary storage

(越往上越靠近 CPU,價格也越高)
- Semi-conductor memory
- Volatile storage
- memory
- Nonvalatile storage (NVS)
- Mechanical(機械式)
- Electrical(電子式)
- 如快閃記憶體
---
- Storage
- Byte (B)
- Kilobyte (KB) $= 10^3$
Kibibyte (KiB, **Ki**lo **bi**nary **byte**) $= 2^{10}$
- Megabyte (MB) $= 10^6$
Mebibyte (MiB) $= 2^{20}$
- Gigabyte (GB) $= 10^9$
Gibibyte (GiB) $= 2^{30}$
- Terabyte (TB) $10^{12}$
Tebibyte (TiB) $= 2^{40}$
- Petabyte (PB) $10^{15}$
Pebibyte (PiB) $2^{50}$
- <font class="blue">考點:單位轉換</font>
### I/O Structure
- Interrupt-driven I/O v.s DMA
- DMA (Direct Memory Access)
- The **device controller** transfers an **entire block** of data directly to or from its own **buffer** storage to **memory**.
- With no intervention by the CPU.
- One interrupt is generated per block.
- <font class="blue">考點:解釋 DMA</font>

註:DMA 還是有 CPU 介入,只是頻率較低
## Computer-System Architecture
### Single-Processor Systems
- Single processor
- Core
- General purpose
- Special purpose
- 工業電腦
### Multiprocessor Systems
- Multiprocessor systems
- Symmetric multiprocessing

- Multicore systems

- Shared system interconnect
- NUMA (Non-uniform memory access)
- 每個 core 存取同一塊 RAM 的時間可能不同
- interconnect 較慢

- Blade servers
### Clustered Systems
- Gathers together **multiple CPUs**.
- **Share** storage and are closely linked via local-area network (**LAN**) or a faster interconnect.
- Providing high availability services.
- Graceful degradation(優雅降級)
- Fault tolerant(容錯)
- Clustering can be structured as
- **Asymmetric clustering**
- **One machine** is in **hot-standby** mode while the other is running the applications
- The hot-standby host machine **does nothing** but **monitor** the active server
- **Symmetric clustering**
- Two or more hosts are **running applications** and **monitoring each other**.
- <font class="blue">考點:兩者差別</font>
- High performance computing (HPC)
- Parallelization
- Parallel clusters allow multiple hosts to access the same data on **shared storage**
- To provide the shared caacess, the system must apply access control and locking to ensure that no conflicting operations occur
- DLM (Distributed **lock** manager)
- **SAN** (storage area network)
- Allow many systems to attach a pool of storage

## Operating System Operators
- **Kernel**
- OS 中一直在運行的部分,負責 OS 的核心功能
- **System program**
- System daemons
- Interrupt driven OS
- **Interrupt**
- **Hardware interrupt**
- 滑鼠、鍵盤的信號輸入
- 周邊裝置向 CPU 發出的控制信號
- **Software generated interrupt**
- Trap (or Exception)
- Error (Exception)
- System call
### Multiprogramming and Multitasking
- Multiprogramming
- Process(執行中的程式)

- Multitasking
- Response time
### Dual-Mode and Multimode Operation
- User mode
- Kernel mode(以下同義)
- Supervisor mode
- System mode
- Previleged
---
- Mode bit

- Privileged instructions
- Multimode mode
- Protection rings([分級保護域](https://zh.wikipedia.org/wiki/分级保护域))

### Timer
- Timer is set to interrupt the computer after a specific period.
- Privileged instructions
- 在 Kernel mode 執行
- 每個 process (或是 thread) 最多只能連續執行一小段時間,保證 time-sharing
## Resource Management
Process needs certain resources to accomplish its task:
- CPU time
- Memory
- Files
- I/O devices
### Process Management
- **Program vs process**
- Process
- a program in execution is a process
- **Single-threaded** process has ***one*** **program counter** specifying the next instruction to execute.
- **Multi-threaded process** has **multiple program counters**, each pointing to the next instruction to execute for a given thread.
---
- The operating system is responsible for the following activities in connection with process management:
- **Creating** and **deleting** both user and system processes
- **Scheduling** processes and threads on the CPUs
- **Suspending** and **resuming** processes
- Providing mechanisms for process

### Memory Management
- For a program to be executed, it must be mapped to absolute addresses and loaded into the memory.
- To improve the utilization of the CPU and the speed of the computer's response time to its users
- Keep **several programs in memory**
- Creating a need for memory management
- The operating system is responsible for the following activities in connection with memory management:
- **Keeping track** of which parts of memory are currently being used and who is using them.
- **Allocating** and **de-allocating** memory space as needed.
- Deciding which processes (or parts thereof) and data to **move into and out of memory**.
### File System Management
- **File**
- The OS abstracts from the physical properties of its **storage devices** to define a logical storage unit, the file
- The operating system is responsible for the following activities in connection with file management:
- **Creating** and **deleting** files
- Creating and deleting **directories** to organizing files
- Supporting primitives for **manipulating** files and directories
- **Mapping** files onto secondary storage
- Backup files onto stable (non-volatile) storage media
### Mass Storage Management
- Main memory is too small to accommodate all data and programs, and because the data that it holds are lost when power is lost, the computer system must provide secondary storage to back up main memory.
- The operating system is responsible for the following activities in connection with disk management:
- Mounting(掛載)and un-mounting
- Free-space management
- Storage allocation
- Disk scheduling
- Partitioning
- Protection
### Caching Management
- Cache management

- size 越左越小
- access time 越左越短
- bandwidth 越左越大

- Cache coherence(快取一致性)
### I/O System Management
- I/O sub-system consists of several components
- A memory-management component that includes buffering, caching, and spooling.
- **SPOOL** (Simultaneous Peripheral Operation On-Line)
- A general device-driver interface
- Drivers for specific hardware devices
## Security and Protection
- Security
- Defend a system from external and internal attacks
- Protection
- Is any mechanism for controlling the access of processes or users to the resources defined by a computer system.
- User ID (UID)
- Group ID
## Virtualization
- Virtual machine
- Emulation
- when the source CPU type is different from the target CPU type

- Guest
- Host
- VMM (Virtual Machine Manager)
## Distributed Systems
- A network
- a communication path between two or more systems
- LAN (Local area network)
- WAN (Wide area network)
- MAN (Metropolitan area network)
- PAN (Personal area network)
- 考點:==應用==
---
- Network operating system
- An OS that provides features such as file sharing across the network
- along with a communication scheme that allows different computers to exchange messages
## Computing Environments
### Mobile Computing
- Mobile computing
- Refers to computing on handheld smartphones and tablet computers
- Google Android
- Apple iOS
### Client Server Computing
- Client systems
- Server systems
- Computer server system
- File server system
- Client server system

### Peer-to-peer Computing
Two general ways to determine what services are available:
- Register
- Lookup
- Discovery protocol

- VoIP (Voice over Internet Protocol)
考點:==比較 client server 跟 P2P==
### Cloud Computing
A type of computing that delivers computing, storage, and even applications as a service across a network
- Public cloud
- Private cloud
- Hybrid cloud
- SaaS (Software as a Service)
- PaaS (Platform as a Service)
- Iaas (Infrastructure as a Service)
- <https://cloud.google.com/learn/paas-vs-iaas-vs-saas?hl=zh-tw>

### Real-Time Embedded Systems
- A real time system has **well-defined**, fixed time constraints.
- Processing must be done within the define constraints, or the system will fail.
## Free and Open-Source Operating Systems
- GNU
- FSF (Free Software Foundation)
- GPL (GNU General Public License)
---
- Open source operating systems
- Available in source code format rather than as compiled binary code
- GNU/Linux
- BSD UNIX
- Solaris