# Chapter 13 – I/O Systems
:::danger
:::spoiler
[TOC]
:::
# Overview
- 計算機的兩個主要工作
- I/O and Computation
- ==I/O devices==
- tape,HD,mouse,joystick搖桿,network card, screen,flash disks,etc
- ==I/O subsystem==
- 控制所有I/O devices的方法
- Two conflicting trends
- 統一(unified)及標準的HW/SW interfaces
- I/O devices有很多種類
- Device drivers
- a uniform device-access ==interface to the I/O subsystem==
- 在OS最下層要有個driver layer
- 相似於apps與OS之間的==system calls==
- Device 分類 -- 不同類型去設計不同的interface
- Storage devices:disks, tapes
- Transmission devices:network cards, modems
- Human-interface devices:keyboard, screen, mouse
- Specialized devices:joystick, touchpad
# I/O Hardware
## Port
- A ==connection point== between I/O devices and the host
- E.g.:USB ports
## Bus
- 一組==wire==和定義好的==protocol==規定messages如何透過這些wire做傳送
- E.g.:PCI bus
## Controller
- A collection of electronics可以操控port,bus,device
- controller可以有他自己的==processor和memory==, etc. (E.g.: SCSI controller)
## Typical PC Bus Structure
- 北橋晶片控制比較快的controller,bw、freq高
- 南橋晶片接傳統ide,比較慢的

# I/O Methods
## Port-mapped I/O
- 每個I/O port (device)被指定一個獨特的==port address==,一開始就燒死與軟體無關
- 每個I/O port由==四個registers (1~4Bytes)組成==
- Data-in register:read by the host to get input
- Data-out register:written by the host to send output
- Status register:read by the host to check I/O status
- Control register:written by the host to control the device
- Program與I/O port的互動透過==special I/O instructions==
- i/o instruction裡頭一定有port address

## I/O Methods Categorization
- Depending on ==how to address a device==
- ==Port-mapped I/O==
- 與memory用不一樣的address space
- 透過special I/O instruction (e.g. IN, OUT)做存取
- ==Memory-mapped I/O==
- 保留特別的memory space給device
- 透過標準的data-transfer instruction (e.g. MOV)做存取
- 寫到memory等於寫到某個device的buffer,要事先register好
- 優點:對大量的memory I/O是更有效率的(e.g. graphic card)
- 缺點:Vulnerable to accidental modification, error
- Depending on ==how to interact with a device==
- ==Poll (busy-waiting)==:processor週期性的檢查device的status register
- ==Interrupt==:device通知processor I/O的完成
- 若是資料量大,選擇用interrupt
- Depending on ==who to control the transfer==
- ==Programmed I/O==:transfer由CPU控制
- CPU會一直接收到interrupt,速度變慢
- ==Direct memory access== (DMA) I/O:由DMA controller控制(一個有特別目的的controller)
- 由I/O controller負責搬資料,不屬於任何一個I/O device
- 設計給大量資料傳輸
- 通常搭配使用memory-mapped I/O和interrupt I/O method
- 
## Interrupt
### Interrupt Vector Table
Intel Pentium Processor

### Interrupt Prioritization
- Maskable interrupt
- 參照IRQ
- 透過設定中斷遮罩暫存器(interrupt mask register; IMR)內的位元遮罩(bit-mask)值來==讓它可被系統忽略==
- 比當前較低優先度的interrupt不被認定直到pending interrupt完成
- Non-maskable interrupt (NMI):highest-priority, never masked
- ==無法被系統忽略的==
- NMI通常使用於計時器(timers)
- Often used for power-down, memory error
### Interrupt-Driven I/O

## CPU and device Interrupt handshake
1. Device asserts ==interrupt request (IRQ)==
2. CPU在每個instruction cycle的開始檢查==interrupt request line==
3. ==Save== the status and address of ==interrupted process==
4. CPU認知道了interrupt,接著在interrupt vector table搜尋interrupt handler routines
5. CPU從==interrupt handler routine==抓下一個 instruction
6. 在完成執行interrupt handler routine後恢復interrupted process
## Note
- PnP(Plug and Play)技術可以協助解決 IRQ 線路衝突的問題
- PnP,簡化來說,就是讓周邊設備自己發出訊號去和所有其他支援 PnP 設計的周邊設備**協商,找到尚未被佔據的 IRQ 線路**
- USB 使得許多種不需要太快傳輸速度的周邊設備,例如:鍵盤、滑鼠、印表機、數據機、軟碟機、掃瞄機等等 (注意硬碟機不在此列,因為硬碟機需要越快越好的傳輸速度), 可以串聯在一起,而且共用一個 IRQ 線路
# Kernel I/O Subsystem
## I/O Subsystem
- I/O Scheduling
- 藉由對I/O queue中的job排順序提昇系統效能
- e.g. disk I/O order scheduling
- Buffering
- 當要在I/O devices之間做transfer,將data存到memory(e.g. disk to network card)
- 調節I/O devices之間的速度
- 原因
- devices之間的==速度不一樣==
- Devices with ==different data-transfer sizes==
- Support copy semantics
- 要做多次的data copy
- Caching – fast memory that holds copies of data
- 永遠只是個copy
- Key to ==performance==,增加access的速度
- Spooling – holds **output** for a device
- 先load到spooling的空間去,再一次送到printer
- e.g. printing (cannot accept interleaved files)
- Error handling – when I/O error happens
- e.g. SCSI devices returns error information
- I/O protection
- Privileged instructions
## UNIX I/O Kernel Data Structure
- Linux treats all I/O devices like a ==file==
- 
## Device-status Table

## Blocking and Nonblocking I/O
- Blocking - process被中斷直到I/O完成
- Easy to use and understand
- Insufficient for some needs
- Use for ==synchronous== communication & I/O
- Nonblocking
- 透過==multi-threading==實作
- Returns quickly with count of bytes read or written
- Use for ==asynchronous== communication & I/O
- e.g. call interrupt告訴你他做完了,有write, write call為一組
## Life Cycle of An I/O Request

# Performance
## I/O在系統效能上是一個主要的因素
### 原因
- 讓CPU執行==device driver code==
- 每call一層driver,每一個subsystem都是不同的process,需要==context switches==
- 因為有一堆layer,controllers、buffer、 cache有一堆的==data copy==
- ==Interrupt handling== is a relatively expensive task
- 當I/O的時間少,Busy-waiting可以比interrupt-driven表現的更以效率
## Intercomputer Communications
- Network traffic could cause high context switch rate
- Interrupt generated during keyboard & network I/O
- Context switch occurs between prog. & kernel (drivers)
- 
## Improving Performance
- 減少==context switches==的次數
- 減少==data copying==
- 藉由large transfers,smart controllers,polling減少==interrupts==
- Use ==DMA==,CPU可以空出來
- Balance CPU,memory,bus,and I/O performance for highest throughput
# Application Interface
## A Kernel I/O Structure
### Device drivers
- a uniform device-access ==interface to the I/O subsystem==
- ==hide the differences== among **device controllers** from the **I/O sub-system of OS**
- 
## Characteristics of I/O Devices

## I/O Device Class
- Device class is fairly standard across different OS
- Block I/O
- Char-stream I/O
- Memory-mapped file access
- Network sockets
- Clock & timer interfaces
- ==Back-door== interfaces (e.g. ioctl() ),難以歸類到以上的類別
- call system calll:ioctl()完後,可以去操作任何的I/O device
- 使application可以任意access被device driver實作函式,而不用透過system call
## Block & Char Devices
- Block devices:disk drives
- system calls:read( ), write( ), seek( ),一整個memory buffer的概念
- Memory-mapped file can be layered on top
- 大的,用interrupt做I/O
- Char-stream devices:mouse, keyboard, serial ports
- system calls:get( ), put( ),以bytes為單位
- Libraries layered on top allow line editing
- 小的,用pulling做I/O
## Network Devices
- 有很多種形式(block、character...),對於個別有自己的interface
- System call: send(), recv(), select()
- select() returns which socket is waiting to send or receive, eliminates the need of busy waiting
- Many other approaches
- pipes, FIFOS, STREAMS, message queues