13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

230 File Structures Chapter 610 type11 OpenFileDescriptor =12 record13 Descriptor : ˆFileDescriptor; { points into disk-block cache }14 Mark : integer; { the next byte to be read or written }15 Mode : (ToRead, ToWrite, ToReadWrite);16 end17 end;In our case, Descriptor (line 13) points into the disk-block cache where the filedescriptor has been read, Mark (line 14) starts at 0, and Mode (line 15) is setToRead.Let us assume that a file entry in a directory is always 20 bytes long.The file layer now calls the byte layer to read the first 20 bytes from / and storethem in a 20-byte data structure (let us call it an entry descriptor) allocated forthis purpose:18 type19 EntryDescriptor =20 record21 Name : array [0..15] of char;22 FileDescriptorNumber : integer; { 4 bytes }23 end;(5) The byte layer, referring to the open-file descriptor for /, asks the block layer tofetch the first block (say 512 bytes) of /. It then copies the first 20 bytes to theentry descriptor, updates the file’s read mark, and returns to the file layer. If ithad been asked for bytes that span several blocks, it would have asked the blocklayer for each required block in turn and copied the relevant information. If thefile is large enough so the file descriptor (line 13) is insufficient to point to all theblocks, the byte layer can ask the block layer to read in overflow file descriptors.We will ignore how the byte layer works from now on.(6) Upon reading the entry descriptor, the file layer may discover that it does notdescribe /a but rather some other file, such as % or ˆ . In that case, the filelayer repeatedly asks the byte layer for another 20 bytes from / until it finds anentry describing /a. If it fails, the Open call was invalid, and an error return istaken to the process.(7) Having found a directory entry for /a, the file layer releases the temporaryopen-file descriptor for / and opens a new one for /a in a similar fashion.(8) The file layer follows a similar course until it has created an open-file descriptorfor /a/b, set to allow writing (line 15). The write mark (line 14) is initializedto 0. All temporary open-file descriptors are released by now. The index of thisopen-file descriptor is passed back to the calling process to use as a file number.(9) The process next submits a Position service call (line 6). The kernel forwardsthis call to the byte layer, which advances the write mark (line 14) to position700.(10) The process submits a Write service call (line 7). The byte layer asks the blocklayer to read in the second block of file /a/b, since the write mark (line 14)indicates that this is the appropriate block. This block now appears in the cacheof file blocks. The byte layer then copies 7 characters from the virtual space ofthe calling process into the cache copy and marks the copy as ‘‘dirty.’’ Thisblock will eventually be cleaned by being copied back to the disk.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!