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

📄 p2

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
字号:
.s23.6 I/O calls.esThe system calls to do I/O are designed to eliminatethe differences between the various devices and styles ofaccess.There is no distinction between ``random''and ``sequential'' I/O, nor is any logical record size imposedby the system.The size of an ordinary file is determinedby the highest byte written on it;no predetermination of the size of a file is necessaryor possible..pgTo illustrate the essentials of I/O in \*sUNIX\*n, some of the basic calls aresummarized belowin an anonymous language which willindicate the required parameters without getting into thecomplexities of machine language programming.Each call to the system may potentially result in an errorreturn, which for simplicity is not representedin the calling sequence..pgTo read or write a file assumed to exist already, it mustbe opened by the following call:.dcfilep = open\|(\|name, flag\|).ec\fIName\fR indicates the name of the file.An arbitrary path name may be given.The \fIflag\fR argument indicates whether the file is to be read, written,or ``updated,'' that is read and written simultaneously..pgThe returned value.ft Ifilep.ft Ris called a.ft Ifile descriptor..ft RIt is a small integer used to identify the filein subsequent calls to read, writeor otherwise manipulate the file..pgTo create a new file or completely rewrite an old one,there is a \fIcreate\fR system call whichcreates the given file if it does not exist,or truncates it to zero lengthif it does exist.\fICreate\fR also opens the new file for writingand, like \fIopen,\fR returns a file descriptor..pgThere are no user-visible locks in the file system, nor is there anyrestriction on the number of users who may have a fileopen for reading or writing.Although it is possible for the contents of a fileto become scrambled when two users write on it simultaneously,in practice difficulties do not arise.We take the view that locks are neithernecessary nor sufficient, in our environment,to prevent interference between users of the same file.They are unnecessary because we are notfaced with large, single-file data basesmaintained by independent processes.They are insufficient becauselocks in the ordinary sense, wherebyone user is prevented from writing on a file which anotheruser is reading,cannot prevent confusionwhen, for example, both users are editinga file with an editor which makesa copy of the file being edited..pgIt should be said that the system hassufficient internal interlocks to maintainthe logical consistency of the file systemwhen two users engage simultaneously in such inconvenientactivities as writing onthe same file,creating files in the same directory,or deleting each other's open files..pgExcept as indicated below, reading and writingare sequential.This means that if a particularbyte in the file was the last byte written (or read),the next I/O call implicitly refers to thefirst following byte.For each open file there is a pointer, maintainedby the system,which indicates the next byte to be reador written.If \fIn\fR bytes are read or written, the pointer advancesby \fIn\fR bytes..pgOnce a file is open, the following callsmay be used..dcn = read\|(\|filep, buffer, count\|).dcn = write\|(\|filep, buffer, count\|).ecUp to.ft Icount.ft Rbytes are transmitted between the file specifiedby.ft Ifilep.ft Rand the byte arrayspecified by.ft Ibuffer..ft RThe returned value.ft In.ft Ris the number of bytes actually transmitted.In the.ft Iwrite.ft Rcase,.ft In.ft Ris the same as.ft Icount.ft Rexcept under exceptional conditions like I/O errors orend of physical medium on special files;in a.ft Iread,.ft Rhowever,.ft In.ft Rmay without error be less than.ft Icount..ft RIf the read pointer is so near the end of thefile that reading \fIcount\fR characterswould cause reading beyond the end, only sufficientbytes are transmitted to reach the end of thefile;also, typewriter-like devicesnever return more than one line of input.When a \fIread\fR call returns with \fIn\fR equalto zero, it indicates the end of the file.For disk files this occurs when the read pointerbecomes equal to the currentsize of the file.It is possible to generate an end-of-filefrom a typewriter by use of an escapesequence which depends on the device used..pgBytes written on a file affect only those implied bythe position of the write pointer and thecount; no other part of the fileis changed.If the last byte lies beyond the end of the file, thefile is grown as needed..pgTo do random (direct access) I/Oit is only necessary to move the read or write pointerto the appropriate location in the file..dclocation = seek\|(\|filep, offset, base\|).ecThe pointerassociated with \fIfilep\fR is moved to a position \fIoffset\fRbytes from the beginning of the file, from the current positionof the pointer, or from the end of the file,depending on.ft Ibase..ft R.ft IOffset.ft Rmay be negative.For some devices (e.g. papertape andtypewriters) seek calls areignored.The actual offset from the beginning of the fileto which the pointer was moved is returnedin \fIlocation\fR..s23.6.1 Other I/O calls.esThere are several additional system entrieshaving to do with I/O and with the filesystem which will not be discussed.For example:close a file,get the status of a file,change the protection mode or the ownerof a file,create a directory,make a link to an existing file,delete a file..s14.  Implementation of the file system.esAs mentioned in \(sc3.2 above, a directory entry containsonly a name for the associated file and a pointer to thefile itself.This pointer is an integer called the.ft Ii-number.ft(for index number)of the file.When the file is accessed,its i-number is used as an index intoa system table (the \fIi-list\|\fR) stored in a knownpart of the device on whichthe directory resides.The entry thereby found (the file's\fIi-node\fR\|)containsthe description of the file:.sp.tr |.in .5i.ti -\w'1. 'u1.|its owner;.ti -\w'1. 'u2.|its protection bits;.ti -\w'1. 'u3.|the physical disk or tape addresses for the file contents;.ti -\w'1. 'u4.|its size;.ti -\w'1. 'u5.|time of last modification;.ti -\w'1. 'u6.|the number of links to the file; that is, the numberof times it appears in a directory;.ti -\w'1. 'u7.|a bit indicating whether thefile is a directory;.ti -\w'1. 'u8.|a bit indicating whether the file is a special file;.ti -\w'1. 'u9.|a bit indicating whether the file is ``large'' or ``small.''.sp.tr ||.in 0The purpose of an.ft Iopen.ft Ror.ft Icreate.ft Rsystem call is to turn the path name given by the userinto an i-numberby searching the explicitly or implicitly named directories.Once a file is open,its device, i-number, and read/write pointer are stored in a system tableindexed by the file descriptor returned by the.ft Iopen.ft Ror.ft Icreate..ft RThus the file descriptor supplied during a subsequentcall to read or write thefilemay be easily related to the information necessary to access the file..pgWhen a new file is created,an i-node is allocated for it and a directory entry is madewhich contains the name of the file and the i-nodenumber.Making a link to an existing file involvescreating a directory entry with the new name,copying the i-number from the original file entry,and incrementing the link-count field of the i-node.Removing (deleting) a file is done bydecrementing thelink-count of the i-node specified by its directory entryand erasing the directory entry.If the link-count drops to 0,any disk blocks in the fileare freed and the i-node is deallocated..pgThe space on all fixed or removable disks whichcontain a file system is divided into a number of512-byteblocks logically addressed from 0 up to a limit whichdepends on the device.There is space in the i-node of each file for eight device addresses.A \fIsmall\fR (non-special) file fits into eight or fewerblocks; in this case the addresses of theblocks themselves are stored.For \fIlarge\fR (non-special) files, seven of the eight device addresses may pointto indirect blocks each containing 256 addressesfor the data blocks of the file.If required,the eighth word is the address of a double-indirect block containing 256 more addressesof indirect blocks.Thus files may conceptually grow to (7+256)\v'-.3'.\v'.3'256\v'-.3'.\v'.3'512 bytes;actually they are restricted to16,777,216(\|2\u\*t24\*n\d\|) bytes.Once opened, a small file (size 1 to 8 blocks)can be accessed directly.A large file (size 9 to 32768 blocks)requires one additional accessto read below logical block 1792(7\v'-.3'.\v'.3'256)and two additional references above 1792..pgThe foregoing discussion applies to ordinary files.When an I/O request is made to a file whose i-node indicates that itis special,the last seven device address words are immaterial,and the first is interpreted asa pair of bytes which constitute an internal.ft Idevice name..ft RThese bytes specifyrespectively a device typeand subdevice number.The device type indicates whichsystem routine will deal with I/O on that device;the subdevice number selects, for example, a disk driveattached to a particular controller or one of severalsimilar typewriter interfaces..pgIn this environment, the implementation of the.ft Imount.ft Rsystem call (\(sc3.4) is quite straightforward..ft IMount.ft Rmaintains a system table whoseargument is the i-number and device name of theordinary file specifiedduring the.ft Imount,.ft Rand whose corresponding value is thedevice name of the indicated special file.This table is searched for each (i-number, device)-pairwhich turns up while a path name is being scannedduring an.it openor.it create;if a match is found,the i-number is replaced by 1 (which is the i-number of the rootdirectory on all file systems),and the device name is replaced by the table value..pgTo the user, both reading and writing of files appear tobe synchronous and unbuffered.That is, immediately afterreturn from a \fIread\fR call the data are available, and converselyafter a \fIwrite\fR the user's workspace may be reused.In fact the system maintains a rather complicatedbuffering mechanism which reduces greatly the numberof I/O operations required to access a file.Suppose a \fIwrite\fR call is made specifying transmissionof a single byte.U\*sNIX\*n will search its buffers to seewhether the affected disk block currently resides in core memory;if not, it will be read in from the device.Then the affected byte is replaced in the buffer and anentry is made in a list of blocks to be written.The return from the \fIwrite\fR call may then take place,although the actual I/O may not be completed until a later time.Conversely, if a single byte is read, the system determineswhether the secondary storage block in which the byte is located is alreadyin one of the system's buffers; if so, the byte can be returned immediately.If not, the block is read into a buffer and the byte picked out..pgThe system recognizes whena program hasmade accesses tosequential blocks of a file,and asynchronouslypre-reads the next block.This significantly reducesthe running time of most programswhile adding little tosystem overhead..pgA program which reads or writes files in units of 512 byteshas an advantage over a program which reads or writesa single byte at a time, but the gain is not immense;it comes mainly from the avoidance of system overhead.A program which is used rarely or which doesno great volume of I/O may quite reasonablyread and write in units as small as it wishes..pgThe notion of the i-list is an unusual featureof \*sUNIX\*n.In practice, this method of organizing the file systemhas proved quite reliable and easy to deal with.To the system itself, one of its strengths isthe fact that each file has a short, unambiguous namewhich is related in a simple way to the protection, addressing,and other information needed to access the file.It also permits a quite simple and rapidalgorithm for checking the consistency of a file system,for example verificationthat the portions of each device containing useful informationand those free to be allocated are disjoint and togetherexhaust the space on the device.This algorithm is independentof the directory hierarchy, since it need only scanthe linearly-organized i-list.At the same time the notion of the i-list induces certainpeculiarities not found in other file system organizations.For example, there is the question of who is to be chargedfor the space a file occupies,since all directory entries for a file have equal status.Charging the owner of a file is unfair in general,since one user may create a file, another may link toit, and the first user may delete the file.The first user is still the owner of thefile, but it should be chargedto the second user.The simplest reasonably fair algorithmseems to be to spread the chargesequally among users who have links to a file.The current version of \*sUNIX\*n avoids theissue by not charging any fees at all..s24.1 Efficiency of the file system.esTo provide an indication of the overall efficiencyof \*sUNIX\*n and of the file system in particular,timings were made of the assembly ofa 8848-line program.The assembly was run alone on the machine;the total clock time was 32 seconds,for a rate of 276 lines per second.The time was divided as follows:66% assembler execution time,21% system overhead,13% disk wait time.We will not attempt any interpretation of these figuresnor any comparison with other systems, but merelynote that we aregenerally satisfied with the overall performanceof the system.

⌨️ 快捷键说明

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