--- title: Ch1:Introduction --- # 作業系統Ch1: Introduction ###### tags: `Operating System` `Review` `Ch1` ## Computer System computer system 分成四個 components * Users: 人、機器、其他電腦 * Application: 定義系統資源用來解決電腦問題的方法 * Operating System: 控制 (controls) 和協調 (coordinates) 硬體/資源的使用 * Hardware: 提供基本運算資源。(CPU、memory、I/O devices) 作業系統比起控制(給資源),更重要在於協調(因為作業系統中絕對不只有一個程式同一時間在跑)。 ![](https://i.imgur.com/jLsjtAd.png) ### What is an Operating System? 作業系統是一個永久軟體(載入記憶體中)對於使用者應用程式控制 (controls) / 抽象化 (abstracts) 硬體資源。 抽象化比較接近 control 那一塊。 ![](https://i.imgur.com/ognlx4z.png) ### Multi-tasking Operating Systems 透過 OS 提供的 API,不同 User Applications 可以 go through OS,這時候 OS 就可以分配、實現這些動作 (instructions),也就是在一個 time slot 要給哪一隻程式(如有兩隻程式都要做這個指令),在做 coordinate 的動作。 ![](https://i.imgur.com/5MVzKax.png) ### General-Purpose Operating Systems 碰到不同的 I/O ,需要不同的 driver 去控制 (part of OS)。 OS 就是提供多樣 API ,也就是在圖中的 System library (call) 內。 透過 Compiler 去產生 machine code(.o file),接著 linker 連接 System library (人家寫好,拿來用的),最後才成為真的能執行的執行檔。 也有可能是先連接到 C library ,而 C library 中需要 System library,所以間接的連接到 System library。 ![](https://i.imgur.com/5DM7JBJ.png) System API 是一組介面,允許 user applications 和作業系統溝通,而無需了解底層實作細節。 API 為 general-purpose 而設計,而非效率驅動為導向的。 ### Definition of an Operating System * 沒有普遍接受的定義 * Resource allocator: 管理和分配資源去確保效率和公平。 * Control program: 控制使用者程式的執行和 I/O device 的操作來避免錯誤和電腦不正確使用 * Kernel: 一隻總是在 running 的程式。 ### Goals of an Operating System * Convenience: 使電腦易於使用和計算。 * Efficiency: 以有效率的方式使用電腦硬體 兩個目標經常是背道而馳的,例如大型電腦傾向於不用 GUI,節省資源達到更好效率,但是不方便使用;小型電腦傾向於安裝 GUI,給使用者帶來方便,但是增加正在跑的程式,效率降低。 ## Computer System Organization CPUs 和 device controllers 連接透過可提供 access 到共享記憶體的 common bus Goal: **Concurrent** execution of CPUs and devices 競爭 memory cycles。 ### Computer System Operations CPU 要對 Device 做動作 (e.g.,讀寫資料),CPU 必須先寫入 buffer,因為 I/O device 很慢,如果沒有 buffer 會造成 CPU idle。 * Status Reg: 有無進行I/O,沒有就是idle。 * Data Reg: 透過Data Reg寫入Buffer資料。 * Device Controller: 本身有簡易 CPU,可以去控制 I/O 動作,所以 CPU 不需要額外下指令。 * Memory: 因為 Memory 是 CPU 在使用的,所以後面這塊 Bus 的 Memory 一定要透過 CPU 下指令。 ![](https://i.imgur.com/BxZlsrd.png) Busy/wait output ```c= #define OUT_CHAR 0x1000 // device data register #define OUT_STATUS 0x1001 // device status register //要寫入到I/O devices 但是buffer有限,必須要清空才能再寫 current_char = mystring; while (*current_char != '\0') { //結尾字元 poke(OUT_CHAR,*current_char); //poke就是寫到buffer,然後寫出到 I/O device。 // I/O 速度比 CPU 慢很多,所以必須要檢查 Buffer 是否滿了,如果滿了的話, // 等到 buffer 寫到 I/O device 上,清空後再寫下一批。 while (peek(OUT_STATUS) != 0); // busy waiting current_char++; } ``` Busy/wait 非常缺乏效率,因為當 CPU 正在檢查 device 時,它不能做其他工作 ;難以同時進行 I/O (非real-time) ### Interrupt I/O Interrupt 允許 device 改變 CPU 執行 (control) 的 flow * 事件驅動 當傳輸結束時 (可能是 buffer 滿了),發出一個 interrupt 把 CPU 打斷,CPU 直接切換到 subroutine (可能是 OS 提供的一隻小程式)去處理搬資料這件事情,搬完之後又可以回去處理原本程式。 ![](https://i.imgur.com/cfM52Hu.png) > When the CPU is interrupted, it stops what it is doing and immediately transfers execution to a fixed location. The fixed location usually contains the starting address where the service routine for the interrupt is located. The interrupt service routine executes; on completion, the CPU resumes the interrupted computation. 1. device driver 會知道是採用 interrupt 的方式,initiate I/O。 2. CPU 會下指令給 Controller,讓 Controller 搬移資料(給予要搬的資料長度)。 3. 在資料搬移期間不需要找 CPU,而是完成後通知 CPU,產生 interrupt signal,告訴 CPU 這資料在 Memory 某一位置。 4. CPU 收到 interrupt,打斷正在執行的程式(B),可能切換到另外一隻程式 (I/O 動作的 A),看 scheduler 安排。 5. interrupt handler 會 make sure 剛剛 I/O 動作做完,讓 A 狀態成為像是剛做完 I/O 的狀態,這樣 A 跟 B 都變成可以執行。 6. 恢復,處理被打斷的工作。 7. 做完之後,再繼續執行下一個 I/O 的動作。 ![](https://i.imgur.com/b9ay50x.png) ### Interrupt 所有作業系統都是 interrupt driven * Hardware may trigger an interrupt at any time by sending a signal to CPU 任何 device 產生的 interrupt,只要不是使用者程式指令所產生的,稱作 **signal**。 * Software may trigger an interrupt either by an error (division by zero or invalid memory access) or by a user request for an operating system service (system call) 當 OS 偵測到 error 時,開始送一個 interrupt,處理 error 狀況,至少 reset 到可以有 messaging 的地方,不會繼續執行下去,因此也不會讓系統到 crash。 呼叫 system call,全部透過 interrupt 方式,OS 接受後會去看對應到哪一個 subroutine,再去 handle。 Software interrupt 稱作 **trap**。 **HW Interrupt** 被動 * Interrupt vector 是一個 array ,其實就是一個 array of function pointers。因為每一個 function call 的大小不同,所以把 define 好的 signal 給予每一個一個欄位,裡面只是 4 bytes 的 function pointer。 * Signal 裡面會有 number,這樣就可以知道是哪一個 function call 要去 handle。 * 處理 function call 的 service 稱作 service routine。 ![](https://i.imgur.com/96B8geQ.png) **SW Interrupt** 主動 * software 是用 switch-case ,因為軟體是無限可能性,取決於作業系統 define ,所以這些 call 的數量是 unbounded * 流程基本上與 HW 相同 ![](https://i.imgur.com/HAOfPMj.png) - **不論是 SW/HW,Interrupt architecture 必須要記住被打斷的指令位址。** - **Incoming interrupts are *disabled* while another interrupt is being processed to prevent a lost interrupt** 不禁用的話,一直中斷也會有 overhead 過大。 確保 OS 速度可以夠快,跳開 Synchronization 問題。 ## Storage-Device Hierarchy 最傳統、標準的階層 ![](https://i.imgur.com/lAW4pOd.png) 依 speed、Cost、Volatility 分層 - Main memory – only large storage media that the CPU can access directly - Secondary storage – extension of main memory that provides large nonvolatile storage capacity RAM 分 DRAM、SRAM。 | | DRAM | SRAM | | -------- | -------- | -------- | | need| one transistor| six transistors| |Consume |less power|more power| |value| periodically refreshed| x| |access speed|>= 30ns|10ns~30ns| ### Disk Mechanism - Transfer time = data size / transfer rate - Positioning time (random access time) - seek time (cylinder) + rotational latency (sector) ![](https://i.imgur.com/BOqCwn4.png) 當資料是 Sequential 時,HDD 並不會輸給 SSD 太多。 ### Caching - Information in use **copied** from slower to faster storage temporarily 用到頻率越高會 cache 到越上層。 - Faster storage (cache) checked first to determine if information is there - 如果有,直接從 cache 使用 - 如果沒有,資料複製到 cache 而且 used there ### Coherency and Consistency Issue (不一致性的問題) - The same data may appear in different levels 最上層和下層可能值會因為改變而產生不一致。 - Single task accessing: 沒差 - Multi-task accessing: 有問題,需要獲得最新的值。 - Distributed system: 問題更大。 ## Hardware Protection ### Dual-Mode Operation - **What to protect?** - Sharing system resources requires OS to ensure that an incorrect program *cannot cause other programs* to execute incorrectly. - 提供 hardware support 區分至少兩種 modes of operations. 1. User mode – execution done on behalf of a user 2. Monitor mode (also kernel mode or system mode) - execution done on behalf of **operating system** (其實是使用者透過 System call 叫 OS 去做) - Mode bit 加入電腦硬體去指明現在 mode: kernel (0) or user (1) - When an interrupt/trap or fault occurs, hardware switches to monitor mode. ![](https://i.imgur.com/bcqQBXU.png) - Privileged instructions (只要會影響到其他人的指令就是特權指令) - Executed only in monitor mode - Requested by users (system calls) 只能透過 System call 去做,送到 CPU 會去 Check mode bit,一看到是 user mode,不是 kernel mode,就會告訴 OS,會被擋掉產生 segmentation fault。 ### I/O Protection - **All I/O instructions** are privileged instructions - 確保一個使用者程式在 monitor mode 時下不能獲得電腦控制權。也就是,作為使用者程式執行的一部份,使用者程式在 interrupt vector 中「不能」儲存新 address,否則在 interrupt vector 儲存新值(實際上是 a function pointer point to a function),可能就有指向到 malicious code (user code) 的風險。 - 資安 issues - e.g., buffer overflow。 ### Memory Protection * Protect - Interrupt vector and the interrupt service routines - 資料存取和從其他程式的 overwrite * HW support: - Base register: 擁有最小合法物理記憶體位址 - Limit register: 包含範圍的 size。 * 在這段範圍記憶體外的空間是受保護的。 ![](https://i.imgur.com/UrLruf0.png) ### Hardware Address Protection ![](https://i.imgur.com/ap299G6.png) ### CPU Protection - 避免使用者程式霸佔 CPU - getting stuck in an infinite loop - not calling system services - HW support: **Timer** —interrupts computer after specified period - Timer 隨時脈週期遞減 - 當 timer 值為 0 時,interrupt 發生 - Timer commonly used to implement **time sharing** - **Load-timer** is a privileged instruction. (改變Timer長度) ## Ref [1] [上課影片 link](https://www.youtube.com/playlist?list=PLS0SUwlYe8czigQPzgJTH2rJtwm0LXvDX) [2] [投影片 link](https://ocw.nthu.edu.tw/ocw/index.php?page=course_news_content&cid=141&id=999)