《中科大操作系统原理与实现课件13_IO.pdf》由会员分享,可在线阅读,更多相关《中科大操作系统原理与实现课件13_IO.pdf(63页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、.操作系统原理与设计第13章 IO Systems(IO管理)陈香兰中国科学技术大学计算机学院2009年09月01日.提纲.I/O HardwarePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,S
2、pooling&device reservationError HandlingI/O ProtectionKernel Data StructuresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.OverviewII/O devicesIvary widelyIThe control of devices connected to the computer is a majorconcern of OS designers.How OS manages and controls various periphe
3、rals?.Outline.I/O HardwarePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&device reservationError HandlingI/O Prot
4、ectionKernel Data StructuresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.I/O Hardware IIIncredible variety of I/O devices.I/O Hardware IIICommon concepts:CPUPORTBUSControllerIPortIBus(daisy chain or shared direct access)IPCI(Peripheral Component Interconnect)ISCSI(Small computer
5、systems interface)IExpansion busIController(host adapter)IHow can the processor command controller?IController has one or more registers for data and controlsignals.IThe process communicates with the controller by reading andwriting bit patterns in the registers.II/O instructions control devicesITwo
6、 communication approaches.I/O Hardware IIIIDirect I/O instructionsIAccess the port addressIEach port typically contains of four registers,i.e.,status,control,data-in and data-out.Ie.g.,In,outIMemory-mapped I/OIe.g.,0 xa0000 0 xfffff are reserved to ISA graphics cards andBIOS routinesIDevice I/O Port
7、 Locations on PCs(partial).I/O Hardware IV.I/O Control Methods1.Polling2.Interrupts3.DMA4.(在汤书上:还有通道的概念).Polling IINeed handshakingIDetermines state of deviceIcommand-readyIIn command registerI1:a command is available for the controllerIbusyIIn status registerI0:ready for the next command;1:busyIErr
8、orITo indicate whether an I/O is ok.Polling IIIBasic handshaking notion for writing output1.Host repeatedly reads the busy bit until it is 02.Host sets write bit in command register and writes a byte intodata-out register3.Host sets command-ready bit4.When controller notices command-ready,sets busy
9、bit5.Controller gets write command and data,and works6.Controller clears command-ready bit,error bit and busy bitIStep1:Busy-wait cycle to wait for I/O from device polling.Interrupts IICPU Interrupt-request line triggered by I/O deviceIInterrupt handler receives interruptsIBasic interrupt schemeIRai
10、se Catch Dispatch Clear.Interrupts IIIInterrupt-Driven I/O Cycle.Interrupts IIIIMore sophisticated interrupt-handling featuresIDefer interrupt handlingIMaskable to ignore or delay some interruptsINonmaskableIEfficient dispatching without polling the devicesIInterrupt vector to dispatch interrupt to
11、correct handlerIInterrupt chaining:to allow more device&more interrupthandlersIDistinguish between high-and low-priority interruptsIInterrupt priorityIInterrupt mechanism also used for exceptions.Intel Pentium Processor Event-Vector Table.Direct Memory Access(DMA)IIUsed to avoid programmed I/O for l
12、arge data movement,and bypasses CPU to transfer data directly between I/Odevice and memoryIRequiresDMA controllerIthe host prepares a DMA command block in memoryIa pointer to the source of a transferIa pointer to the destination of the transferIa count of the number of bytes to be transferedICPU wri
13、tes the address of the DMA command block to DMAcontroller,and then goes on with other work.Direct Memory Access(DMA)IIIHandshaking between DMA controller&device controller1.Device controller raises DMA-request when one word isavailable2.DMA controller seizes memory bus,places the desiredaddress on m
14、emory-address wires,and raisesDMA-acknowledge3.Device controller transfers the word to memory,and removesthe DMA-request signal.Goto 14.DMA controller interrupts the CPU.Six Step Process to Perform DMA TransferICycle stealing:when DMA seizes the memory bus,CPU ismomentarily prevented from accessing
15、main memory.I/O hardware summaryIA busIA controllerIAn I/O port and its registersIThe handshaking relationship between the host and a devicecontrollerIThe execution of this handshaing in a pooling loop viainterruptsIthe offloading of this work to a DMA controller for largetransfer.Outline.I/O Hardwa
16、rePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&device reservationError HandlingI/O ProtectionKernel Data Struct
17、uresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.I/O control challengesIWide variety of devicesITwo challengesApplications OS DevicesIHow can the OS give a convenient,uniform I/O interface toapplications?IHow can the OS be designed such that new devices can beattached to the comp
18、uter without the OS being rewritten?.OSdeviceIFor device manufacturers,device-driver layer hidesdifferences among I/O controllers from kernelIA Kernel I/O Structure.Application I/O InterfaceIFor applications,I/O system calls encapsulate devicebehaviors in generic classesI设备独立性:应用程序与具体的物理设备无关。IDevice
19、-driver layer hides differences among I/O controllersfrom kernelIDevices vary in many dimensionsICharacter-stream or blockISequential or random-accessISharable or dedicatedISpeed of operationIread-write,read only,or write only.Characteristics of I/O Devices.Major Device Access ConventionsIBlock I/OI
20、Character-stream I/OIMemory-mapped file accessINetwork socketsIClock and Time.Block and Character DevicesIBlock devices include disk drivesICommands include read,write,seekIRaw I/O or file-system accessIMemory-mapped file access possibleICharacter devices include keyboards,mice,serial portsICommands
21、 include get,putILibraries layered on top allow line editing.Network DevicesIVarying enough from block and character to have owninterfaceIUnix and Windows NT/9x/2000 include socket interfaceISeparates network protocol from network operationIServer socket,bind,listen,acceptIClient socket,connectIIncl
22、udes select functionalityIApproaches vary widely(pipes,FIFOs,streams,queues,mailboxes).Clocks and TimersIProvide current time,elapsed time,timerIHardware clocksIReal Time Clock(RTC)ITime Stamp Counter(TSC)IProgrammable Interval Timer(PIT,8253 or 8254)Iused for timings,periodic interruptsIioctl(on UN
23、IX)covers odd aspects of I/O such as clocks andtimers.RTCIIntegrated with CMOS RAM,always tick.ISeconds from 00:00:00 January 1,1970 UTCICan be used as an alarm clockIIRQ8IInterrupt frequency:2HZ8192HZII/O address(port no):0 x70,0 x71IExample:IMotorola 146818:CMOS RAM+RTCISecond year,month,date,week
24、 HOW?.TSCI64bit TSC register in the processorIPentium and afterIIncremented at each clock signal on CLK input pinIexample:CPU frequency 400MHZadds 1 per 2.5 ns=adds 400 106per secondIInstruction:rdtscIHow to know CPU frequency?.PITI8253,8254IIssues time interrupt in a programmable time internalICan
25、also be used to calculate processor frequency during bootup.I8253I14,3178 MHz crystal 4,772,727 Hz system clock1,193,180 Hz to 8253Iusing 16 bit divisor interrupt every 838 ns 54.925493 ms.Blocking and Nonblocking I/OIBlocking-process suspended until I/O completedIEasy to use and understandIInsuffic
26、ient for some needsINonblocking-I/O call returns as much as availableIUser interface,data copy(buffered I/O)IImplemented via multi-threadingIReturns quickly with count of bytes read or writtenIAsynchronous-process runs while I/O executesIDifficult to useII/O subsystem signals process when I/O comple
27、ted.Two I/O Methods.Outline.I/O HardwarePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&device reservationError Ha
28、ndlingI/O ProtectionKernel Data StructuresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.Kernel I/O Subsystem ServicesII/O SchedulingIBufferingICachingISpoolingIDevice reservationIError handling.I/O Scheduling IITo schedule a set of I/O requests means to determine a goodorder in wh
29、ich to execute themIissued rarely issuing order is the best choiceISchedulingIcan improve overall system performanceIcan share device access fairly among processesIcan reduce the average waiting time for I/O to completeISome I/O request ordering via per-device queueISome OSes try fairness,some notIE
30、.g.,Disk read:I0,50,100,now at 100IOS maintaining a wait queue of request for each deviceIDevice-status Table.I/O Scheduling IIIAnother way to improve performance is by using storage spacein main memory or on diskIBufferingICachingISpooling.Buffering(缓冲机制)IBufferingIBuffer A memory area that stores
31、data while they aretransferred between two devices or between a device and anapplicationIStore data in memory while transferring between devicesIWhy buffering?ITo cope with device speed mismatchITo cope with device transfer size mismatchITo maintain“copy semantics”.Sun Enterprise 6000 Device-Transfe
32、r Rates.Buffering IISingle buffer单缓冲I用户区(OS,时间M)缓冲区(设备,时间T)设备I假设计算时间为CICurrent C 与next T可并行,max(C,T)+M.Buffering IIIDouble buffer(双缓冲)I max(C,T);连续输入(C T)I缓冲的另一种用法:用于两个机器之间的通信.Buffering III.Buffering IVICircular bufferI多个(种)缓冲区多个缓冲区指针I空缓冲区和Nexti;满缓冲区和Nextg;当前消费中的缓冲区CurrentI类似前面介绍的经典的具有有限缓冲区的生产者和消费者问
33、题IBuffer poolI前三种,缓冲区是专用的I为提高缓冲区利用率:设置公共的缓冲池.Caching,Spooling&device reservationICaching-fast memory holding copy of data(前面讲过)IAlways just a copyIKey to performanceISpooling-hold output for a device(前面也讲过)IDedicated device can serve only one request at a timeISpooling is a way of dealing with I/O d
34、evices in amultiprogramming systemIi.e.,PrintingIDevice reservation-provides exclusive access to a deviceISystem calls for allocation and deallocationIWatch out for deadlock.Spooling IIOut-line I/O(脱机I/O),使用外围机ISPOOL:Simultaneous Peripheral Operation On-Line(外部设备联机并行操作,假脱机)IDedicated device sharable
35、 deviceIUsing processes of multiprogramming systemIStructureIInput-well,output-wellIInput-buffer,output-bufferIInput-process SPin,output-process SPoutIRequested-queue.Spooling II.Error HandlingIOS can recover from disk read,device unavailable,transientwrite failuresIe.g.:read()again,resend(),.,accor
36、ding to some sepecifiedrulesIMost return an error number or code when I/O request failsISystem error logs hold problem reports.I/O Protection IIUser process may accidentally or purposefully attempt todisrupt normal operation via illegal I/O instructionsIto prevent users from performing illegal I/OIA
37、ll I/O instructions defined to be privilegedII/O must be performed via system callsIMemory-mapped and I/O port memory locations must beprotected tooIUse of a System Call to Perform I/O(figure).I/O Protection II.Kernel Data Structures IIKernel keeps state info for I/O components,including openfile ta
38、bles,network connections,character device stateIMany,many complex data structures to track buffers,memoryallocation,“dirty”blocksISome use object-oriented methods and message passing toimplement I/OIExample:UNIX I/O Kernel Structure.Kernel Data Structures II.Outline.I/O HardwarePollingInterruptsDire
39、ct Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&device reservationError HandlingI/O ProtectionKernel Data StructuresTransforming I/O Re
40、quests to Hardware OperationsPerformance小结和作业.I/O Requests to Hardware OperationsIConsider reading a file from disk for a process:IDetermine device holding fileITranslate name to device representationIPhysically read data from disk into bufferIMake data available to requesting processIReturn control
41、 to process.Life Cycle of An I/O Request.Outline.I/O HardwarePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&devic
42、e reservationError HandlingI/O ProtectionKernel Data StructuresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.PerformanceII/O a major factor in system performance:IDemands CPU to execute device driver,kernel I/O codeIContext switches due to interruptsIData copyingINetwork traffic e
43、specially stressful.Intercomputer Communications.Improving PerformanceIReduce number of context switchesIReduce data copyingIReduce interrupts by using large transfers,smart controllers,pollingIUse DMAIBalance CPU,memory,bus,and I/O performance for highestthroughput.Device-Functionality Progression.
44、Outline.I/O HardwarePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&device reservationError HandlingI/O Protection
45、Kernel Data StructuresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.小结.I/O HardwarePollingInterruptsDirect Memory Access(DMA)I/O hardware summaryApplication I/O InterfaceBlock and Character DevicesNetwork DevicesClocks and TimersBlocking and Nonblocking I/OKernel I/O SubsystemI/O SchedulingBufferingCaching,Spooling&device reservationError HandlingI/O ProtectionKernel Data StructuresTransforming I/O Requests to Hardware OperationsPerformance小结和作业.作业I设备控制方式有哪几种?I脱机I/O和SPOOLingI为什么要引入缓冲机制?有哪几种缓冲机制?.谢谢!
限制150内