You are on page 1of 3

Input / Output (I/O)

Input / Output (I/O)


(cont.)

n Terminology
● Synchronous I/O — CPU execution waits
CPU
Di sk Pr in t er Tape Drive Memory while I/O proceeds
Cont ro ller Cont r oller Co nt ro ller Cont r oller

● Asynchronous I/O — I/O proceeds


concurrently with CPU execution
n CPU and device controllers all use a
common bus for communication n Interrupt-based asynchronous I/O
● Device controller has its own processor,
n Software-polling synchronous I/O and executes asynchronously with CPU
n Device controller puts an interrupt signal
● CPU starts an I/O operation, and on the bus when it needs CPU’s attention
continuously polls (checks) that device
until the I/O operation finishes ● When CPU receives an interrupt:
1. It saves the CPU state and invokes the
● Device controller contains registers for appropriate interrupt handler using the
communication with that device interrupt vector (addresses of OS
n Input register, output register — for data routines to handle various events)
n Control register — to tell it what to do 2. Handler must save and restore software
n Status register — to see what it’s done state (e.g., registers it will modify)
3. CPU restores CPU state
● Why not connect all I/O devices directly to
CPU? Why not… memory…?
1 Spring 1997, Lecture 03 2 Spring 1997, Lecture 03

Input / Output (I/O)


Storage Structures
(cont.)
FAST Reg ist er s SMALL
n Memory-mapped I/O
Level 1 Cache
● Uses direct memory access (DMA) — I/O
device can transfer block of data to / from Level 2 Cache

memory without going through CPU Main Memory

I/ O commands
Disk (Virt ual Memory)
CPU I/ O device
Disk (File Syst em)

dat a dat a
SLOW Backup Tape LA RGE
memory

● OS allocates buffer in memory, tells I/O n At a given level, memory may not be as
device to use that buffer big or as fast as you’d like it be
● I/O device operates asynchronously with ● Tradeoffs between size and speed
CPU, interrupts CPU when finished
● Used for most high-speed I/O devices n Principle of Locality of Reference leads
(e.g., disks, communication interfaces) to caching
● When info is needed, look on this level
● If it’s not on this level, get it from the next
slower level, and save a copy here in
case it’s needed again sometime soon
3 Spring 1997, Lecture 03 4 Spring 1997, Lecture 03
Magnetic Disks Protection

n Provide secondary storage for system n Multiprogramming and timesharing


(after main memory) require that the memory and I/O of the
OS and user processes be protected
n Technology against each other
● Covered with magnetic material ● Note that most PCs do not support this
kind of protection
● Read / write head “floats” just above
surface of disk
n Provide protection via two modes of CPU
● Hierarchically organized as platters, execution: user mode and kernel mode
tracks, sectors (blocks)
● In kernel / privileged / supervisor mode,
n Devices privileged instructions can:
n Access I/O devices, control interrupts
● Hard (moving-head) disk — one or more n Manipulate the state of the memory (page
platters, head moves across tracks table, TLB, etc.)
● Floppy disk — disk covered with hard n Halt the machine
surface, read / write head sits on disk, n Change the mode
slower, smaller, removable, rugged ● Requires architectural support:
● CDROM — uses laser, read-only, high- n Mode bit in a protected register
density n Privileged instructions, which can only be
n Optical —read / write executed in kernel mode
5 Spring 1997, Lecture 03 6 Spring 1997, Lecture 03

I/O Protection Memory Protection

n To prevent illegal I/O, or simultaneous n Must protect OS’s memory from user
I/O requests from multiple processes, programs (can’t overwrite, can’t access)
perform all I/O via privileged instructions ● Must protect memory of one process from
● User programs must make a system call another process
to the OS to perform I/O ● Must not protect memory of user process
from OS
n When user process makes a system call:
● A trap (software-generated interrupt) n Simplest and most common technique:
occurs, which causes:
● Base register —smallest legal address
n The appropriate trap handler to be invoked
using the trap vector ● Limit register — size of address range
n Kernel mode to be set ● Base and limit registers are loaded by OS
● Trap handler: before running a particular process
n Saves state ● CPU checks each address (instruction &
n Performs requested I/O (if appropriate) data) generated in user mode
n Restores state, sets user mode, and n Any attempt to access memory outside the
returns to calling program legal range results in a trap to the OS

n Additional hardware support is provided


for virtual memory
7 Spring 1997, Lecture 03 8 Spring 1997, Lecture 03
CPU Protection Computer Architecture & OS

n Use a timer to prevent CPU from being n Need for OS services often drives
hogged by one process (either inclusion of architectural features in CPU:
maliciously, or due to an infinite loop) OS Service Hardware Support
● Set timer to interrupt OS after a specified I/O interrupts
period (small fraction of a second) memory-mapped I/O
● When interrupt occurs, control transfers caching
to OS, which decides which process to Data access memory hierarchies
execute for next time interval (maybe the file systems
same process, maybe another one)
Protection system calls
n Also use timer to implement time sharing kernel & user mode
privileged instructions
● At end of each time interval, OS switches interrupts & traps
to another process base & limit registers
● Context switch = save state of that Scheduling & timers
process, update Process Control Block Error recovery
for each of the two processes, restore
state of next process

9 Spring 1997, Lecture 03 10 Spring 1997, Lecture 03

You might also like