Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13.

Similar presentations


Presentation on theme: "Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13."— Presentation transcript:

1 Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13

2 Slide 13-2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

3 Slide 13-3 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Fig 13-2: The External View of the File Manager Hardware Application Program Application Program File MgrDevice MgrMemory Mgr Process Mgr UNIX File MgrDevice MgrMemory Mgr Process Mgr Windows open() read() close() write() lseek() CreateFile() ReadFile() CloseHandle() SetFilePointer() WriteFile() mount()

4 Slide 13-4 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Persistent storage Shared device Why Programmers Need Files HTML Editor HTML Editor … … Web Browser Web Browser Structured information Can be read by any applic Accessibility Protocol … … … … foo.html File Manager File Manager File Manager File Manager

5 Slide 13-5 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 File Management File is a named, ordered collection of information The file manager administers the collection by: –Storing the information on a device –Mapping the block storage to a logical view –Allocating/deallocating storage –Providing file directories What abstraction should be presented to programmer?

6 Slide 13-6 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Information Structure Records Applications Structured Record Files Record-Stream Translation Stream-Block Translation Byte Stream Files Storage device

7 Slide 13-7 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Byte Stream File Interface fileID = open(fileName) close(fileID) read(fileID, buffer, length) write(fileID, buffer, length) seek(fileID, filePosition)

8 Slide 13-8 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Low Level Files Stream-Block Translation b0b0 b1b1 b2b2 bibi... fid = open(“fileName”,…); … read(fid, buf, buflen); … close(fid); int open(…) {…} int close(…) {…} int read(…) {…} int write(…) {…} int seek(…) {…} Storage device response to commands

9 Slide 13-9 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Structured Files Records Record-Block Translation

10 Slide 13-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Record-Oriented Sequential Files Logical Record fileID = open(fileName) close(fileID) getRecord(fileID, record) putRecord(fileID, record) seek(fileID, position)

11 Slide 13-11 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Record-Oriented Sequential Files... H byte headerk byte logical record Logical Record

12 Slide 13-12 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Record-Oriented Sequential Files... H byte headerk byte logical record... Fragment Physical Storage Blocks Logical Record

13 Slide 13-13 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Electronic Mail Example struct message { /* The mail message */ address to; address from; line subject; address cc; string body; }; struct message *getRecord(void) { struct message *msg; msg = allocate(sizeof(message)); msg->to = getAddress(...); msg->from = getAddress(...); msg->cc = getAddress(...); msg->subject = getLine(); msg->body = getString(); return(msg); } putRecord(struct message *msg) { putAddress(msg->to); putAddress(msg->from); putAddress(msg->cc); putLine(msg->subject); putString(msg->body); }

14 Slide 13-14 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Indexed Sequential File Suppose we want to directly access records Add an index to the file fileID = open(fileName) close(fileID) getRecord(fileID, index) index = putRecord(fileID, record) deleteRecord(fileID, index)

15 Slide 13-15 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Indexed Sequential File (cont) Account # 012345 123456 294376... 529366... 965987 Index i k j index = i index = k index = j Application structure

16 Slide 13-16 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 More Abstract Files Inverted files –System index for each datum in the file Databases –More elaborate indexing mechanism –DDL & DML Multimedia storage –Records contain radically different types –Access methods must be general

17 Slide 13-17 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Implementing Low Level Files Secondary storage device contains: –Volume directory (sometimes a root directory for a file system) –External file descriptor for each file –The file contents Manages blocks –Assigns blocks to files (descriptor keeps track) –Keeps track of available blocks Maps to/from byte stream

18 Slide 13-18 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Disk Organization Blk 0 Blk 1 Blk k-1 Blk k Blk k+1 Blk 2k-1 Track 0, Cylinder 0 Track 0, Cylinder 1 Blk Track 1, Cylinder 0 Blk Track N-1, Cylinder 0 Blk Track N-1, Cylinder M-1 … … … … … … … … Boot SectorVolume Directory

19 Slide 13-19 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Low-level File System Architecture b 0 b 1 b 2 b 3 b n-1 …… Block 0... Sequential Device Randomly Accessed Device

20 Slide 13-20 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 File Descriptors External name Current state Sharable Owner User Locks Protection settings Length Time of creation Time of last modification Time of last access Reference count Storage device details

21 Slide 13-21 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 An open() Operation Locate the on-device (external) file descriptor Extract info needed to read/write file Authenticate that process can access the file Create an internal file descriptor in primary memory Create an entry in a “per process” open file status table Allocate resources, e.g., buffers, to support file usage

22 Slide 13-22 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 File Manager Data Structures External File Descriptor Open File Descriptor Copy info from external to the open file descriptor 1 Process-File Session Keep the state of the process- file session 2 Return a reference to the data structure 3

23 Slide 13-23 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Opening a UNIX File fid = open(“fileA”, flags); … read(fid, buffer, len); 0 stdin 1 stdout 2 stderr 3... Open File Table File structure inode Internal File Descriptor On-Device File Descriptor

24 Slide 13-24 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Block Management The job of selecting & assigning storage blocks to the file For a fixed sized file of k blocks –File of length m requires N =  m/k  blocks –Byte b i is stored in block  i/k  Three basic strategies: –Contiguous allocation –Linked lists –Indexed allocation

25 Slide 13-25 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Contiguous Allocation Maps the N blocks into N contiguous blocks on the secondary storage device Difficult to support dynamic file sizes Head position237 … First block785 Number of blocks25 File descriptor

26 Slide 13-26 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Linked Lists Each block contains a header with –Number of bytes in the block –Pointer to next block Blocks need not be contiguous Files can expand and contract Seeks can be slow First block … Head: 417... Length Byte 0 Byte 4095... Length Byte 0 Byte 4095... Length Byte 0 Byte 4095... Block 0Block 1Block N-1

27 Slide 13-27 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Indexed Allocation Extract headers and put them in an index Simplify seeks May link indices together (for large files) Index block … Head: 417... Byte 0 Byte 4095... Byte 0 Byte 4095... Byte 0 Byte 4095... Block 0 Block 1 Block N-1 Length

28 Slide 13-28 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 DOS FAT Files Disk Block File Descriptor Disk Block Disk Block … 43 107 254 File Access Table (FAT) Disk Block Disk Block Disk Block … 43 107 43 254 File Descriptor

29 Slide 13-29 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 UNIX Files Data mode owner … Direct block 0 Direct block 1 … Direct block 11 Single indirect Double indirect Triple indirect inode Data Index Data Index Data Index Data

30 Slide 13-30 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Unallocated Blocks How should unallocated blocks be managed? Need a data structure to keep track of them –Linked list Very large Hard to manage spatial locality –Block status map (“disk map”) Bit per block Easy to identify nearby free blocks Useful for disk recovery

31 Slide 13-31 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Marshalling the Byte Stream Must read at least one buffer ahead on input Must write at least one buffer behind on output Seek  flushing the current buffer and finding the correct one to load into memory Inserting/deleting bytes in the interior of the stream

32 Slide 13-32 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Full Block Buffering Storage devices use block I/O Files place an explicit order on the bytes Therefore, it is possible to predict what is likely to be read after byte i When file is opened, manager reads as many blocks ahead as feasible After a block is logically written, it is queued for writing behind, whenever the disk is available Buffer pool – usually variably sized, depending on virtual memory needs –Interaction with the device manager and memory manager

33 Slide 13-33 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Directories A set of logically associated files and sub directories File manager provides set of controls: –enumerate –copy –rename –delete –traverse –etc.

34 Slide 13-34 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Directory Structures How should files be organized within directory? –Flat name space All files appear in a single directory –Hierarchical name space Directory contains files and subdirectories Each file/directory appears as an entry in exactly one other directory -- a tree Popular variant: All directories form a tree, but a file can have multiple parents.

35 Slide 13-35 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 Directory Implementation Device Directory –A device can contain a collection of files –Easier to manage if there is a root for every file on the device -- the device root directory File Directory –Typical implementations have directories implemented as a file with a special format –Entries in a file directory are handles for other files (which can be files or subdirectories)

36 Slide 13-36 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 UNIX mount Command / binusretcfoo billnutt abc / blah cdexyz FS / binusretcfoo billnutt abc / blah cdexyz mount FS at foo FS

37 Slide 13-37 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 VFS-based File Manager File System Independent Part of File Manager File System Independent Part of File Manager Exports OS-specific API Virtual File System Switch MS-DOS Part of File Manager MS-DOS Part of File Manager ISO 9660 Part of File Manager ISO 9660 Part of File Manager ext2 Part of File Manager ext2 Part of File Manager …


Download ppt "Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13."

Similar presentations


Ads by Google