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

📄 iosys

📁 unix v7是最后一个广泛发布的研究型UNIX版本
💻
📖 第 1 页 / 共 2 页
字号:
.TLThe UNIX I/O System.AUDennis M. Ritchie.AI.MH.PPThis paper gives an overview of the workings of the UNIX\(dg.FS\(dgUNIX is a Trademark of Bell Laboratories..FEI/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..PPIt 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.''A more detailed discussionappears in``UNIX Implementation;''the current document restates parts of that one,but is still more detailed.It is most useful inconjunction with a copy of the system code,since it is basically an exegesis of that code..SHDevice Classes.PPThere are two classes of device:.I blockand.I 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 forwardandbackward 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..PPCharacter-type devices have a muchmore straightforward interface, althoughmore work must be done by the driver itself..PPDevices of both types are named by a.I majorand a.I minordevice number.These numbers are generally stored as an integerwith the minor device numberin the low-order 8 bits and the major device numberin the next-higher 8 bits;macros.I majorand.I minorare available to access these numbers.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..PPThe major device numbers for block and character devicesare used as indices in separate tables;they both start at 0 and therefore overlap..SHOverview of I/O.PPThe purpose ofthe.I openand.I creatsystem calls is to set up entries in three separatesystem tables.The first of these is the.I u_ofiletable,which is stored in the system's per-processdata area.I u.This table is indexed bythe file descriptor returned by the.I openor.I creat,and is accessed duringa.I read,.I write,or other operation on the open file.An entry contains onlya pointer to the correspondingentry of the.I filetable,which is a per-system data base.There is one entry in the.I filetable for eachinstance of.I openor.I creat.This table is per-system because the same instanceof an open file must be shared among the several processeswhich can result from.I forksafter the file is opened.A.I 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.I inodetable,which contains a copy of the file's i-node..PPCertain open files can be designated ``multiplexed''files, and several other flags apply to suchchannels.In such a case, instead of an offset,there is a pointer to an associated multiplex channel table.Multiplex channels will not be discussed here..PPAn entry in the.I filetable corresponds precisely to an instance of.I openor.I creat;if the same file is opened several times,it will have severalentries in this table.However,there is at most one entryin the.I inodetable for a given file.Also, a file may enter the.I 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..PPAn entry in the.I 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.Also, the several block numbers that give addressinginformation for the file are expanded fromthe 3-byte, compressed format used on the disk to full.I longquantities..PPDuring the processing of an.I openor.I creatcall for a special file,the system always calls the device's.I openroutine to allow for any special processingrequired (rewinding a tape, turning onthe data-terminal-ready lead of a modem, etc.).However,the.I 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..PPWhen a.I reador.I writetakes place,the user's argumentsand the.I filetable entry are used to set up thevariables.I u.u_base,.I u.u_count,and.I 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 indirect blocks)to a physical block number; a block-typespecial file need not be mapped.This mapping is performed by the.I bmaproutine.In any event, the resulting physical block numberis used, as discussed below, toread or write the appropriate device..SHCharacter Device Drivers.PPThe.I cdevswtable specifies the interface routines present forcharacter devices.Each device provides five routines:open, close, read, write, and special-function(to implement the.I ioctlsystem call).Any of these may be missing.If a call on the routineshould be ignored,(e.g..I openon non-exclusive devices that require no setup)the.I cdevswentry can be given as.I nulldev;if it should be considered an error,(e.g..I writeon read-only devices).I nodevis used.For terminals,the.I cdevswstructure also contains a pointer to the.I ttystructure associated with the terminal..PPThe.I 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..PPThe.I 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.I close..PPWhen.I writeis called, it is supplied the deviceas argument.The per-user variable.I u.u_counthas been set tothe number of characters indicated by the user;for character devices, this number may be 0initially..I u.u_baseis the address supplied by the user from which to starttaking characters.The system may call theroutine internally, so theflag.I u.u_segflgis supplied that indicates,if.I on,that.I u.u_baserefers to the system address space instead ofthe user's..PPThe.I writeroutineshould copy up to.I u.u_countcharacters from the user's buffer to the device,decrementing.I u.u_countfor each character passed.For most drivers, which work one character at a time,the routine.I "cpass( )"is used to pick up charactersfrom the user's buffer.Successive calls on it returnthe characters to be written until.I u.u_countgoes to 0 or an error occurs,when it returns \(mi1..I Cpasstakes care of interrogating.I u.u_segflgand updating.I u.u_count..PPWrite routines which want to transfera probably large number of characters into an internalbuffer may also use the routine.I "iomove(buffer, offset, count, flag)"which is faster when many characters must be moved..I Iomovetransfers up to.I countcharacters into the.I bufferstarting.I offsetbytes from the start of the buffer;.I flagshould be.I 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,.I iomoveis much slower if any of.I "buffer+offset, count"or.I u.u_baseis odd..PPThe device's.I readroutine is called under conditions similar to.I write,except that.I u.u_countis guaranteed to be non-zero.To return characters to the user, the routine.I "passc(c)"is available; it takes care of housekeepinglike.I cpassand returns \(mi1 as the last characterspecified by.I u.u_countis returned to the user;before that time, 0 is returned..I Iomoveis also usable as with.I write;the flag should be.I B_READbut the same cautions apply..PPThe ``special-functions'' routineis invoked by the.I sttyand.I gttysystem calls as follows:.I "(*p) (dev, v)"where.I pis a pointer to the device's routine,.I devis the device number,and.I vis a vector.In the.I 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.I sttycase,.I vis 0;the device should take up to 3 words ofcontrol information fromthe array.I "u.u_arg[0...2].".PPFinally, 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 driverswhich deal with multiple similar devicesto encode the minor device number.After the interrupt has been processed,a return from the interrupt handler willreturn from the interrupt itself..PPA number of subroutines are available which are usefulto character device drivers.Most of these handlers, for example, need a placeto buffer characters in the internal interfacebetween their ``top half'' (read/write)and ``bottom half'' (interrupt) routines.For relatively low data-rate devices, the best mechanismis the character queue maintained by theroutines.I getcand.I putc.A queue header has the structure.DSstruct {	int	c_cc;	/* character count */	char	*c_cf;	/* first character */	char	*c_cl;	/* last character */} queue;.DEA character is placed on the end of a queue by.I "putc(c, &queue)"where.I cis the character and.I queueis the queue header.The routine returns \(mi1 if there is no spaceto put the character, 0 otherwise.The first character on the queue may be retrievedby.I "getc(&queue)"which returns either the (non-negative) characteror \(mi1 if the queue is empty..PPNotice that the space for characters in queues isshared among all devices in the systemand in the standard system there are only some 600character slots available.Thus device handlers,especially write routines, must takecare to avoid gobbling up excessive numbers of characters..PPThe other major help availableto device handlers is the sleep-wakeup mechanism.The call.I "sleep(event, priority)"causes the process to wait (allowing other processes to run)until the.I eventoccurs;at that time, the process is marked ready-to-runand the call will return when there is noprocess with higher.I priority..PPThe call.I "wakeup(event)"indicates that the.I eventhas happened, that is, causes processes sleepingon the event to be awakened.The.I eventis an arbitrary quantity agreed uponby the sleeper and the waker-up.By convention, it is the address of some data area usedby the driver, which guarantees that eventsare unique..PPProcesses sleeping on an event should not assumethat the event has really happened;they should check that the conditions which

⌨️ 快捷键说明

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