Professional Documents
Culture Documents
Chapter 4 I/O and Driver: Operating Systems
Chapter 4 I/O and Driver: Operating Systems
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 2
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Overview
• Two fundamental operations performed by a computing system
– Processing of data (implement an algorithm)
– Perform I/O (move data into and out of the system)
• Large diversity in I/O devices, capabilities and performance
– Common Categories: storage, transmission and user-interface devices.
– Examples: display, keyboard, network, hard disks, tape drives, CDROM ...
• OS Goal is to
1. provide simple and consistent interface to user
2. optimize I/O use for maximum concurrency
• Mechanisms used by OS
– device drivers provide standard interface to I/O devices
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 3
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Categories of I/O Devices
• Human readable
– Used to communicate with the user
– Printers
– Video display terminals
• Display
• Keyboard
• Mouse
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 4
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Categories of I/O Devices
• Machine readable
– Used to communicate with electronic equipment
– Disk and tape drives
– Sensors
– Controllers
– Actuators
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 5
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Categories of I/O Devices
• Communication
– Used to communicate with remote devices
– Digital line drivers
– Modems
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 6
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Differences in I/O Devices
• Data rate
– May be differences of several orders of magnitude
between the data transfer rates
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 7
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
8
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Differences in I/O Devices
• Application
– Disk used to store files requires file management
software
– Disk used to store virtual memory pages needs special
hardware and software to support it
– Terminal used by system administrator may have a higher
priority
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 9
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Differences in I/O Devices
• Complexity of control
• Unit of transfer
– Data may be transferred as a stream of bytes for a
terminal or in larger blocks for a disk
• Data representation
– Encoding schemes
• Error conditions
– Devices respond to errors differently
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 10
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Performing I/O
• Programmed I/O
– Process is busy-waiting for the operation to complete
• Interrupt-driven I/O
– I/O command is issued
– Processor continues executing instructions
– I/O module sends an interrupt when done
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 11
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Performing I/O
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 12
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Relationship Among Techniques
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 13
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O and Devices
Bus
command status data 0
data 1
Device Controller
(firmware and logic) ...
Device X data N-1
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 14
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Hardware
• Common concepts
– Port (device-machine communication point)
– Bus (daisy chain or shared communication channel)
– Controller (host adapter)
• Common Techniques
1. Direct I/O with polling, aka Programmed I/O: Processor does
all the work. Poll for results.
2. Interrupt Driven I/O: Device notifies CPU when I/O operation
complete
3. Memory Mapped I/O: rather than reading/writing to
controller registers the device is mapped into the OS memory
space. increased efficiency
4. Direct memory access (DMA): DMA controller read and write
directly to memory, freeing the CPU to do other things.CPU
notified when DMA complete
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 15
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Programmed I/O
• Busy-wait cycle to wait for I/O from Insert Read
CPU I/O
device command to
I/O Module
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 16
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Interrupt-Driven I/O
Insert Read
command to CPU I/O
• Similar to direct I/O but processor I/O Module Do something
else
not required to poll device. Read Status
Interrupt
of I/O
• Interrupt asserted to notify Module I/O CPU
Ready
• Maskable to ignore or delay some Read word
I/O CPU
interrupts from I/O
Module
– Based on priority
No
– Some unmaskable Done?
Yes
Next Instruction
Interrupt mechanism also used for exceptions
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 17
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Direct Memory Access (DMA)
• I/O exchanges to memory
– Processor grants I/O module
authority to read from or write to
memory
Issue Read CPU DMA
– Relieves the processor from the task block command
to I/O module Do something
– Processor is free to do other things else
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 19
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Another way to Describe I/O Modes
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 20
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Programmed I/O (1)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 21
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Programmed I/O (2)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 22
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Interrupt-Driven I/O
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 23
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Using DMA
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 24
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Introduction to I/O
• https://www.youtube.com/watch?v=DYGrqN
BWymw&ab_channel=GnanaTeja
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 25
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Interfaces (Layers)
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 26
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Block and Character Devices
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 27
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Communication Endpoints
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 28
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Clocks and Timers
• Provide current time, elapsed time, timer
• Programmable interval time used for timings,
periodic interrupts
• ioctl (on UNIX) covers odd aspects of I/O such as
clocks and timers
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 29
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Blocking versus Nonblocking I/O
• Blocking - process suspended until I/O completed
– Easy to use and understand
– Insufficient for some needs
• Nonblocking - I/O call returns what is available
– User interface, data copy (buffered I/O)
– Implemented via multi-threading
– Returns quickly with count of bytes read or written
• Asynchronous - process runs while I/O executes
– Difficult to use
– I/O subsystem signals process when I/O completed
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 30
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Kernel I/O Subsystem
• Scheduling
– Efficiency, fairness, prioritized access
• Buffering - store data in memory while transferring between
devices
– To cope with device speed mismatch, transfer size mismatch and to maintain
“copy semantics”
• Caching - fast memory holding copy of data
– Always just a copy, Key to performance
• Spooling - hold output for a device
– Used when device can serve only one request at a time, i.e., Printing
• Device reservation - provides exclusive access to a device
– System calls for allocation and deallocation
– Watch out for deadlock
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 31
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Error Handling
• OS can recover from disk read, device unavailable,
transient write failures
• Most return an error number or code when I/O
request fails
• System error logs hold problem reports
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 32
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Intercomputer communications
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 33
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Improving Performance
• I/O a major factor in system performance
– Demands CPU to execute device driver, kernel I/O code
– Context switches due to interrupts
– Data copying
– Network traffic especially stressful
• 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
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 34
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O and registers
• The I/O ports of each device are actually structured into a
set of specialized registers
• CPU write commands to control registers
• CPU reads a value that represents the internal state of the
device from status register
• CPU fetch data from a device by reading input register
• CPU push data to device by writing bytes into output
registers Control register
input register
Output register
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 35
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
What is an I/O interface?
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 36
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Types of I/O interfaces
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 37
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Custom I/O interfaces
• Keyboard interface
• Graphic interface
• Disk interface
• Network interface
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 38
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
General –purpose I/O interface
• Parallel port
– traditionally connected to printers, but
can also be connected to removable
disks, scanners
• Serial port
– one bit at a time
– use UART chip (Universal Asynchronous
Receiver and Transmitter) to string out
the bytes into a sequence of bits.
• Universal serial bus (USB)
• PCMCIA interface
• SCSI (Small Computer System
Interface) Interface
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 39
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device Controller
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 40
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Principles of I/O Software
Goals of I/O Software (1)
• Device independence
– programs can access any I/O device
– without specifying device in advance
· (NIC, hard drive, or DVD)
• Uniform naming
– name of a file or device a string or an integer
– not depending on which machine
• Error handling
– handle as close to the hardware as possible
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 41
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Goals of I/O Software (2)
• Synchronous vs. asynchronous transfers
– blocked transfers vs. interrupt-driven
• Buffering
– data coming off a device cannot be stored in final
destination
• Sharable vs. dedicated devices
– disks are sharable
– tape drives would not be
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 42
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Software Layers
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 43
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device Drivers
• https://www.coursera.org/lecture/iot-
architecture/device-drivers-AL7YG
• Windows: Device Driver Development
Tutorial – YouTube
https://www.youtube.com/watch?v=jC0B2kSyKAI
• Linux: Learning Linux Device Drivers
Development : Find and Create Network
Drivers | packtpub.com - YouTube
https://www.youtube.com/watch?v=4Njw_VJUhZ4
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 45
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596