⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iosys

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
📖 第 1 页 / 共 3 页
字号:
.lg.tr _\(ul.pl 11i.ll 6.5i.ps 10.vs 11p.br.tl '-'''.de pg.sp .5...de it.ft I\\$1.ft R...de bd.ft B\\$1.ft R...de he.tl '-''''sp .5i.ft I.if e  'tl '% - Unix I/O System'''.if o 'tl '''Unix I/O System - %'.ft P'sp .5i'ns...de fo'bp...de MS.ne 4.sp.ft B\\$1.pg.ft R...de ms.ne 4.sp.ft I\\$1.pg.ft R...sp 1.5i.wh 0 he.wh -1i fo.ps 16.ceThe Unix I/O System.sp .25i.ps 10.ft I.ce 2Dennis M. RitchieBell Telephone Laboratories.ft R.ps 10.sp .7iThis paper gives an overview of the workings of the UnixI/O system.It was written with an eye toward providingguidance to writers of device driver routines,and is oriented more toward describing the environmentand nature of device drivers than the implementationof that part of the file system which deals withordinary files..pgIt is assumed that the reader has a good knowledgeof the overall structure of the file system as discussedin the paper ``The Unix Time-sharing System.''Moreover the present document is intended to be used inconjunction with a copy of the system code,since it is basically an exegesis of that code..MS "Device Classes"There are two classes of device:.it blockand.it character.The block interface is suitable for deviceslike disks, tapes, and DECtapewhich work, or can work, with addressible 512-byte blocks.Ordinary magnetic tape just barely fits in this category,since by use of forwardbackward spacing any block can be read, even thoughblocks can be written only at the end of the tape.Block devices can at least potentially contain a mountedfile system.The interface to block devices is very highly structured;the drivers for these devices share a great many routinesas well as a pool of buffers..pgCharacter-type devices have a muchmore straightforward interface, althoughmore work must be done by the driver itself..pgDevices of both types are named by a.it majorand a.it minordevice number.These numbers are generally stored as a wordwith the minor device numberas the low byte and the major device numberas the high byte.The major device number selects which driver will deal withthe device; the minor device number is not usedby the rest of the system but is passed to thedriver at appropriate times.Typically the minor numberselects a subdevice attached toa given controller, or one ofseveral similar hardware interfaces..pgThe major device numbers for block and character devicesare used as indices in separate tables;they both start at 0 and therefore overlap..MS "Overview of I/O"The purpose ofthe.it openand.it creatsystem calls is to set up entries in three separatesystem tables.The first of these is the.it u_ofiletable,which is stored in the system's per-processdata area.it u.This table is indexed bythe file descriptor returned by the.it openor.it creat,and is accessed duringa.it read,.it write,or other operation on the open file.An entry contains onlya pointer to the correspondingentry of the.it filetable,which is a per-system data base.There is one entry in the.it filetable for eachinstance of.it openor.it creat.This table is per-system because the same instanceof an open file must be shared among the several processeswhich can result from.it forksafter the file is opened.A.it filetable entry containsflags which indicate whether the filewas open for reading or writing or is a pipe, anda count which is used to decide when all processesusing the entry have terminated or closed the file(so the entry can be abandoned).There is also a 32-bit file offsetwhich is used to indicate where in the file the next reador write will take place.Finally, there is a pointer to theentry for the file in the.it inodetable,which contains a copy of the file's i-node.Notice that an entry in the.it filetable corresponds precisely to an instance of.it openor.it creat;if the same file is opened several times,it will have severalentries in this table.However,there is at most one entryin the.it inodetable for a given file.Also, a file may enter the.it inodetable not only because it is open,but also because it is the current directoryof some process or because itis a special file containing a currently-mountedfile system..pgAn entry in the.it inodetable differs somewhat from thecorresponding i-node as stored on the disk;the modified and accessed times are not stored,and the entry is augmentedby a flag word containing information about the entry,a count used to determine when it may beallowed to disappear,and the device and i-numberwhence the entry came..pgDuring the processing of an.it openor.it creatcall for a special file,the system always calls the device's.it openroutine to allow for any special processingrequired (rewinding a tape, turning onthe data-terminal-ready lead of a modem, etc.).However,the.it closeroutine is called only when the lastprocess closes a file,that is, when the i-node table entryis being deallocated.Thus it is not feasiblefor a device to maintain, or depend on,a count of its users, although it is quitepossible toimplement an exclusive-use device which cannotbe reopened until it has been closed..pgWhen a.it reador.it writetakes place,the user's argumentsand the.it filetable entry are used to set up thevariables.it u.u_base,.it u.u_count,and.it u.u_offsetwhich respectively contain the (user) addressof the I/O target area, the byte-count for the transfer,and the current location in the file.If the file referred to isa character-type special file, the appropriate reador write routine is called; it is responsiblefor transferring data and updating thecount and current location appropriatelyas discussed below.Otherwise, the current location is used to calculatea logical block number in the file.If the file is an ordinary file the logical blocknumber must be mapped (possibly using an indirect block)to a physical block number; a block-typespecial file need not be mapped.In any event, the resulting physical block numberis used, as discussed below, toread or write the appropriate device..MS "Character device drivers"The.it cdevswtable specifies the interface routines present forcharacter devices.Each device provides five routines:open, close, read, write, and special-function.Any of these may be missing.If a call on the routineshould be ignored,(e.g..it openon non-exclusive devices which require no setup)the.it cdevswentry can be given as.it nulldev;if it should be considered an error,(e.g..it writeon read-only devices).it nodevis used..pgThe.it openroutine is called each time the fileis opened with the full device number as argument.The second argument is a flag which isnon-zero only if the device is to be written upon..pgThe.it closeroutine is called only when the fileis closed for the last time,that is when the very last process inwhich the file is open closes it.This means it is not possible for the driver tomaintain its own count of its users.The first argument is the device number;the second is a flag which is non-zeroif the file was open for writing in the process whichperforms the final.it close..pgWhen.it writeis called, it is supplied the deviceas argument.The per-user variable.it u.u_counthas been set tothe number of characters indicated by the user;for character devices, this number may be 0initially..it u.u_baseis the address supplied by the user from which to starttaking characters.The system may call theroutine internally, so theflag.it u.u_segflgis supplied which indicates,if.it on,that.it u.u_baserefers to the system address space instead ofthe user's..pgThe.it writeroutineshould copy up to.it u.u_countcharacters from the user's buffer to the device,decrementing.it u.u_countfor each character passed.For most drivers, which work one character at a time,the routine.pg.ti 5.it "cpass( )".pgis used to pick up charactersfrom the user's buffer.Successive calls on it returnthe characters to be written until.it u.u_countgoes to 0 or an error occurs,when it returns \(mi1..it Cpasstakes care of interrogating.it u.u_segflgand updating.it u.u_count..pgWrite routines which want to transfera probably large number of characters into an internalbuffer may also use the routine.pg.ti 5.it "iomove(buffer, offset, count, flag)".pgwhich is faster when many characters must be moved..it Iomovetransfers up to.it countcharacters into the.it bufferstarting.it offsetbytes from the start of the buffer;.it flagshould be.it B_WRITE(which is 0) in the write case.Caution:the caller is responsible for making surethe count is not too large and is non-zero.As an efficiency note,.it iomoveis much slower if any of.it "buffer+offset, count"or.it u.u_baseis odd..pgThe device's.it readroutine is called under conditions similar to.it write,except that.it u.u_countis guaranteed to be non-zero.To return characters to the user, the routine.pg.ti 5.it "passc(c)".pgis available; it takes care of housekeepinglike.it cpassand returns \(mi1 as the last characterspecified by.it u.u_countis returned to the user;before that time, 0 is returned..it Iomoveis also usable as with.it write;the flag should be.it B_READbut the same cautions apply..pgThe ``special-functions'' routineis invoked by the.it sttyand.it gttysystem calls as follows:.pg.ti 5.it "(*p) (dev, v)".pgwhere.it pis a pointer to the device's routine,.it devis the device number,and.it vis a vector.In the.it gttycase,the device is supposed to place up to 3 words of status informationinto the vector; this will be returned to the caller.In the.it sttycase,.it vis 0;the device should take up to 3 words ofcontrol information fromthe array.it "u.u_arg[0...2].".pgFinally, each device should have appropriate interrupt-timeroutines.When an interrupt occurs, it is turned into a C-compatible callon the devices's interrupt routine.The interrupt-catching mechanism makesthe low-order four bits of the ``new PS'' word in thetrap vector for the interrupt availableto the interrupt handler.This is conventionally used by drivers

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -