---
# System prepended metadata

title: OS Chapter1
tags: [作業系統]

---

# OS Chapter1
###### tags: `作業系統`

## *Computer System Structure*
:::success
1. Users

2. Aplication programs
    
3. Operating System
    - controls and coordinates use of hardware among applications and users
    - acting as a intermediary between a user and hardware.
4. Hardware
:::

:::info
![](https://i.imgur.com/iysQ3a3.png)
:::

## *What OS do*
:::success
:arrow_forward:OS is a resource allocator(分配) and control program
:::
## *Operation System Definition*
:::success
- **kernel** : 處理軟體I/O，將這些要求**轉譯**為指令並交由中央處理器（CPU）處理

- middleware : a set of software frameworks that provide addition services to application developers.
:::

## *Computer System Organization*
:::success
CPU , device controllers connect through bus / hub providing access to shared memory
![](https://i.imgur.com/ezE4V73.png)
:::

:::info
**Clustered System** sharing storage via storage-area network(SAN)
- example : 光纖、磁碟陣列
:::

## *Computer-System Operation*
:::success
- 每一個device controller類型都有對應的os device driver去管理
- device controller透過interrupt(trap)告知CPU已完成工作(operation)

:::

## *Common functions of interrputs*
:::success
- an os is interrupt driven

- 每一個硬體都有自己的**interrupt service routine(ISR)** , 當中斷產生之後控制權會交由ISR，再透過interrupt vector可以得知中斷後要往哪邊做什麼事

- two software-generated interrupt : 
    1. **trap** : 向系統請求的 (e.g. device controller已完成任務，透過trap告知CPU已完成)
    2. **exception** : 意外(也是最常處理的) (e.g. 除以0)
:::

## *Interrupt Handling*
:::success
當interrupt發生的時候，CPU將被打斷的狀況儲存在暫存器或program counter中
:::

:::info
determines which type of interrupt has occurred : 
1. polling interrupt system
2. vectored interrupt system
:::

## *I/O sturcture*
:::success
After I/O starts , control return to user program only upon I/O completion.
e.g. wait loop
:::

:::info
After I/O starts , control returns to user program without waiting for I/O completion.
    - **System call** : request to OS to allow user to wait for I/O completion
    - **Device-status table** : contains entry for each I/O device indicating its type , address , and state.
:::

## *Storage structure*
:::success
*Main memory*
- definition : 
    only large storage media that CPU can access directly
    
- 特色 : 
    - random access
    - volatile

- 舉例 :
    - random-access memory in the form of Dynamic Random-access Memory(DRAM)
:::

:::info
*Secondary storage*
- definition
    extension of main memory that provide large nonvolatile storage capacity

- 舉例 : 
    - Hard Disk Drives(HDD)
    - Non-volatile memory(NVM) 
:::

:::warning
![](https://i.imgur.com/Y6ibZek.png)
:::

## *How a modern computer works*
:::success
![](https://i.imgur.com/KH8Gogb.png)

:skull: instuction execution : instruction fetch
:skull: data movement : load & stall
:skull: **Direct Memory Access(DMA)** : 直接傳送資料到memory不需要CPU介入
:::

## *Computer-System Architecture*
:::success
*Single general-purpose processor*
:arrow_forward: most systems using
:arrow_forward: using bus
:::

:::info
*Multiprocessors*
:arrow_forward: parallel systems , tightly-coupled systems
:arrow_forward: using bus
- 優點 : 
    - Increased throughput
    - Economy of scale
    - increased reliability

- type
    1. Asymmetric Multiprocessing : each processor is assigned a special task
    2. Symmetric Multiprocessing : each processor performs all tasks
:::

:::warning
*Clustered System*
:arrow_forward: multiple systems works together , sharing storage via storage-area network(SAN)

- 優點 : 
    - provides a high-availability service 

- type : 
    - Asymmetric clustering : has one machine in hot-stanby mode (透過熱備份，避免電線過熱等原因而使工作中斷)
    - Symmetric clustering : multiple nodes running applications , monitoring each other
:::

## *Operating-System Operation*
:::success
- firmware(韌體) : 初始各方面的系統，載入OS kernel及開始執行
- kernel載入 :arrow_right: start system daemons(系統服務)
:::

## *Multiprogramming and Multitasking*
:::success
*Multiprogramming*
- batch system(批次執行)

- one job selected and run via job scheduling

- When it has to wait(e.g. I/O) , OS switches to another job

- 要主動退出工作權才會交換
:::

:::info
*Timesharing (Multitasking)*
- if several jobs ready to run at the same time :arrow_right: CPU scheduling

- if processes don't fit in memory , swapping moves them in and out to run

- Virtual memory allows execution of process not fit completely in memory
:::

## *Dual-mode*
:::success
- 優點 : allows OS to protect itself and other system components

- User mode and kernel mode

- mode bit
    - 由硬體提供，用於判斷現在是user mode還是kernel mode
    
    - 有些指令被設為特權，只有kernel mode的時候可以使用
    
    - using system call to change user mode to kernel mode , return from call resets it to user
:::

## *Protection and Security*
:::success
*Privilege escalation*
- 優點 : 允許使用者能變更更有權限的帳號
:::

## *Virtualization*
:::success
- definition : allows OS to run application within other OSes

- Emulation : used when source CPU type different from target type

- type
    - machine-level virtualization
        - provides virtualization services
        - e.g. VMM , Hypervisor

    - Operating-system-level virtualization
        - containerization
:::
