# Computer-System Organization [TOC] * - [ ] polling IO 圖 - [ ] Interrupt IO 圖 ## I/O 運作方式 ### Polling I/O(詢問式 I/O) * 定義:又稱 Busy Waiting I/O 或 Programmed I/O * 步驟如下:~users~ ~process~ ~→~ ~kernel~ ~→~ ~kernel~ ~I/O~ ~subsystem~ ~→~ ~device~ ~driver~ ~→~ ~device~ ~controller~ ~→~ ~Device~ 1. 執行中的 process 對 OS 發出 I/O request 2. 執行中的 process 會 blocked,wait for I/O complete ~(不一定)~ 3. OS 取得 CPU 執行相關的 system call,以完成 I/O service 4. 即 CPU 會設定 I/O command 給 device controller 5. Device controller 控制 I/O device 進行 real I/O operation 6. CPU 可以切給其他 process 使用 > **CPU 並未全部用於 process execution 上**:在執行其他 process 的過程中確耗費大量的 CPU time 用來 polling I/O device controller 的資訊,以確認剛交付之 I/O 運作完成與否、有無 error 等 > > 所以 **CPU utilization ,process throughput 不高** > ### Interrupted I/O(中斷式 I/O) * 步驟: 1~5 同前述 polling I/O 6. CPU 切給另一個 process 執行且 IO device 也正在執行 I/O 運作 7. 當 I/O 運作完成, I/O device controller 會發出 "I/O complete" interrupt 通知 OS ~(凡是通知~ ~OS~ ~的動作都叫~ ~interrupt)~ 8. OS 收到 interrupt 後,會先暫停目前 process 的執行~(不一定,要看中斷優先權)~,保存其狀態 9. OS 會查詢 interrput table 來判定何種中斷發生,並找出對應 interrupt service routine(ISR,或 interrupt handler) 的位址 10. Jump to ISR 11. 待 ISR 完成後,return control to kernel,kernel 通知 wait process 其 I/O 完成 12. OS 恢復中斷之前的 process 執行(或交由 CPU scheduler 決定下一個執行的 process) * 優點: * CPU 無須耗費大量的時間用於 polling I/O device controller,而是**可專注用在 process execution 上**,所以 CPU utilization 較高,產能提昇,improve the system performance * 缺點: 1. Interrupt 的處理也是必須耗費 CPU time 的~(查表、ISR~ ~執行~ ~等)~,若 I/O 運作時間不長,則 polling I/O 也許有利 2. 若 interrupt 發生的頻率極大頻繁,則 CPU utilization 會很低,system performance 會很差 3. ~(為~ ~DMA~ ~鋪路)~CPU 仍須用於 data 在 I/O device 與 memory 的傳輸,過程監督 > 所以 CPU time 此時未用在 process execution 上 ### DMA * 定義:DMA controller 負責 I/O device 與 memory 之間的資料傳輸,**其過程不須 CPU 參與監督** * 優點: 1. CPU 有更多時間用於 process execution 上,CPU utilization 相對更高。此外,適用於高速 Block-Transfer oriented 的 IO device ## Interrupt 機制的處理程序與種類 ###### tags: `OS` `共筆`