540 likes | 658 Views
I/O and Filesystems. How to provide interfaces. Rough reading guide (no exam guarantee): Tanenbaum Ch. 5.1 – 5.5 & 6.1-3 Silberschatz Ch. 13 & 10.1-4, 11.1-8 Also: Patterson Ch. 8 (I/O). I/O. A computer without peripheral devices is pretty much useless
E N D
How to provide interfaces Rough reading guide (no exam guarantee): Tanenbaum Ch. 5.1 – 5.5 & 6.1-3 Silberschatz Ch. 13 & 10.1-4, 11.1-8 Also: Patterson Ch. 8 (I/O)
I/O • A computer without peripheral devices is pretty much useless • Old mainframes required kernel-recompile to install new devices • Now: PCs have millions of possible devices
Goals for I/O Handling • Enable use of peripheral devices • Present a uniform interface for • Users (files etc.) • Devices drivers • Hide the details of devices from users (and OS)
Device Controllers • I/O devices have components: • mechanical component • electronic component • The electronic component is the device controller • may be able to handle multiple devices • OS deals with controllers not devices • Controller's tasks • convert bit stream to block of bytes • perform error correction as necessary • make blocks available to main memory
Relevant Device Properties (1) • The mechanical/electronic properties of devices impact software • timing (seek times etc.) • interrupts • data rates
Relevant Device Properties (2) Some typical device, network, and bus data rates
Accessing Devices (1) • Most devices provide • buffers (in / out) • control registers • status registers • These are accessed from the OS/Apps • I/O ports • memory-mapped • hybrid
Accessing Devices (2) • Separate I/O and memory space • Memory-mapped I/O • Hybrid
Memory-Mapped I/O • No special instruction in assembler needed • No special protection needed (remember VM) • Testing control registers directly Not load and test! • Caching of control registers? • One bus?
Memory-Mapped I/O (a) A single-bus architecture (b) A dual-bus memory architecture
Shuffling Data • Data needs to be sent • one byte at a time • several bytes at a time • Using the CPU to shuffle small amounts of data may be inefficient Direct Memory Access (DMA)
Direct Memory Access (DMA) Operation of a DMA transfer
Interrupts Revisited Connections between devices and interrupt controller actually use interrupt lines on the bus rather than dedicated wires
Goals of I/O Software (1) • Device independence • programs can access any I/O device • without specifying device in advance • (file on floppy, hard drive, or CD-ROM) • Uniform naming • name a file or device as a string or an integer • not depending on which machine • Error handling • handle as close to the HW as possible
Goals of I/O Software (2) • Synchronous vs. asynchronous transfers • blocking transfers vs. interrupt-driven • OS may make interrupt-driven operations look like blocking to the user • Buffering • data coming off a device cannot be stored in final destination • OS should buffer for pre-processing/RT.. • Sharable vs. dedicated devices • disks are sharable • tape drives would not be • OS should be able to support both
I/O There are three kinds of I/O handling • Programmed I/O • “Do it all yourself” • Interrupt-driven I/O • “Here you are, now tell me when its done” • DMA-based I/O • “Let someone else do it”
Writing a string to the printer using interrupt-driven I/O Interrupt-Driven I/O Code for system call Code for interrupt handler
I/O Using DMA • Printing a string using DMA a) code executed when the print system call is made b) interrupt service procedure
I/O Software Layers Layers of the I/O Software System
Interrupt Handlers (1) • Interrupt handlers are best hidden • have driver starting an I/O operation block until interrupt notifies of completion • Interrupt procedure does its task • then unblocks driver that started it
Interrupt Service Routine Steps that must be performed in software when interrupt occurs (no complete list) Step 1: Preparation • Save regs not already saved by interrupt hardware • Set up context for interrupt service procedure • Ack interrupt controller, reenable interrupts () Step 2: Actual task execution • Copy registers from where saved • Run service procedure • Schedule and run a new process (+ new context)
Interrupt Handlers (3) • Interrupt handlers should be fast (why?) • Sometimes there is a lot of things to do • Copying buffers, waking up processes, starting new I/O ops, etc. • Solution: split in two parts • Top half: disabled interrupts, only essential work • Bottom half: enabled interrupts, does most of the work
Device Drivers (1) • Logical position of device drivers is shown here • Communication between drivers and device controllers over the bus
Device Drivers (2) • Classically we distinguish two types • Block devices (disks..) • Character devices (keyboards, printers..) • Handles (typically) • Abstract requests “reads” and “writes” • Communication with device controller • Initialization • Power management
Device-Independent I/O Software (1) Generic I/O management in the kernel Functions of the device-independent I/O software
Uniform Interfacing for Device Drivers (a) Without a standard driver interface (b) With a standard driver interface
Device-Independent I/O Software (3) • How are devices addressed? • UNIX/Linux • Major number • Identifying the device driver • Minor number • Identifying the virtual device
Linux Driver Registration • Drivers (modules) need to register with the kernel • Kernel keeps table of drivers • Special kernel routines for adding/deleting entries
Buffering • Where to put the buffers • User • Kernel • What if the buffer is full? • How many buffers? • When to tell the user app?
Device-Independent I/O Software (a) Unbuffered input (b) Buffering in user space (c) Buffering in the kernel followed by copying to user space (d) Double buffering in the kernel
I/O Buffering • Where to store data to be sent? • User buffer • Locking? • When finished? • Kernel buffer • No locking • Device controller
Device-Independent I/O Software Networking may involve many copies
User-Space I/O Software filesystem Layers of the I/O system and the main functions of each layer
More I/O • Read more about disks, clocks, terminals etc. in Tanenbaum • Disks: • Slow • Block size (transfer unit) • Read about RAID 0-5! (Tanenbaum 302-306) • Covered in the exercise
File Systems • UNIX: Everything is a file! • Main memory is not enough • Persistency • Not large enough • Need a structuring of “data”
Long-term Information Storage • Must store large amounts of data • Information stored must survive the termination of the process using it • Multiple processes must be able to access the information concurrently • Store information on disks and other external media in units called files • Filesystem: OS Part that manages files
File Naming Typical file extensions
File Structure Three common ways how OS structures files a) byte sequence b) record sequence c) Tree (records of varied length, key field)
File Types • Regular Files • ASCII • binary (a) executable (b) archive • Directories • Character special files • Block special files
File Access • Sequential access (early OS) • read all bytes/records from the beginning • cannot jump around, could rewind or back up • convenient when medium was magnetic tape • Random access (modern OS) • bytes/records read in any order • essential for many app., e.g., database • read can be … • move file marker (seek) and then read, or • read and then move file marker
File Attributes Possible file attributes (also metadata)
Create Delete Open Close Read Write Append Seek Get attributes Set attributes Rename Lock File Operations
Memory-Mapped Files • Accessing files using read/writes is sometimes cumbersome • Alternative: map the whole file in memory • Access the memory directly
Memory-Mapped Files (a) Segmented process before mapping files into its address space (b) Process after mapping existing file abc into one segment creating new segment for xyz