📄 p2
字号:
.SH3.6 I/O calls.PPThe 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 number of bytes written on it;no predetermination of the size of a file is necessaryor possible..PPTo illustrate the essentials of I/O,some of the basic calls aresummarized belowin an anonymous language that willindicate the required parameters without getting into theunderlyingcomplexities.Each call to the system may potentially result in an errorreturn, which for simplicity is not representedin the calling sequence..PPTo read or write a file assumed to exist already, it mustbe opened by the following call:.P1filep = open\|(\|name, flag\|).P2where.UL nameindicates the name of the file.An arbitrary path name may be given.The.UL flagargument indicates whether the file is to be read, written,or ``updated,'' that is, read and written simultaneously..PPThe returned value.UL filepis called a.IT "file descriptor" .It is a small integer used to identify the filein subsequent calls to read, write,or otherwise manipulate the file..PPTo create a new file or completely rewrite an old one,there is a.UL createsystem call thatcreates the given file if it does not exist,or truncates it to zero lengthif it does exist;.UL createalso opens the new file for writingand, like.UL open ,returns a file descriptor..PPThe file system maintains no locks visible to the user, 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 that anotheruser is reading,cannot prevent confusionwhen, for example, both users are editinga file with an editor that makesa copy of the file being edited..PPThere are, however,sufficient internal interlocks to maintainthe logical consistency of the file systemwhen two users engage simultaneously inactivities such as writing onthe same file,creating files in the same directory,or deleting each other's open files..PPExcept 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 theimmediately following byte.For each open file there is a pointer, maintainedinside the system,that indicates the next byte to be reador written.If.IT nbytes are read or written, the pointer advancesby.IT nbytes..PPOnce a file is open, the following callsmay be used:.P1n = read\|(\|filep, buffer, count\|)n = write\|(\|filep, buffer, count\|).P2Up to.UL countbytes are transmitted between the file specifiedby.UL filepand the byte arrayspecified by.UL buffer .The returned value.UL nis the number of bytes actually transmitted.In the.UL writecase,.UL nis the same as.UL countexcept under exceptional conditions, such as I/O errors orend of physical medium on special files;in a.UL read ,however,.UL nmay without error be less than.UL count .If the read pointer is so near the end of thefile that reading.UL countcharacterswould cause reading beyond the end, only sufficientbytes are transmitted to reach the end of thefile;also, typewriter-like terminalsnever return more than one line of input.When a.UL readcall returns with.UL nequalto zero, the end of the file has been reached.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 terminal by use of an escapesequence that depends on the device used..PPBytes written affect only those parts of a file 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 made to grow as needed..PPTo do random (direct-access) I/Oit is only necessary to move the read or write pointerto the appropriate location in the file..P1location = lseek\|(\|filep, offset, base\|).P2The pointerassociated with.UL filepis moved to a position.UL offsetbytes from the beginning of the file, from the current positionof the pointer, or from the end of the file,depending on.UL base..UL \&offsetmay be negative.For some devices (e.g., papertape andterminals) seek calls areignored.The actual offset from the beginning of the fileto which the pointer was moved is returnedin.UL location ..PPThere are several additional system entrieshaving to do with I/O and with the filesystem that 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..SHIV. IMPLEMENTATION OF THE FILE SYSTEM.PPAs mentioned in Section 3.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.IT i-number(for index number)of the file.When the file is accessed,its i-number is used as an index intoa system table (the.IT i-list \|)stored in a knownpart of the device on whichthe directory resides.The entry found thereby (the file's.IT i-node \|)containsthe description of the file:.IP ithe user and group-\*sID\*n of its owner.IP iiits protection bits.IP iiithe physical disk or tape addresses for the file contents.IP ivits size.IP vtime of creation, last use, and last modification.IP vithe number of links to the file, that is, the number of times it appears in a directory.IP viia code indicating whether the file is a directory, an ordinary file, or a special file..LPThe purpose of an.UL openor.UL createsystem 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.UL openor.UL create .Thus, during a subsequentcall to read or write thefile,the descriptormay be easily related to the information necessary to access the file..PPWhen a new file is created,an i-node is allocated for it and a directory entry is madethat 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 de-allocated..PPThe space on all disks thatcontain a file system is divided into a number of512-byteblocks logically addressed from 0 up to a limit thatdepends on the device.There is space in the i-node of each file for 13 device addresses.For nonspecial files,the first 10 device addresses point at the first10 blocks of the file.If the file is larger than 10 blocks,the 11 device address points to anindirect block containing up to 128 addressesof additional blocks in the file.Still larger files use the twelfth device addressof the i-node to point toa double-indirect block naming128 indirect blocks,eachpointing to 128 blocks of the file.If required,the thirteenth device address isa triple-indirect block.Thus files may conceptually grow to[\|(10+128+128\u\s62\s0\d+128\u\s63\s0\d)\*m512\|] bytes.Once opened,bytes numbered below 5120 can be read with a singledisk access;bytes in the range 5120 to 70,656require two accesses;bytes in the range 70,656to 8,459,264require three accesses;bytes from there to thelargest file(1,082,201,088)require four accesses.In practice,a device cache mechanism(see below)proves effective in eliminatingmost of the indirect fetches..PPThe 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 12 device address words are immaterial,and the first specifiesan internal.IT "device name" ,which is interpreted as a pair of numbersrepresenting,respectively, 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 terminal interfaces..PPIn this environment, the implementation of the.UL mountsystem call (Section 3.4) is quite straightforward..UL \&mountmaintains a system table whoseargument is the i-number and device name of theordinary file specifiedduring the.UL mount ,and whose corresponding value is thedevice name of the indicated special file.This table is searched for each i-number/device pairthat turns up while a path name is being scannedduring an.UL openor.UL create ;if a match is found,the i-number is replaced by the i-number of the rootdirectoryand the device name is replaced by the table value..PPTo the user, both reading and writing of files appear tobe synchronous and unbuffered.That is, immediately afterreturn from a.UL readcall the data are available; conversely,after a.UL writethe user's workspace may be reused.In fact, the system maintains a rather complicatedbuffering mechanism that reduces greatly the numberof I/O operations required to access a file.Suppose a.UL writecall is made specifying transmissionof a single byte.The systemwill search its buffers to seewhether the affected disk block currently resides in main 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.UL writecall 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..PPThe 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..PPA program that reads or writes files in units of 512 byteshas an advantage over a program that reads or writesa single byte at a time, but the gain is not immense;it comes mainly from the avoidance of system overhead.If a program is used rarely or doesno great volume of I/O, it may quite reasonablyread and write in units as small as it wishes..PPThe notion of the i-list is an unusual featureof.UX .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 namerelated 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, because 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,because all directory entries for a file have equal status.Charging the owner of a file is unfair in general,for 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.Many installationsavoid theissue by not charging any fees at all.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -