Operating System:
Chap13 I/O Systems
National Tsing-Hua University
2016, Fall Semester
Outline
Overview
I/O Hardware
I/O Methods
Kernel I/O Subsystem
Performance
Application Interface
Chapter13 I/O Systems
Overview
The two main jobs of a computer
I/O
andComputation
I/O devices : tape, HD, mouse, joystick, network card, screen, flash disks, etc
I/O subsystem : the methods to control all I/O devices
Two conflicting trends
Standardization of HW/SW interfaces
Board variety of I/O devices
Operating System Concepts – NTHU LSA Lab 3
Overview
Device drivers : a uniform device-access interface to the I/O subsystem
Similar to system calls between apps and OS
Device categories
Storage devices: disks, tapes
Transmission devices: network cards, modems
Human-interface devices: keyboard, screen, mouse
Specialized devices: joystick, touchpad
Chapter13 I/O Systems
I/O Hardware
Port : A connection point between I/O devices and the host
E.g.: USB ports
Bus : A set of wires and a well-defined protocol that specifies messages sent over the wires
E.g.: PCI bus
Controller : A collection of electronics that can operate a port, a bus, or a device
A controller could have its own processor, memory, etc. (E.g.: SCSI controller)
Operating System Concepts – NTHU LSA Lab 5
Typical PC Bus Structure
Chapter13 I/O Systems
Basic I/O Method (Port-mapped I/O)
Each I/O port (device) is identified by a unique port address
Each I/O port consists of four 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 interact with an I/O port through
special I/O instructions
(different from mem. access) X86: IN, OUT
Operating System Concepts – NTHU LSA Lab 7
Device I/O Port Locations on PCs (partial)
Chapter13 I/O Systems
I/O Methods Categorization
Depending on how to address a device:
Port-mapped I/O
Use different address space from memory
Access by special I/O instruction (e.g. IN, OUT)
Memory-mapped I/O
Reserve specific memory space for device
Access by standard data-transfer instruction (e.g. MOV)
More efficient for large memory I/O (e.g. graphic card)
Vulnerable to accidental modification, error
Operating System Concepts – NTHU LSA Lab 9
I/O Methods Categorization
Depending on how to interact with a device:
Poll (busy-waiting): processor periodically check status register of a device
Interrupt: device notify processor of its completion
Depending on who to control the transfer:
Programmed I/O: transfer controlled by CPU
Direct memory access (DMA) I/O: controlled by DMA controller (a special purpose controller)
Design for large data transfer
Commonly used with memory-mapped I/O and interrupt I/O method
Chapter13 I/O Systems
Six-Step Process to Perform DMA (Direct Memory Access)
Operating System Concepts – NTHU LSA Lab 11
Review Slides ( I )
Definition of I/O port? Bus? Controller?
I/O device and CPU communication?
Port-mapped vs. Memory-mapped
Poll vs. Interrupt
Programmed I/O vs. DMA
Steps to handle an interrupt I/O and DMA
request?
Chapter13 I/O Systems Operating System Concepts - NTHU EOS Lab 13
Kernel I/O Subsystem
I/O Subsystem
I/O Scheduling – improve system performance by ordering the jobs in I/O queue
e.g. disk I/O order scheduling
Buffering – store data in memory while transferring between I/O devices
Speed mismatch between devices
Devices with different data-transfer sizes
Support copy semantics
Chapter13 I/O Systems
I/O Subsystem
Caching – fast memory that holds copies of data
Always just a copy
Key to performance
Spooling – holds output for a device
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
Operating System Concepts – NTHU LSA Lab 15
UNIX I/O Kernel Data Structure
Linux treats all I/O devices like a file
Chapter13 I/O Systems
Device-status Table
Operating System Concepts – NTHU LSA Lab 17
Blocking and Nonblocking I/O
Blocking - process suspended until I/O completed
Easy to use and understand
Insufficient for some needs
Use for synchronous communication & I/O
Nonblocking
Implemented via multi-threading
Returns quickly with count of bytes read or written
Use for asynchronous communication & I/O
Chapter13 I/O Systems
Life Cycle of An I/O Request
Operating System Concepts – NTHU LSA Lab 19
In buffer Physical I/O
Data transfer Check buffer
cache
Move process from run queue to wait queue
Allocate kernel buffer Schedule I/O
Performance
I/O is a major factor in system performance
It places heavy demands on the CPU to execute device driver code
The resulting context switches stress the CPU and its hardware caches
I/O loads down the memory bus during data copy between controllers and physical memory, …
Interrupt handling is a relatively expensive task
Busy-waiting could be more efficient than interrupt- driven if I/O time is small
Chapter13 I/O Systems
Improving Performance
Reduce number of context switches
Reduce data copying
Reduce interrupts by using large transfers, smart controllers, polling
Use DMA
Balance CPU, memory, bus, and I/O performance for highest throughput
Operating System Concepts – NTHU LSA Lab 21
Review Slides ( II )
What are the key I/O services
Scheduling
Cache
Buffering
Spooling
Error handling
I/ protection
How to improve system performance?
Chapter13 I/O Systems Operating System Concepts - NTHU EOS Lab 23
Application I/O 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
Chapter13 I/O Systems
Characteristics of I/O Devices
Operating System Concepts – NTHU LSA Lab 25
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() )
Enable an application to access any functionality
implemented by a device driver without the need to invent a new system call
Chapter13 I/O Systems
Block & Char Devices
Block devices: disk drives
system calls: read( ), write( ), seek( )
Memory-mapped file can be layered on top
Char-stream devices: mouse, keyboard, serial ports
system calls: get( ), put( )
Libraries layered on top allow line editing
Operating System Concepts – NTHU LSA Lab 27
Network Devices
Varying enough from block and character to have own 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
Chapter13 I/O Systems
Reading Material & HW
13.1 – 13.6
Problem Set
13.2
13.5
13.6
13.8
Operating System Concepts – NTHU LSA Lab 29
Interrupt Vector Table
Intel Pentium Processor:
Chapter13 I/O Systems
CPU and device Interrupt handshake
1. Device asserts interrupt request (IRQ)
2. CPU checks the interrupt request line at the beginning of each instruction cycle
3. Save the status and address of interrupted process
4. CPU acknowledges the interrupt and search the
interrupt vector table for interrupt handler routines
5. CPU fetches the next instruction from the interrupt handler routine
6. Restore interrupted process after executing interrupt handler routine
Operating System Concepts – NTHU LSA Lab 31
Interrupt Prioritization
Maskable interrupt: interrupt with
priority lower than current priority is not recognized until pending interrupt is
complete
Non-maskable interrupt (NMI): highest- priority, never masked
Often used for power-down, memory error
Chapter13 I/O Systems Operating System Concepts – NTHU LSA Lab 33
Interrupt-Driven I/O
CPU executing checks for interrupts between instructions
CPU Controller
initiates I/O 2
input ready, output complete, or error Generates interrupt signal
3 CPU receiving interrupt,
transfers control to Interrupt handler
4
interrupt handler processes data, returns from interrupt
5
CPU resumes processing of interrupted task
6 device driver
initiates I/O 1
7
Summary of Services in I/O Subsystem
The management of the name space for files and devices
Access control to files and devices
Operation control
File system space allocation
Disk allocation
Buffering, caching, and spooling
I/O scheduling
Device status monitoring, error handling, and failure recovery
Device driver configuration and initialization
Chapter13 I/O Systems
I/O Requests to Hardware Operations
Consider reading a file from disk for a process
Determine device holding file
Translate name to device representation
Physically read data from disk into buffer
Make data available to requesting process
Return control to process
Operating System Concepts – NTHU LSA Lab 35
Layered File System revisited
Logical File System Application Programs
File-Organization Module
Basic File System
I/O Control
Devices
Given a symbolic file name, use directory to provide values needed by FOM
Transform logical address to physical block address Numeric disk address
Driver: hardware instructions write(data_file, item)
file name
information of data_file
2144th block
driver 1, cylinder 73, surface 2, sector 10
Chapter13 I/O Systems
Intercomputer Communications
Network traffic could cause high context switch rate
Interrupt generated during keyboard & network I/O
Context switch occurs between prog. & kernel (drivers)
Operating System Concepts – NTHU LSA Lab 37
keyboard
e.g. telnet
e.g. telnet session
STREAMS
A full-duplex communication channel
between a user-level process and a device
STREAM provides a framework for a modular
and incremental approach to writing device
drivers and network protocols
Chapter13 I/O Systems
The STREAM Structure
A STREAM consists of
STREAM head interfaces with user process
Driver end interfaces with the device
zero or more STREAM modules between them
Each module contains a read and a write queue
Message passing is used to communicate between
queues
Operating System Concepts – NTHU LSA Lab 39