Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Operating Systems

Chapter 4 I/O and Driver

Tien Pham Van, Dr. rer. nat.

Compiled with reference to other presentations


Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 1
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Concept

• Communication between an information processing


system (computer) and peripherals, or another
processing system
• It may refer to input/output operations for data
exchange

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

• Direct Memory Access (DMA)


– DMA module controls exchange of data between main
memory and the I/O device
– Processor interrupted only after entire block has been
transferred

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

Processor Device table


dispatcher Device Driver
X
(interrupt handler)

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

• Poll at select times: periodic, Read Status


entering/leaving kernel etc. of I/O
Module
I/O CPU

– Determines state of device: command- Not


Ready
ready, busy, error Check Error
Condition
Status
• Processor transfer data to/from Ready

device. Read word


from I/O I/O CPU
Module
• Read/write directly to status and
command registers Write word
into memory CPU Memory

• Processor polls device for status


• Consumes a lot of processor time No
Done?

because every word read or written Yes


passes through the processor Next Instruction

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

processor of a change in status Error


Check
• Interrupt handler receives interrupts Status Condition

Ready
• Maskable to ignore or delay some Read word
I/O CPU
interrupts from I/O
Module

• Interrupt vector to dispatch interrupt Write word


to correct handler into memory CPU Memory

– 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

• An interrupt is sent when the task


Read status
is complete of DMA Interrupt
module
• The processor is only involved at DMA CPU

the beginning and end of the


transfer Next Instruction

• Used to avoid programmed I/O for


large data movement
• Requires DMA controller
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 18
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Six steps to perform DMA transfer

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

• Synchronous (Programmed I/O) - control returns to


user program only upon I/O completion.
– Idle CPU until the next interrupt
– wait loop (contention for memory access).
• Asynchronous (Interrupt driven I/O) - control
returns to user program before I/O completion.
– System call – request to the operating system to allow
user to wait for I/O completion.
– Device-status table contains entry for each I/O device
indicating its type, address, and state.

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)

Steps in printing a string

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)

Writing a string to the printer using programmed I/O

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

• Writing a string to the printer using interrupt-driven I/O


– Code executed when print system call is made
– Interrupt service procedure

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

• Printing a string using DMA


– code executed when the print system call is made
– interrupt service procedure

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)

• I/O system call layer encapsulates device behaviors in


generic classes
• Device-driver layer hides differences among I/O
controllers from kernel
– device dependent versus device independent layer
• Devices vary in many dimensions
– Character-stream or block
– Sequential or random-access
– Sharable or dedicated
– Speed of operation
– read-write, read only, or write only

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

• Block devices include disk drives


– Commands include read, write, seek
– Raw I/O or file-system access
– Memory-mapped file access possible
• Character devices include keyboards, mice, serial
ports
– Commands include get, put
– Libraries layered on top allow line editing

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

• Varying enough from block and character oriented


devices to have their own interface
– Introduces new concepts such as connections,
communications protocols
• Unix and Windows include socket interface
– Separates network protocol from network operation
– Includes select functionality
• Approaches vary widely (pipes, FIFOs, streams,
queues, mailboxes)

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

Status register Device’s


CPU I/O Interface

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?

• An I/O interface is a hardware circuit inserted


between a group of I/O ports and the
corresponding device controller.
• It acts as an interpreter that translates the values in
the I/O ports into commands and data for the
device.

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

• Custom I/O interfaces


– devoted to one specific
hardware device
– usually, the device
controller is on the same
card of I/O interfaces.
• General-purpose I/O
interface (GPIO)
– used to connect several
different hardware
devices

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

• It interprets the high-level commands received from


the I/O interface and force the device to execute
specific actions by sending proper sequence of
electrical signals
• It converts and properly interprets the electrical
signals received from the device and modifies
(through the I/O interface) the value of the status
register

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

Layers of the I/O Software System

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

• Logical position of device drivers is shown here


• Communications between drivers and device controllers goes over the
bus
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 44
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device driver development

• 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

You might also like