Presentation on theme: "Operating Systems 12. I/O Systems (Ch. 12, S&G) Objectives"— Presentation transcript:
1Operating Systems 12. I/O Systems (Ch. 12, S&G) Objectives Certificate Program in Software Development CSE-TC and CSIM, AIT September--November, 200312. I/O Systems (Ch. 12, S&G)Objectivesdiscuss how an OS manages and controls I/O operations and I/O devices
2Contents 1. Issues 2. I/O Hardware 3. Application I/O Interfaces 4. Kernel I/O Subsystems5. From I/O Requests to Hardware6. Performance
31. Issues I/O is the main job in most computers processing is secondaryAn OS must deal with a wide variety of I/O devices with different properties:mouse, hard disk, CD-ROM, joystick, keyboard, etc.continued
4Principal Design AimsIncrease the standardization of the I/O software and hardware interfaces.Support many types of devices.Performance.One solution:device driver modules with standard interfaces
52. I/O Hardware Some terminology: port: a device’s connection point to the main processor (computer)bus: a connection line allowing several devices to access the processorcontroller: a chip or circuit board in the device that manages interaction between the device and processor
62.1. Typical PC Bus Structure Fig. 12.1, p.399diskdiskmonitorprocessorSCSI busdiskcachediskgraphicscontrollerbridge/memorycontrollermemorySCSI controllerPCI BusIDE disk controllerexpansion bus interfacekeyboardExpansion Busdiskdiskdiskdiskparallel portserial port
72.2. Controller Features Runs microcode for specific tasks e.g. a hard disk controller may do bad-sector avoidance, bufferingUse registers for holding data, control signalsthe processor communicates with the controller by reading/writing to these registersRegisters may be visible to a processor as I/O port addresses.
82.3. Some PC I/O Port Addresses Fig. 12.2, p.400I/O Address Range (Hex) Device F DMA controller timer F hard disk controller F parallel port 3D0-3DF graphics controller 3F0-3F7 floppy drive controller : :
92.4. Registers <--> I/O Port Typically, an I/O port address is made up of 4 registers.One register is usually 1- 4 bytesDifferent bits/bytes of a register are used for manipulating different parts of the device.
10Register Namesstatus: read by a processor (host) to see if a device is ready to execute a command, or has finished a command, etc.control/command: written by a processor (host) to tell the device to start/change its settings, etc.data-in: read by a procesor (host)data-out: written by a processor (host)
112.5. HandshakingHandshaking is one possible protocol between a host (processor) and a controller.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/falsebusy-waiting (polling)continued
123. The host sets the ready bit of the command register. 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.continued
135. The controller sees the write bit of its command register 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.
14Polling a Device Polling often requires only 3 CPU instructions: read the status registeruse a logical-and operation to extract the busy bitbranch (goto) is not zerocontinued
15Polling becomes inefficient if done too often. An alternative is to have the device interrupt the host (CPU) when it is free.
162.6. Interrupt Driven I/O Cycle Fig. 12.3, p.4032.6. Interrupt Driven I/O CycleCPUI/O Controller12device driver initiates I/O7initiates I/OCPU checks for interrupts between instructions3input ready, or output complete, or error generate interrupt signal4CPU receives interrupt, transfers control to interrupt handler56interrupt handler: processes data, returnsCPU resumes interruped task
17More SophisticationDefer interrupt handling during critical processing.Use more efficient ways of finding the right interrupt handler.Multi-level interrupts.Multiple interrupt request lines:nonmaskable and maskable lines
18Interrupt Vector Table An interrupt vector table is used to map an interrupt to a routinedone by treating the interrupt number as an offset in the tablecalled an interrupt vectorEach entry in the table may be a list (chain) of handlersallows overloading of the interrupt number
19Part of a Pentium Vector Table Fig. 12.4, p.404Vector Number Description 0 Divide error 3 Breakpoint 5 Bound range error 7 Device not available Stack fault General protection Page fault Intel reserved Maskable interrupts
20Threads and Interrupts A threaded kernel is useful for implementing multiple interrupt priorities.The Solaris thread scheduler allows low priority interrupt handlers (threads) to be pre-empted by high priority interrupt handlers (threads).
212.7. Direct Memory Access (DMA) When the host’s CPU does device handshaking, it is called Programmed I/O (PIO)a waste of CPU resourcesInstead, offload the work to a DMA controllerthe CPU sends the I/O command to the DMA, then goes on with other workcontinued
22Advantages Higher CPU performance since: less kernel communication less context switching
23DMA Diagram Fig. 12.5, p.407 CPU cache DMA controller memory PCI Bus bufferCPU/memory busPCI BusIDE disk controllerdiskdisk
24Cycle StealingCycle stealing occurs when the DMA controller uses the CPU memory busthe CPU is prevented from accessing memorythis can slow down the CPU’s computation
25Direct Virtual Memory Access (DVMA) In DVMA, data is transferred between two memory-mapped devices (i.e. from controller to controller) without requiring main memoryno cycle stealing required
263. Application I/O Interfaces OSes treat I/O devices in a standard way using:abstraction, encapulation, software layeringThe user/programmer sees a standardized set of I/O functions.Differences are hidden inside kernel modules called device drivers.
27PCI bus device controller A Kernel I/O StructureFig. 12.6, p.409kernelkernel I/O subsystemSCSI devicedriverkeyboard devicedrivermouse devicedriverPCI bus devicedriverfloppy devicedriverATAPI devicedriverSCSI devicecontrollerkeyboard devicecontrollermouse devicecontrollerPCI bus device controllerfloppy devicecontrollerATAPI devicecontrollerSCSI deviceskeyboardmousePCI busfloppy disk drivesdisk & tape drives
28Advantages Simplifies OS developer’s work. Benefits hardware manufacturersBut…different OSes have different ‘standard’ device driver interfaces.
293.1. Characteristics of I/O Devices Fig. 12.7, p.410Aspect Variation Example disk-transfer mode character terminal block disk access method sequential modem random CD-ROM transfer schedule synchronous tape asynchronous keyboard sharing dedicated tape sharable keyboard :continued
30Aspect. Variation. Example device speed. latency. seek time Aspect Variation Example device speed latency seek time transfer rate delay between ops I/O direction read-only CD-ROM write-only graphics ctrller read-write disk
313.2. Character and Block Devices Character device:transfer byte by byteBlock device:treat blocks of bytes as the transfer unitStandard file-system interface:read(), write(), seek(), etcMemory mapping:useful for executing kernel modules/services
323.3. Network DevicesDistinguished due to differences in addressing and performance (reliablility, data sequencing, speed).Standard interface:sockets, message packets, streamsselect() for monitoring collections of socketscontinued
33Other UNIX (inter-process) communication features: half-duplex pipesfull-duplex FIFOs (queues)message queues
343.4. Clocks and Timers Basic uses: get the current time/the elapsed timeset a timer to trigger an operation at time T (e.g. time-slice a process)Timers have coarse resolution (20-60 ticks/sec), which limits the precision of triggersa CPU can execute ~100M instructions/sec
353.5. Blocking & Nonblocking I/O Blocking (synchronous) I/Othe application is suspended until the I/O is completedeasy to understand/implementNonblocking (asynchronous) I/Othe application does not waitcontinued
36Some 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
37Implementing Nonblocking I/O Threadsplace the blocking I/O in one thread, and let the other behaviour execute in other threadsUse nonblocking I/O system callsthey return immediately with whatever was availablee.g. select() called with a time-out of 0continued
38Use asynchronous I/O system calls they return immediately, but the completed I/O result is ‘available’ some time laterthe result can be made ‘available’ in different ways:a special variableraising a signal (software interrupt)calling a callback function (e.g. used in Java’s event model)
394. Kernel I/O SubsystemsThese are features built on top of the hardware and device-driver infrastructure, including:I/O schedulingbufferingcachingspooling
404.1. I/O Scheduling Scheduling (re-ordering) is carried out to: improve overall system performanceshare device access fairlyreduce average waiting time
41Scheduling ExampleThree 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 diskjob 2: requests a block near the startjob 3: requests a block in the middleNew schedule: jobs 2, 3, 1
424.2. BufferingA buffer is a memory area that stores data while it is being transferred between two devices, or between a device and a process.
43Three Uses for Buffering Cope with speed mismatchese.g. between a modem and hard diskdouble buffering may be usedCope with different data-transfer sizese.g. packet transfer over a networkSupport copy semantics for application I/Oe.g. so write() data cannot be changed
44Double Buffering Illustrated VUW cs305Slow producerbufferFast consumerbufferhard disk
474.3. CachingA cache is a region of ‘fast memory’ that holds copies of data:used to speed-up reuse and updatese.g. recently read instructions copied from the hard diskCache and buffer difference:a buffer may hold the only copy of a data item
484.4. SpoolingA spool is a buffer (or buffers) used to hold output for a device while the device is processing the current job.The device can only process one job at oncee.g. a printer, tape readerSpool management is often done by a dedicated system daemon (process)
49Spooling Diagram VUW, cs305 spool daemon Program Program Program PrinterdriverPrinterdriver
504.5. UNIX I/O Kernel Data Structures Fig. 12.9, p.4194.5. UNIX I/O Kernel Data Structureskernelsystem open-file tableactive inode tablefile-system record inode pointer pointers to functions: read(), write(), select(),...file descriptornetwork record socket d.s. pointer pointers to functions: read(), write(), select(),...process open-file tablenetwork info tableuser process
51Details are hidden from the user by standard abstractions: A variety of I/O components are controlled/monitored through the open file table.Details are hidden from the user by standard abstractions:e.g. read() works with files and network sockets
525. From I/O Requests to Hardware How does the OS convert an I/O request into hardware commands?Consider how UNIX reads from a file on disk.
53Lifecycle of an I/O Request Fig , p.422user processRequest I/OI/O completedyesreturn from callsystem callTransfer data to processcan satisfy already?kernel I/O subsystemnoSend request to device driver. Block process.kernel I/O subsystemtimecontinued
54Determine when I/O completed. Tell I/O subsystem. Carry out request. Send commands to controller. Block.device driverReceive interrupt. Store data in dd buffer. Signal dd.device controller commandsinterrupt handlerkeyboard device controllerMonitor device.Interrupt when I/O completedI/O completed. Generate interrupt.time
55Filename to Disk Controller A UNIX path name has no clear separation of the device portion:/home/fidji/ad/public_html/index.htmlUNIX stores a mount table (in /etc/mnttab) of device name to pathname details:/dev/dsk/c1t1d0s0 /home/fidji … /dev/dsk/c1t2d0s0 /home/hawai … /dev/dsk/c1t4d0s0 /home/corse …continued
56The name (e.g. /dev/dsk/c1t1d0s0) is looked up in the file-system, and UNIX finds: <major, minor> device numbersThe major device number identifies the device driver type (e.g. character/block transfer).continued
57The 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.
58Stream ModulesUNIX System V allows pipelines of driver code to be assembled dynamically for an application.stream headmodule 1Applicationmodule 2module Ndriver endhardware
596. Performance I/O is a major factor in system performance: it uses the CPU heavily to execute device drivers, schedule processescycle stealingcosts of interrupt processingnetwork traffic
60Network Comms. Sending System Fig. 12.11, p.425 hard ware char typed system call completescontext switchinterrupt generatedinterrupt handledstate savedstate savedinterrupt handledinterrupt generateddevice drivernetworknetwork adapterkerneldevice drivercontext switchcontext switchSending Systemuser processkernel
61network packet received hard wareReceiving Systemnetworknetwork adapterinterrupt generatedstate saveddevice drivernetwork subdaemonkernelcontext switchcontext switchcontext switchkernelnetwork daemon
62Ways to Improve I/O Efficiency Reduce the number of context switches.Reduce the number of data copys to/from memory.Reduce the number of interrupts.continued
63Offload I/O work to other processors. Move more processing into hardware.Balance the performance of CPU, memory, bus, and the I/O.
64Efficiency ExamplesThe telnet daemon in Solaris runs inside a kernel thread:that eliminates context switching between the daemon and the kernelFront-end processorse.g. terminal concentratorscontinued
65DMA controllers I/O channels an I/O channel is a dedicated CPU for offloading I/O work from the main CPU
66Where to put I/O Functionality? Fig , p.426I/O functionality implemented using:application codekernel codedevice driver codeincreased flexibilityincreased development timeincreased efficiencyincreased development costincreased hiding from userdevice controller code (hardware)device code (hardware)