Operating System

Operating system
a program that acts between a user and computer hardware
一種System Software,主要管理Processor、Memory、Information、Device這四種資源

goal:
execute user programs
make the computer system convenient to use
use the hardware in an efficient manner

4 components of computer system
user
application programs
operating system
hardware

definition:
OS is a resource allocator
OS is a control program
OS is a interrupt driven

主要功能
控制system的作業流程
有效manage並善用系統資源
方便user計算機系統的使用

位置
通常OS是在bare machine上執行,而user program是在Extended Machine執行
Bare Machine(裸機)
 只有CPU, memory, I/O device等Hardware Components所組成marchine,沒有任何user可用的system program存在
 bare machine本身有一組Basic Instruction Set(基本指令集)及一套Operating System來管理並有效地使用系統資源
Extended Machine
 在Bare Machine上加上user可用的OS, Compiler, DBMS等System Programs所組成的marchine

……………………………………………………………………………………………………………

作業系統分類 

作業系統主要分為
單人單工
ex:MS-DOS
單人多工,需使用multitasking
ex:Win95、98、Me、2000 Pro、NT Workstation、Mac OS 8.6
多人多工,需使用multitasking
ex:Netware、Windows NT Server、Windows 2000 Server、UNIX、Linux

以時間分類system type
Batch System(批次系統):
 用來進行批次處理的作業系統 ,適合Long-Term Scheduler
Time-Sharing System(分時系統):
 將運算時間分成多份,並分給每個process,可達到multitasking功能, 主要以RR的cpu scheduling實作
 ps:
 需硬體設備支援,像是可定時發出interrupt的timer電路,可避免CPU被永遠佔據
Real-Time System(即時系統):
 對於process的完成時間有極為嚴格的要求
 可分為
  Hard Real-Time System:process須在規定的時間內完成
  Soft Real-Time System:有即時性的process具有較高的優先權


以數量分類system type
Tightly-Coupled System
 主要以Multiprocessing功能實作
Loosely-Coupled System/Distributed System
 可在分為兩種
 Client-Server mode
 Peer-to-Peer mode
Clustered System
 多個系統集結組成,共享相同的stroage device,目的在於提供High availability

ps:
常見OS功能
Multiprogramming
 讓多個process可以同時在memory中被CPU執行
 優點:避免CPU idle,提高CPU utilization
 說明:多個process會被載入到memory中,當某一個process進行I/O作業時,CPU就會切換到另一個process,使得多個process可同時被處理
 ps:多個process同時被處理可分為Concurrent(並行) mode,Parallel(平行) mode
Multitasking
 讓user可以同時執行多個process
 優點:response time(反應時間)快,適合用在user interactive
 說明:透過context switching的方式,輪流執行多個process
Multiprocessing
 多個CPU可同時執行一個或多個process
 優點:可提高計算機系統之可靠度及速度

……………………………………………………………………………………

OS is a interrupt driven

interrupt
某裝置要通知另一裝置,用來引起注意的技巧
屬於非同步發生的event,在任何時間都可以發生
ex:
當周邊設備有資料產生時,周邊設備會用interrupt的方式通知CPU,而CPU會先將目前的工作停下並儲存當時的狀態,然後處理周邊設備的要求,待處理完成後再繼續剛剛的工作

interrupt可分為
software interrupt/trap/exception:由軟體所產生的中斷
ex: system call、邏輯錯誤,不合法的記憶體存取,overflow
hardware interrupt/IRQ:由硬體產生的中斷
 可在分兩類
 external interrupt :CPU以外的周邊元件所產生的
 ex:I/O complete、I/O error、machine check
 internal interrupt:CPU本身所產生的
 ex:stack overflow、illegal command(非法指令執行)、divided by zero(除以0)…


Interrupt Handler(中斷處理)
一種table,可在interupt發生時,快速找到ISR
ps:通常都在記憶體開始的地方
ISR(Interrupt Service Routine,中斷服務常式)
處理interrupt event的程式
每個interrupt event會提供ISR去服務此interrupt
Interrupt vector(中斷向量)
由00H~FFH的數字,用來判斷interrupt的種類


Interrupt Step
1.OS暫停目前的process,並用context switching保存該process狀態
2.根據interrupt ID查詢interrupt vector,取出對應的ISR initial address
3.到ISR的initial address,執行該ISR
4.當ISR complete
5.OS恢復原先中斷前的process執行

……………………………………

I/O operations

CPU與I/O的運作方式
主要可分為三種
 Polling I/O (詢問式I/O)
 Interrupt I/O (中斷式I/O)
 DMA

Polling I/O
CPU會不斷問I/O Device的I/O運作結果,會浪費許多Polling Time
CPU在Process上花的時間不多,雖然CPU很忙碌,但對Process的執行進度無幫助,對CPU Throughput無益

Interrupt I/O
會透過interrupt的機制

DMA
DMA Controller負責Memory與I/O Device之間的資料傳輸,其過程不需CPU的參與
因CPU可用在Process上的時間更多,所以可增加CPU Utilization與Throughput
常用在高速的Block-Transfer I/O Device ex:Disk
ps:
DMA也會發出中斷,只是發出的時間點與Interrupt I/O不同

I/O Structure
主要有兩種
 Synchronous I/O:當I/O完成運作後才會將控制權交還給User Process
 Asynchronous I/O:立刻將控制權交還給User Process,不需等到I/O運作完成

Synchronous I/O
一個時間內只允許一個I/O請求
不允許平行I/O處理
ps:當I/O完成中斷產生時,OS就會知道是那一個device所發出
ps:process藉由busy waiting(loop)或特殊wait指令以達到等待的目的

Asynchronous I/O
一個時間內可允許多個I/O請求
system efficiency增加
ps:需對多個I/O處理同時追蹤,OS會使用Device status table記錄各種devices位址,使用狀況,I/O request之執行進度,…等


……………………………………


硬體保護

Dual Mode(雙重模式)
保護hardware重要的resources,把可能引起危害的一些機器指令設為priveleged instruction,避免user program執行這些指令對系統或其它user造成危害
ps
以前的CPU無mode的概念,任何一隻程式都可以破壞系統

透過hardware的支援提供2種狀態保護系統
Monitor mode
 也就是supervisor mode/system mode/kernel mode
 此mode可執行priveleged instruction
 主要是OS的system processes在執行,OS擁有系統控制權
 ex: ISR, system call, 對應的service routine
User mode 
 user program可以執行的狀態
 此mode無法執行priveleged instruction,否則會透過trap產生錯誤中斷,使OS強迫process中止
 ps:
 會讓CPU離開user mode 而進入kernel mode事件主要為intruput
 ex:hardware interrupt, system call, 非法之記憶體存取, 算數除0錯誤,

mode bit
判斷mode狀態
mode bit=0:monitor mode
mode bit=1:user mod

三種硬體保護
I/O Protection
 防止User Program直接用I/O device
Memory Protection
 防止User Program誤用memory space
CPU Protection
 防止User Program無限期佔用CPU


……………………………………..

management of OS 

process management of OS
一個process是一個在執行的program
OS有很多process在運作
process內含thread,可以在分為
 single-threaded process
 multi-threaded process
activities of process management
 creating,deleting,suspending,resuming process
 process synchronization
 process communication
 deadlock handling

memory management of OS
target:
 1. determines what is memory
 2. determines when optimizing CPU utilization
activities of memory management
 keeping track: 追蹤那一區塊的memory誰在用
 move into and out:決定那個process或資料要從memory移進或移出
 allocating and deallocating:依需求分配memory 

storage management of OS
activities of file management
 creating and deleting files and directories
 manipulate files and directories
 mapping files onto secondary storage
activities of mass-storage management
 free-space management
 storage allocation
 disk scheduling