fc898967fb9a7cd4d7e0be5a4a298a92.ppt
- Количество слайдов: 66
Operating Systems Certificate Program in Software Development CSE-TC and CSIM, AIT September--November, 2003 12. I/O Systems (Ch. 12, S&G) v Objectives – discuss how an OS manages and controls I/O operations and I/O devices OSes: 12. IOSys 1
Contents 1. Issues 2. I/O Hardware 3. Application I/O Interfaces 4. Kernel I/O Subsystems 5. From I/O Requests to Hardware 6. Performance OSes: 12. IOSys 2
1. Issues v I/O is the main job in most computers – processing is secondary v An OS must deal with a wide variety of I/O devices with different properties: – mouse, hard disk, CD-ROM, joystick, keyboard, etc. OSes: 12. IOSys continued 3
Principal Design Aims v Increase the standardization of the I/O software and hardware interfaces. v Support many types of devices. v Performance. v One solution: – device driver modules with standard interfaces OSes: 12. IOSys 4
2. I/O Hardware v Some terminology: – port: a device’s connection point to the main processor (computer) – bus: a connection line allowing several devices to access the processor – controller: a chip or circuit board in the device that manages interaction between the device and processor OSes: 12. IOSys 5
2. 1. Typical PC Bus Structure monitor processor cache graphics controller bridge/memory controller memory SCSI bus Fig. 12. 1, p. 399 disk SCSI controller PCI Bus expansion bus interface IDE disk controller disk OSes: 12. IOSys disk keyboard Expansion Bus parallel port serial port 6
2. 2. Controller Features v Runs microcode for specific tasks – e. g. a hard disk controller may do bad-sector avoidance, buffering v Use registers for holding data, control signals – the processor communicates with the controller by reading/writing to these registers v Registers may be visible to a processor as I/O port addresses. OSes: 12. IOSys 7
2. 3. Some PC I/O Port Addresses Fig. 12. 2, p. 400 v I/O Address Range (Hex) Device 000 -00 F DMA controller 040 -043 timer 320 -32 F hard disk controller 378 -37 F parallel port 3 D 0 -3 DF graphics controller 3 F 0 -3 F 7 floppy drive controller : : OSes: 12. IOSys 8
2. 4. Registers <--> I/O Port v Typically, an I/O port address is made up of 4 registers. v One register is usually 1 - 4 bytes v Different bits/bytes of a register are used for manipulating different parts of the device. OSes: 12. IOSys 9
Register Names v status: read by a processor (host) to see if a device is ready to execute a command, or has finished a command, etc. v control/command: written by a processor (host) to tell the device to start/change its settings, etc. v data-in: read by a procesor (host) v data-out: written by a processor (host) OSes: 12. IOSys 10
2. 5. Handshaking v Handshaking is one possible protocol between a host (processor) and a controller. v Example: the host writes a byte to the device: – 1. The host repeatedly reads the busy bit of the device’s status register until is it clear/false u busy-waiting OSes: 12. IOSys (polling) continued 11
– 2. The host sets the write bit of the device’s command register and writes a byte into the data -out register. – 3. The host sets the ready bit of the command register. – 4. When the device’s controller notices the ready bit, it sets the busy bit of its status register. OSes: 12. IOSys continued 12
– 5. The controller sees the write bit of its command register. It then reads the byte from the data-out register, and does the I/O. – 6. The controller clears the command ready bit, and clears the status busy bit. OSes: 12. IOSys 13
Polling a Device v Polling often requires only 3 CPU instructions: – read the status register – use a logical-and operation to extract the busy bit – branch (goto) is not zero OSes: 12. IOSys continued 14
v Polling becomes inefficient if done too often. v An alternative is to have the device interrupt the host (CPU) when it is free. OSes: 12. IOSys 15
Fig. 12. 3, p. 403 2. 6. Interrupt Driven I/O Cycle I/O Controller CPU 1 7 2 device driver initiates I/O CPU checks for interrupts between instructions 3 4 CPU receives interrupt, transfers control to interrupt handler 5 interrupt handler: processes data, returns OSes: 12. IOSys input ready, or output complete, or error generate interrupt signal 6 CPU resumes interruped task 16
More Sophistication v Defer interrupt handling during critical processing. v Use more efficient ways of finding the right interrupt handler. v Multi-level interrupts. v Multiple interrupt request lines: – nonmaskable and maskable lines OSes: 12. IOSys 17
Interrupt Vector Table v An interrupt vector table is used to map an interrupt to a routine – done by treating the interrupt number as an offset in the table – called an interrupt vector v Each entry in the table may be a list (chain) of handlers – allows overloading of the interrupt number OSes: 12. IOSys 18
Part of a Pentium Vector Table Fig. 12. 4, p. 404 v Vector Number 0 3 5 7 12 13 14 19 -31 32 -255 OSes: 12. IOSys Description Divide error Breakpoint Bound range error Device not available Stack fault General protection Page fault Intel reserved Maskable interrupts 19
Threads and Interrupts v A threaded kernel is useful for implementing multiple interrupt priorities. v The Solaris thread scheduler allows low priority interrupt handlers (threads) to be pre -empted by high priority interrupt handlers (threads). OSes: 12. IOSys 20
2. 7. Direct Memory Access (DMA) v When the host’s CPU does device handshaking, it is called Programmed I/O (PIO) – a waste of CPU resources v Instead, offload the work to a DMA controller – the CPU sends the I/O command to the DMA, then goes on with other work OSes: 12. IOSys continued 21
Advantages v Higher CPU performance since: – less kernel communication – less context switching OSes: 12. IOSys 22
DMA Diagram Fig. 12. 5, p. 407 CPU cache DMA controller CPU/memory bus memory buffer PCI Bus IDE disk controller disk OSes: 12. IOSys disk 23
Cycle Stealing v Cycle stealing occurs when the DMA controller uses the CPU memory bus – the CPU is prevented from accessing memory – this can slow down the CPU’s computation OSes: 12. IOSys 24
Direct Virtual Memory Access (DVMA) v In DVMA, data is transferred between two memory-mapped devices (i. e. from controller to controller) without requiring main memory – no cycle stealing required OSes: 12. IOSys 25
3. Application I/O Interfaces v OSes treat I/O devices in a standard way using: – abstraction, encapulation, software layering v The user/programmer sees a standardized set of I/O functions. v Differences are hidden inside kernel modules called device drivers. OSes: 12. IOSys 26
A Kernel I/O Structure Fig. 12. 6, p. 409 kernel I/O subsystem SCSI device driver keyboard device driver mouse device driver PCI bus device driver floppy device driver ATAPI device driver SCSI device controller keyboard device controller mouse device controller PCI bus device controller floppy device controller ATAPI device controller SCSI devices keyboard mouse PCI bus floppy disk drives disk & tape drives OSes: 12. IOSys 27
Advantages v Simplifies OS developer’s work. v Benefits hardware manufacturers v But… – different OSes have different ‘standard’ device driver interfaces. OSes: 12. IOSys 28
3. 1. Characteristics of I/O Devices Fig. 12. 7, p. 410 v Aspect Variation Example disk-transfer mode character block terminal disk access method sequential random modem CD-ROM transfer schedule synchronous asynchronous tape keyboard sharing dedicated sharable tape keyboard OSes: 12. IOSys : continued 29
Aspect Variation device speed latency seek time transfer rate delay between ops I/O direction v read-only write-only read-write OSes: 12. IOSys Example CD-ROM graphics ctrller disk 30
3. 2. Character and Block Devices v Character device: – transfer byte by byte v Block device: – treat blocks of bytes as the transfer unit v Standard file-system interface: – read(), write(), seek(), etc v Memory mapping: – useful for executing kernel modules/services OSes: 12. IOSys 31
3. 3. Network Devices v Distinguished due to differences in addressing and performance (reliablility, data sequencing, speed). v Standard interface: – sockets, message packets, streams – select() for monitoring collections of sockets OSes: 12. IOSys continued 32
v Other UNIX (inter-process) communication features: – half-duplex pipes – full-duplex FIFOs (queues) – message queues OSes: 12. IOSys 33
3. 4. Clocks and Timers v Basic uses: – get the current time/the elapsed time – set a timer to trigger an operation at time T (e. g. time-slice a process) v Timers have coarse resolution (20 -60 ticks/sec), which limits the precision of triggers – a CPU can execute ~100 M instructions/sec OSes: 12. IOSys 34
3. 5. Blocking & Nonblocking I/O v Blocking (synchronous) I/O – the application is suspended until the I/O is completed – easy to understand/implement v Nonblocking (asynchronous) I/O – the application does not wait OSes: 12. IOSys continued 35
v Some software requires nonblocking I/O – most games software has to be able to receive user input from the keyboard at any time while updating the screen continuously OSes: 12. IOSys 36
Implementing Nonblocking I/O v Threads – place the blocking I/O in one thread, and let the other behaviour execute in other threads v Use nonblocking I/O system calls – they return immediately with whatever was available – e. g. select() called with a time-out of 0 OSes: 12. IOSys continued 37
v Use asynchronous I/O system calls – they return immediately, but the completed I/O result is ‘available’ some time later – the result can be made ‘available’ in different ways: ua special variable u raising a signal (software interrupt) u calling a callback function (e. g. used in Java’s event model) OSes: 12. IOSys 38
4. Kernel I/O Subsystems v These are features built on top of the hardware and device-driver infrastructure, including: – I/O scheduling – buffering – caching – spooling OSes: 12. IOSys 39
4. 1. I/O Scheduling v Scheduling (re-ordering) is carried out to: – improve overall system performance – share device access fairly – reduce average waiting time OSes: 12. IOSys 40
Scheduling Example v Three jobs want to read from a hard disk. The read arm is currently near the beginning of the disk. – job 1: requests a block at the end of the disk – job 2: requests a block near the start – job 3: requests a block in the middle v New schedule: jobs 2, 3, 1 OSes: 12. IOSys 41
4. 2. Buffering v A buffer is a memory area that stores data while it is being transferred between two devices, or between a device and a process. OSes: 12. IOSys 42
Three Uses for Buffering v Cope with speed mismatches – e. g. between a modem and hard disk – double buffering may be used v Cope with different data-transfer sizes – e. g. packet transfer over a network v Support copy semantics for application I/O – e. g. so write() data cannot be changed OSes: 12. IOSys 43
Double Buffering Illustrated VUW cs 305 buffer Slow producer Fast consumer buffer hard disk OSes: 12. IOSys 44
buffer Slow producer Fast consumer buffer hard disk OSes: 12. IOSys 45
buffer Slow producer Fast consumer buffer hard disk OSes: 12. IOSys 46
4. 3. Caching v A cache is a region of ‘fast memory’ that holds copies of data: – used to speed-up reuse and updates – e. g. recently read instructions copied from the hard disk v Cache and buffer difference: – a buffer may hold the only copy of a data item OSes: 12. IOSys 47
4. 4. Spooling v A spool is a buffer (or buffers) used to hold output for a device while the device is processing the current job. v The device can only process one job at once – e. g. a printer, tape reader v Spool management is often done by a dedicated system daemon (process) OSes: 12. IOSys 48
Spooling Diagram Program VUW, cs 305 spool daemon Program Printer driver OSes: 12. IOSys Printer driver 49
Fig. 12. 9, p. 419 4. 5. UNIX I/O Kernel Data Structures kernel file descriptor process open-file table user process OSes: 12. IOSys system open-file table file-system record active inode table inode pointers to functions: read(), write(), select(), . . . network record socket d. s. pointers to functions: read(), write(), select(), . . . network info table 50
v A variety of I/O components are controlled/monitored through the open file table. v Details are hidden from the user by standard abstractions: – e. g. read() works with files and network sockets OSes: 12. IOSys 51
5. From I/O Requests to Hardware v How does the OS convert an I/O request into hardware commands? v Consider how UNIX reads from a file on disk. OSes: 12. IOSys 52
Lifecycle of an I/O Request Fig. 12. 10, p. 422 Request I/O yes system call can satisfy already? user process kernel I/O subsystem I/O completed return from call Transfer data to process no Send request to device driver. Block process. kernel I/O subsystem time OSes: 12. IOSys continued 53
Carry out request. Send commands to controller. Block. device controller commands Monitor device. Interrupt when I/O completed device driver Determine when I/O completed. Tell I/O subsystem. interrupt handler Receive interrupt. Store data in dd buffer. Signal dd. keyboard device controller I/O completed. Generate interrupt. time OSes: 12. IOSys 54
Filename to Disk Controller v A UNIX path name has no clear separation of the device portion: /home/fidji/ad/public_html/index. html v UNIX stores a mount table (in /etc/mnttab) of device name to pathname details: /dev/dsk/c 1 t 1 d 0 s 0 /dev/dsk/c 1 t 2 d 0 s 0 /dev/dsk/c 1 t 4 d 0 s 0 OSes: 12. IOSys /home/fidji /home/hawai /home/corse … … … continued 55
v The name (e. g. /dev/dsk/c 1 t 1 d 0 s 0) is looked up in the file-system, and UNIX finds: –
v The minor device number can be used to index into a device table to find the device controller’s I/O port address or its memory mapped address. OSes: 12. IOSys 57
Stream Modules v UNIX System V allows pipelines of driver code to be assembled dynamically for an application. stream head Application module 1 module 2 OSes: 12. IOSys module N driver end hardware 58
6. Performance v I/O is a major factor in system performance: – it uses the CPU heavily to execute device drivers, schedule processes – cycle stealing – costs of interrupt processing – network traffic OSes: 12. IOSys 59
Network Comms. hard ware char typed Fig. 12. 11, p. 425 interrupt generated state saved interrupt handled system call completes context switch interrupt handled state saved interrupt generated device driver network adapter kernel device driver context switch OSes: 12. IOSys user process context switch kernel network Sending System 60
network packet receive d network hard ware Receiving System network adapter interrupt generated state saved device driver network subdaemon kernel context switch network daemon OSes: 12. IOSys context switch kernel 61
Ways to Improve I/O Efficiency v Reduce the number of context switches. v Reduce the number of data copys to/from memory. v Reduce the number of interrupts. OSes: 12. IOSys continued 62
v Offload I/O work to other processors. v Move more processing into hardware. v Balance the performance of CPU, memory, bus, and the I/O. OSes: 12. IOSys 63
Efficiency Examples v The telnet daemon in Solaris runs inside a kernel thread: – that eliminates context switching between the daemon and the kernel v Front-end processors – e. g. terminal concentrators OSes: 12. IOSys continued 64
v DMA controllers v I/O channels – an I/O channel is a dedicated CPU for offloading I/O work from the main CPU OSes: 12. IOSys 65
Where to put I/O Functionality? device code (hardware) OSes: 12. IOSys increased flexibility device controller code (hardware) increased development cost device driver code increased efficiency application code kernel code increased development time I/O functionality implemented using: increased hiding from user Fig. 12, p. 426 66


