Title: InputOutput
1Input/Output
5.1 Principles of I/O hardware 5.2 Principles of
I/O software 5.3 I/O software layers 5.4
Disks 5.5 Clocks 5.6 Character-oriented
terminals 5.7 Graphical user interfaces 5.8
Network terminals 5.9 Power management
2I/O Software Layers
- Each layer has well-defined interface to the
adjacent layers
3Interrupt Handlers
- Steps must be performed in software when an I/O
interrupt occurs - Save registers
- Set up context for interrupt service procedure
- Set up stack for interrupt service procedure
- Acknowledge interrupt controller
- Copy registers from where saved to the process
table - Run service procedure
- Choose the process to run next
- Set up MMU context for process to run next
- Load new process' registers
- Start running the new process
4Device Drivers
- Device-specific code required for controlling a
device. - A specific driver is required for a mouse,
joystick, etc. - Typically written by the device manufacturer and
delivered with the device. - Device drivers are OS-specific. Typically, a
different one is needed for each operating
system. - For modern operating systems, device drivers are
dynamically loaded into the system during
execution.
5Device Drivers (cont)
- Logical position of device drivers is shown here
- Communications between drivers and device
controllers goes over the bus
6Functions ofA Device Driver
- For each I/O request on a device, the device
driver will - Initialize the device
- check for valid input parameters
- for disk drivers, convert linear block number to
head, track, sector and cylinder. - Check status of device
- If the device is busy, add request to a queue
- If the device is idle, turn the device on if off
and handle the request - Issue commands to the device
- Determine the command sequence
- Write the command to the device controllers
registers - Block if the device operation is long and wake up
with an interrupt or, continue - Check for error
- Pass data or error message to device-independent
software layer above.
7Device Independent I/O Software
- Basic function is to perform the I/O functions
that are common to all devices and to provide a
uniform interface to the user-level software. - Specific functions include
- Uniform interfacing for device drivers
- Buffering
- Error reporting
- Allocating and releasing dedicated devices
- Providing a device-independent block size
8System without a standard driver interface
- Each device driver has a different interface to
the system requiring a lot of programming for
each new driver. - Driver functions that the OS can call will be
different for each device. - The OS functions the driver needs will be
different for each device.
9System with a standard driver interface
- All drivers have the same interface.
- All drivers whose interface conforms to the
standard is pluggable. - Device driver programmers need to know in advance
what functions they may provide and what OS
functions they may call. - Symbolic device names are mapped to the proper
drivers. - Protection of access to devices by users is
enforced.
10Buffering
- Unbuffered input
- - The user process is blocked/unblocked with
each character read. - - Inefficient
- Buffering in user space
- - User process is interrupted only after the
buffer is filled up - - More efficient than (a) but will cause
problems if the buffer is paged out when the
character arrives
11Buffering
- (c) Buffering in the kernel followed by copying
to user space - - More efficient than (a) and (b)
- - Minimizes the risk of the user page being
paged out when the copying is performed. - - Will cause problems if the kernel buffer is
full and ready to be copied but the user buffer
is still being paged in for copying. - (d) Double buffering in the kernel
- - Solves the problem with (c)
12Error Reporting
- Handle errors that cannot be handled by device
drivers - Programmer errors
- Writing to an input device (keyboard)
- Reading from an output device (monitor)
- Actual I/O errors
- Damaged disk block
- Error handling
- Display a dialog box to the user
- Retry, ignore or abort (kill the process)
13Allocating and Releasing Dedicated Devices
- Examples are CD-ROM, scanner
- Two ways to allocate and release dedicated
devices - Require processes to perform an open command
(allocate). If the open fails, the device is in
use and the process has to try again. A close
command releases the device. - Have a special mechanism for requesting and
releasing the device - When a user process tries to acquire a device
that is held by another process, the user process
is blocked and placed in a queue. When the
device becomes available, the first process in
the queue is allowed to acquire the device and
continue exectuion.
14Device-Independent Block Size
- Hide the fact that different devices deliver
different size inputs or receive different size
outputs - disks may have different sector sizes
- provide a uniform block size to the higher layers
by treating several sectors as a single block
size. - Modems may deliver data one character at a time
- Provide a uniform block size to higher layers by
buffering the input to a uniform block size
before delivering to higher layers.
15User-Space I/O Software
- Most I/O software is in the kernel. A small
portion are user-level, library procedures. - Categories of user-level I/O software
- Library procedures
- Make I/O system calls
- count write( fd, buffer, nbytes)
- Formatting of I/O
- Printf( The square of 3d is 6d\n, i, iI )
- Spooling system
- Printer
- A way to deal with dedicated devices
16Layers of the I/O System Summary
17Disks
- Magnetic disks
- Reads and writes equally fast
- Ideal secondary storage (paging, file systems,
etc.) - eg. hard disks, floppy disks
- Optical disks
- For distribution of programs, data and movies
- CD-ROMs
- DVDs
18Disks parameters floppy and hard disks
19Physical vs. Virtual Disk Geometry
cylinder
tracks
sectors
- Physical geometry of a disk with two zones
- Modern disks are divided into zones with more
zones in the outer section than in the inner ones - A possible virtual geometry for this disk
- This is presented to the OS.
- The controller remaps the request made using the
virtual geometry to the real cylinder, head and
sector in the physical geometry
20Low Level Disk Formatting
512 bytes
- Low-level formatting of a disk sector done by
software - Preamble
- contains certain bit patterns that allow hardware
to recognize the start of a sector - Contains cylinder, track and sector numbers along
with other information - Data
- To contain the actual data the will be copied to
the disk - ECC (error correction code)
- Used to recover from read errors
- Size and content vary from manufacturer to
manufacturer - Spare sectors are allocated to replace defective
sectors. - Results in reduced disk capacity of about 20
- Uses cylinder skew
21Cylinder Skew
- The position of sector 0 in each track is offset
from the previous track - Allows for reads of multiple tracks in one
continuous operation without losing data. - The amount of cylinder skew depends on the disk
geometry
22Interleaving
- Problem
- A controller with one-sector size buffer is ask
to read two sectors from the disk. The
controller needs to read the first sector from
the disk, perform error checking and transfer the
data to main memory. While all of this is taking
place, the second sector moves past the disk arm
preventing the controller from reading the
sector. Now the controller will have to wait for
the disk to complete its rotation for the second
sector. - Solution
- Use interleaving
23Interleaving
- Interleaving gives the controller time between
consecutive sector reads to process a sector
read. - (a) No interleaving
- problem
- (b) Single interleaving
- May not be sufficient if the time to process a
sector by the controller is slow. - (c) Double interleaving
- Gives the controller more time
24Disk Arm Scheduling
- Time required to read or write a disk block is
determined by 3 factors - Seek time
- Time to move the arm to the correct cylinder
- Rotational delay
- Time for the correct sector to rotate under the
head - Actual transfer time
- Time to transfer data to the controller
- Seek time dominates
- It is the longest
- Reducing seek mean time can improve system
performance substantially.
25Disk Arm Scheduling Algorithms
- Requires that disk drivers maintain a table,
indexed by cylinder number, where all the pending
requests for each cylinder are chained together
in a linked list headed by the table entries. - FCFS
- first-come, first served
- SSF
- shortest seek first
- Elevator
- the same algorithm used in building elevators
26First-Come First Served
- Requires disk arm motions of
- 11?1 10 16?34 18
- 1?36 35 34?9 25
- 36?16 20 9?12 3 Total of 111 cylinders
27Shortest Seek First
- Requires disk arm motions of
- 11?12 1 16?1 15
- 12?9 3 1?34 33
- 9?16 7 34?36 2 Total of 61 cylinders
28Elevator Algorithm
- Requires disk arm motions of
- 11?12 1 34?36 2
- 12?16 4 36?9 27
- 16?34 18 9?1 8 Total of 60 cylinders
29Disk Read Optimization
- Disk scheduling algorithm
- See previous discussion
- Caching
- The controllers cache may be used to hold
blocks that have not been requested but were
convenient to read because they happen to pass
under the head as a side effect of some other read
30Disk Errors
- Bad sector
- Manufacturer defect
- Bad sectors in a new disk caused by the
manufacturing process - Use spare sector to replace
- Transient
- Dust
- Simply re-read
- A sector that failed
- Use spare sector to replace
- Mechanical disk arm
- A seek operation ends at the wrong cylinder
- The disk controller recalibrates the arm.
31Replacing a bad sector
- (a) A disk track with a bad sector
- (b) Remap one of the spares as sector 7, or
- (c) Shift all the sectors to bypass the bad one
32Clocks
- Also called timers
- Some functions
- Maintains the time of day
- Prevent one process from monopolizing the CPU
33Clock Hardware
- The value in the holding register is loaded to
the counter - The crystal oscillator generates a signal of very
high frequency - This signal is multiplied by a small integer to
get frequencies up to 1000 MHz or more resulting
in the synchronization signal used by the
computer - The synchronization signal is fed into the
counter causing it to decrement with each pulse
from the crystal until it counts down to zero. - When the counter gets to zero, a CPU interrupt
occurs then the cycle is started all over again.
34The Clock Software Clock Driver
- Functions of the clock driver
- Maintain the time of day
- Requires incrementing a time-of-day counter at
each clock tick - Prevent process from running longer that they are
allowed to - When a process is allowed to run, the scheduler
loads a counter with that process quantum in
clock ticks - At every clock interrupt, the clock driver
decrements the quantum counter by 1. - When the counter gets to zero, the clock driver
calls the scheduler to run another process. - Accounting for CPU usage
- Keep track of how much CPU time a process has had.
35Clock Driver (cont.)
- Functions of the clock driver
- Handling alarm system call of user processes
- When a timer goes off, the clock driver sends an
alarm to the user process - Useful in networking.
- When a packet is sent out and an acknowledgement
is not received, the packet has to be
retransmitted. - Provide watchdog timers for parts of the system
itself - When a timer goes off, the clock driver calls a
procedure supplied by the user process. - When a floppy disk motor is started, a timer goes
off after a sufficient time interval to notify
the user process that the disk is ready to read
or write. - Do profiling, monitoring and statistics gathering
- The OS builds up a histogram of a processes
program counter to see where it is spending its
time
36Character-Oriented Terminals
- Terminal
- A generic term to denote a keyboard and an
attached display - Standalone terminals with RS-232 interfaces used
on mainframes - Includes the keyboard and monitor
- PCs with graphical user interface
- Network terminals
37Standalone RS-232 Terminal Hardware
- Text-only terminals
- An RS-232 terminal communicates with computer 1
bit at a time - Called a serial line bits go out in series, 1
bit at a time - The bits are buffered in the interface card. The
UART then shifts out the character one bit at a
time over the serial line. - Windows uses COM1 and COM2 ports to link to
serial lines - All modems use this interface
38Input Software
- Keyboard driver
- Collect input from the user and pass it to the
user programs - On every key action, the keyboard driver extracts
the character typed by reading an I/O port. - Two implementation paradigm for the driver
- Raw mode (noncanonical)
- accept the input and pass it, unmodified, to the
user process. - Eg. Dste corrected to Date will get passed as
- Dste???ateCR
- Character-oriented
- Cooked mode (canonical)
- Sends only the corrected input. The driver
handles all intraline editing and just delivers
the corrected lines - Line-oriented
39Buffering Requirements
- Raw mode
- Allow the user to type ahead even before the
programs have asked for the input - Cooked mode
- Characters must be stored until the entire line
has been accumulated in case the user decide to
erase part of the line.
40Buffering Technique- Central Buffer Pool
- The keyboard driver maintains a central pool of
buffers, each buffer holding 10 characters. - Associated with each terminal is a data structure
which contains a pointer to the chain of buffers
for input collected from that terminal. - As more characters are typed, more buffers are
acquired and attached to the chain. - When characters are passed to the user program,
the buffers are released and put back on the pool.
41Buffering Technique- Dedicated Buffer
- Use the terminal data structure to buffer the
characters typed directly. - Allocate something like 200 characters per
terminal
42Output Software
- Displays characters to the monitor
- Can use the same buffering techniques as the
input software. - When a program writes to the terminal, the
characters are first copied to the buffer. When
the output from the buffer to the monitor starts,
the driver goes to sleep.
43PCs with Graphical User Interface
- PCs can use character-based interfaces
- MS-DOS is character-based
- PCs also use graphical user interfaces (GUIs)
- Essential elements of a GUI (WIMP
- Windows
- Rectangular boxes in the screen to run users
programs - Icon
- Small symbols that can be clicked to cause some
action to happen - Menus
- List of action from which one can be chosen
- Pointing device
- Used to move the cursor around the screen to
select items
44PCs Input and Output Devices
- Keyboard
- Similar in concept to the RS-232 input device
- Mouse
- Sends a message to the computer that contains
three items ?x, ?y, button - ?x, ?y change in y and x positions since last
message - Button status of the buttons
- Display hardware
- Vector graphics
- Devices that accept and carry out commands such
as draw points, lines, geometric figures and text - plotters
- Raster graphics (bitmap graphics)
- Devices that represent the output area as a
rectangular grid of oints called pixels - Pixels has some grayscale value or color
- Implemented by a graphics adapter as
memory-mapped displays
45Graphics Adapter
Serial, parallel or USB port
- Memory-mapped displays where the video driver
writes directly into display's video RAM - Video Ram
- Forms part of the computers address space
- Video Controller
- Pulls characters or bits out of the video RAM and
generates the video signal to drive the monitor
46Video Ram
- Part of the computers address space, it is
addressable by the CPU the same way as the rest
of memory. - Monochrome starting address B0000000
- Color starting address B00080000
- Stores the screen image character mode or bit
mode - Character mode a byte or two bytes of video RAM
contains one character to be displayed - Bit mode each pixel on the screen is represented
with 1 bit for black to 24 or more bits for high
quality color display in the video RAM
47Video RAM and the Monitor
- A video RAM image
- simple monochrome display in character mode
- Corresponding screen
- the xs are attribute bytes
48Output Software for Windows
- The basic item on the display screen is the
window created by a Windows program. - Sample window located at (200,100) on XGA display
49A Windows Program
- Message oriented
- User actions involving the keyboard or mouse are
captured by Windows and converted into messages
to the program owning the window being addressed. - Each program has a message queue to which the
messages relating to all its windows are sent - The programs behavior is driven by the incoming
messages.
50Network Terminals
- Used to connect a remote user to a computer over
a network - Types of network terminals
- a fat terminal
- Contains a CPU, memory, keyboard and terminal
that can run complex protocols to compress large
amounts of data over the network - X Window System
- a SLIM terminal
- Simple, basically displaying pixels and not doing
much thinking - cheap
51X Windows System
- Clients and servers in the M.I.T. X Window System
52The SLIM Network Terminal
- The architecture of the SLIM terminal system
53The SLIM Network Terminal (2)
- Messages used in the SLIM protocol from the
server to the terminals