file management. 2 operating system components processor(s)main memorydevices process & resource...

101
File Management

Upload: peregrine-osborne

Post on 19-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

File Management

Page 2: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

2

Operating System Components

Processor(s) Main Memory Devices

Process &ResourceManager

MemoryManager

DeviceManager

FileManager

Computer Hardware

Operating System

Page 3: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

3

• Persistent storage• Shared device

Why Programmers Need Files

HTMLEditor

HTMLEditor

<head>…</head><body>…</body>

WebBrowser

WebBrowser

• Structured information• Can be read by any applic

• Accessibility• Protocol

<head>…</head><body>…</body>

<head>…</head><body>…</body>

foo.html

FileManager

FileManager

FileManager

FileManager

Page 4: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

4

Fig 13-2: The External View of the File Manager

Hardware

ApplicationProgram

ApplicationProgram

Fil

e M

gr

Dev

ice

Mgr

Mem

ory

Mgr

Pro

cess

Mgr

UNIXF

ile

Mgr

Dev

ice

Mgr

Mem

ory

Mgr

Pro

cess

Mgr

Windows

open()read()

close()

write()

lseek()

CreateFile()ReadFile()CloseHandle()

SetFilePointer()

WriteFile()mount()

Page 5: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

5

Introduction

• What is a file?

• Where is a file located physically?

• What are the steps to access a file?

Page 6: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

6

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?

Page 7: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

7

File system context

Page 8: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

8

Levels in a file system

Page 9: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

9

Levels of data abstraction

Page 10: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

10

Logical structures in a file

Page 11: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

11

Information Structure

Records

Applications

Structured Record Files

Record-Stream Translation

Stream-Block Translation

Byte Stream Files

Storage device

Page 12: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

12

Byte Stream File Interface

• Implements the block-stream interface• Info on file held in file descriptor

– described later

• Typical operations on file– fileID = open(fileName)– close(fileID)– read(fileID, buffer, length)– write(fileID, buffer, length)– seek(fileID, filePosition)

Page 13: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

13

Low Level Files

Stream-Block Translation

b0 b1 b2 bi......

fid = open(“fileName”,…);…read(fid, buf, buflen);…close(fid);

int open(…) {…}int close(…) {…}int read(…) {…}int write(…) {…}int seek(…) {…}

Storage device response to commands

Page 14: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

14

File meta-data

• File contain data plus information about the data, that is, meta-data– Meta-data is kept in a file descriptor

Page 15: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

15

File Descriptor Information

• 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

Page 16: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

16

File Descriptor in Unix

• File descriptor in UNIX is called an inode (index node), containing the following entries

Page 17: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

17

Structured Files

• A file is a stream of bytes

• Usually want to access in a structured manner– May have no structure imposed (UNIX)

• Must be provided by application

– May have a structure imposed (VMS)• Need to maintain additional information

– Type of file

– Access methods

– Other information

Page 18: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

18

Block Record Translation

Records

Record-Block Translation

Page 19: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

19

Record-Oriented Sequential Files

• A structured sequential file is a named sequence of logical records, indexed by nonnegative integers

• Records may be of fixed size, or variable size– This is determined by file manager

Logical Record

fileID = open(fileName)close(fileID)getRecord(fileID, record)putRecord(fileID, record)seek(fileID, position)

Page 20: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

20

Record-Oriented Sequential Files

...H byte header k byte logical record

Logical Record

• Header contains record descriptor information(occupies H bytes)

• Logical record takes up k bytes – fixed size

Next Header

Page 21: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

21

Record-Oriented Sequential Files

...H byte header k byte logical record

...

FragmentPhysical Storage Blocks

Logical Record

Page 22: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

22

Electronic Mail Examplestruct 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);}

Page 23: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

23

Record-Oriented Sequential Files

• Fixed size records can be a problem– Applications requiring large record sizes would

require that the programmer break the records into smaller pieces

– Applications only requiring small record sizes would waste space

• A solution is for the file system to be enhanced to include a function to define the record size for a file – encoded in header

Page 24: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

24

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)

Page 25: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

25

Indexed Sequential File (cont)

Account #012345123456294376...529366...965987

Index

ik

j

index = i

index = k

index = j

Application structure

Page 26: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

26

More Abstract Files

• Inverted files– System index for each datum in the file– Records accessed based on appearance in table

rather than their logical location• Company accounts may be accessed by customer

name, but customer may have several accounts• Set up external index table by name with pointers to

the main table

• Multimedia storage– Records contain radically different types– Access methods must be general

Page 27: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

27

Database Management Systems

• A database is a very highly structured set of information– Stored across different files – Optimized to minimize access time

• DBMSs implementation– Some DBMSs use the normal files provided by

the OS for generic use– Some use their own storage device block

Page 28: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

28

File systems

• File system– A data structure on a disk that holds files

• actually a file system is in a disk partition

• a technical term different from a “file system” as the part of the OS that implements files

• File systems in different OSs have different internal structures

Page 29: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

29

A file system layout

Page 30: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

30

Implementing Low Level Files

• Process needs to be able to read from and write to storage devices

• Simplest system is byte stream file system– (will consider record-oriented systems later)

• Storage device may be accessed 2 ways– Sequentially – like a tape drive– Randomly – like a magnetic disk

Page 31: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

31

Low-level File System Architecture

b0 b1 b2 b3 bn-1 … …

Block 0

...

Sequential Device Randomly Accessed Device

Page 32: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

32

Low Level Files Management

• 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

Page 33: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

33

File Manager Data Structures

External File Descriptor

Open FileDescriptor

Copy info from external to the open file descriptor

1

Process-FileSession

Keep the state of the process-file session

2

Return a reference to the data structure

3

Page 34: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

34

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

Page 35: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

35

A close Operation

• Completes all pending operations

• Release I/O buffers

• Release locks process holds on file

• Update external file descriptor

• Deallocate file status table entry

Page 36: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

36

Opening a UNIX File

fid = open(“fileA”, flags);…read(fid, buffer, len);

0 stdin1 stdout2 stderr3 ...

Open File Table

File structure

inode

Internal File Descriptor

On-Device File Descriptor

Page 37: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

37

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 bi is stored in block i/k

• The logical file is divided into logical blocks

• Each logical block is mapped to a physical disk block

Page 38: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

38

Locating file data

• The file descriptor contains data on how to perform this mapping– there are many methods for performing this

mapping

• Three basic strategies:– Contiguous allocation– Linked lists– Indexed allocation

Page 39: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

39

Dividing a file into blocks

Page 40: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

40

Disk Organization

Blk0Blk0 Blk1

Blk1 Blkk-1Blkk-1

BlkkBlkk Blkk+1

Blkk+1 Blk2k-1Blk2k-1

Track 0, Cylinder 0

Track 0, Cylinder 1

BlkBlk BlkBlk BlkBlk Track 1, Cylinder 0

BlkBlk BlkBlk BlkBlk Track N-1, Cylinder 0

BlkBlk BlkBlk BlkBlk Track N-1, Cylinder M-1

Boot Sector Volume Directory

Page 41: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

41

Contiguous Allocation

• Maps the N blocks into N contiguous blocks on the secondary storage device– Simple to implement– Random access

• Does not provide for dynamic file sizes– If you want to extend a file, hope there is an empty

block following, or recopy the entire file to a larger group of unallocated contiguous blocks

Head position 237…First block 785Number of blocks 25

File descriptor

Page 42: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

42

A contiguous file

Page 43: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

43

Keeping a file in pieces

• We need a block pointer for each logical block, an array of block pointers– block mapping indexes into this array– Each file is a linked list of disk blocks

• But where do we keep this array?– usually it is not kept as contiguous array– the array of disk pointers is like a second related

file (that is 1/1024 as big)

Page 44: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

44

Block pointers in the file descriptor

Page 45: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

45

Block pointers in contiguous disk blocks

Page 46: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

46

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 slowFirst block…

Head: 417...

Length

Byte 0

Byte 4095...

Length

Byte 0

Byte 4095...

Length

Byte 0

Byte 4095...

Block 0 Block 1 Block N-1

NULL

Page 47: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

47

Linked Lists – cont.

Page 48: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

48

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

Length

Length

Page 49: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

49

Block pointers in an index block

Page 50: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

50

Block pointers in an index block – cont.

Page 51: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

51

Chained index blocks

Page 52: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

52

Two-level index blocks

Page 53: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

53

Two-level index blocks – cont.

primary index

secondary index table data blocks

Page 54: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

54

File system layout variations

• New UNIX file systems use cylinder groups (mini-file systems) to achieve better locality of file data

• MS/DOS uses a FAT (file allocation table) file system– so does the Macintosh OS (although the MacOS

layout is different)

Page 55: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

UNIX FilesDatamode

owner…Direct block 0Direct block 1…Direct block 11Single indirectDouble indirectTriple indirect

inode

Data

Data

Index

Data

DataIndex

Index

Index

Index

Index

IndexIndex

Index

Data

Data

Data

Data

Page 56: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

56

DOS FAT Files

DiskBlock

File Descriptor

DiskBlock

DiskBlock

…43

107254

Logical Linked List

Page 57: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

57

DOS FAT Files

DiskBlock

File Descriptor

DiskBlock

DiskBlock

File Access Table (FAT)

DiskBlock

DiskBlock

DiskBlock

43

43

107

107

10743

254

254

254

File Descriptor

Page 58: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

58

Unallocated Blocks

• How should unallocated blocks be managed?

• Need a data structure to keep track of them– Block status map (or disk bitmap)

• Small enough to be held in primary memory

– Linked list (or free list)• Very large

• Hard to manage spatial locality (need to scan list to find blocks ‘close to’ each other)

Page 59: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

59

Free-Space Management

• Bit vector (n blocks)

0 1 2 n-1

bit[i] = 1 block[i] free

0 block[i] occupied

• First free block number

(number of bits per word) *(number of 0-value words) +offset of first 1 bit

Page 60: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

60

Free-Space Management - cont.

• Bit map requires extra space. Example:

block size = 212 bytes

disk size = 230 bytes (1 gigabyte)

n = 230/212 = 218 bits (or 32K bytes)

• Easy to get contiguous files

• Linked list (free list)– Cannot get contiguous space easily– No waste of space

Page 61: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

61

Free list organization

Page 62: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

62

Free-Space Management – cont.

• Need to protect:– Pointer to free list– Bit map

• Must be kept on disk• Copy in memory and disk may differ.• Cannot allow for block[i] to have a situation where

bit[i] = 0 in memory and bit[i] = 1 on disk.

– Solution:• Set bit[i] = 0 in disk.• Allocate block[i]• Set bit[i] = 0 in memory

Page 63: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

63

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

Page 64: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

64

Buffering

• Storage devices use Block I/O• Files place an explicit order on the bytes• Therefore, it is possible to predict what will be

read after bytei

• 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

Page 65: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

65

Supporting Other Storage Abstractions

• Low-level file systems avoid encoding record-level functionality– If applications use very large or very small

records, a generic file manager may be efficient– Some operating systems provide a higher-layer

file system to support applications with large or small files

– Database management systems and multimedia documents are examples

Page 66: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

66

Other Storage Abstractions

• Modern, open operating systems tend towards low-level file systems

• Proprietary operating systems designed for specific applications implement higher layer files systems

• Structured Sequential Records– Contain collections of logical records– Need to read from or write to entire records

Page 67: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

67

Other Storage Abstractions

• Indexed sequential files– File manager keeps table for each open file and

maps index to block containing the record– Consumes space– Read/write operations more complex– Buffering is not of much value (records accessed

in arbitrary order)

• Multimedia– Requires large files and high bandwidth

• Use larger block sizes• Try to use contiguous block allocation

Page 68: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

68

Directories

• A directory is a set of logically associated files and other directories of files– Directories are the mechanism we use to organize

files

• The file manager provides a set of commands to manage directories– Traverse a directory– Enumerate a list of all files and nested directories

Page 69: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

69

Directories

• Directory commands– enumerate– copy– rename– delete– traverse– etc.

Page 70: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

70

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.

Page 71: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

71

Directory Structures

Page 72: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

72

Directory Structures – cont.

Page 73: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

73

A directory tree

Page 74: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

74

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)

Page 75: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

75

Directory Implementation – cont.

• Sorted linear list of file names with pointers to the data blocks– simple to program– time-consuming to execute

• Hash Table – linear list with hash data structure– decreases directory search time– collisions – situations where two file names hash

to the same location– fixed size

Page 76: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

76

Directory Implementation – cont.

• Physical disk may be divided into two or more logical disks– Bitmap table doesn’t need to be as large– Easier to archive– Can handle several operating systems

• Requires partitioning at device driver level

Page 77: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

77

Mounting file systems

• Each file system has a root directory

• We can combine file systems by mounting– that is, link a directory in one file system to the

root directory of another file system

• This allows us to build a single tree out of several file systems

• This can also be done across a network, mounting file systems on other machines

Page 78: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

78

Mounting a file system

Page 79: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

79

UNIX mount Command

/

bin usr etc foo

bill nutt

abc

bar

blah

cde xyz

Page 80: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

80

UNIX mount Command

/

bin usr etc foo

bill nutt

abc

bar

blah

cde xyz

/

bin usr etc foo

bill nutt

abc

bar

blah

cde xyz

mount bar at foo

Page 81: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

More on Files

Page 82: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

82

File names

• Directory – Maps component names into objects (files or directories)

• Path name– A sequence of component names specifying a path of

directories• absolute path: starts at the root directory

• relative path: starts at the working directory

• File name extension: suffix of a component names that indicate the type of the file

• Alias: alternate path names for a file

Page 83: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

83

File name space topologies

Page 84: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

84

Some common file extensions

• file.c -- a C program file• file.txt -- a text file• file.s -- an assembly language file• file.obj -- an object file (in MS/DOS)• file.o -- an object file (in UNIX)• file.exe -- an executable file (in MS/DOS)• file.wk1 -- spreadsheet worksheet• file.tex -- tex or latex (a text formatter) file• file.mif -- Framemaker interchange file• file.scm -- Scheme program file• file.tmp -- a temporary file

Page 85: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

85

Path name examples

• /home/faculty/egle/os/book/ch02 – UNIX

• /home/student/jdoe/os/proj2 – UNIX

• book/ch02 –UNIX relative path name

• E:\egle\class\os\book\ch02 – MS/DOS

• users:u1:egle:os:book:ch02 – Macintosh

• disk$faculty:[egle.os.book]ch02 – VMS

• [.book]ch02 – VMS relative path name

Page 86: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

86

Open Files

• When a file is opened, the file manager keeps additional dynamic information – File position

Page 87: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

87

File Operations

• openFile = open(file name)

• openFile = create(file name)

• fileMetaData = status(file name)

• okay = access(file name, access type)

• okay = change mode(file name, new mode)– changes protection information

• okay = change owner(file name,new owner)

Page 88: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

88

Open file operations

• bytesRead = read(open file)• bytesWritten = write(open file)• newFilePos = seek(open file, how much,

how) -- move file position• close(open file)• openFile = duplicate(open file)• fileLock(open file)• fileControl(open file)• twoOpenFiles = pipe()

Page 89: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

89

Directory operations in UNIX

• link(file name, alias name)

• unlink(file name): delete file

• rename(old name, new name)

• makeDirectory(directory name)

• removeDirectory(directory name)

Page 90: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

90

Two major parts of a file system

The file manager needs to map a filename to a collectionof physical blocks on the storage devices

Page 91: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

91

File system data structures

Page 92: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

92

Flow of control for an open

Page 93: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

93

Flow of control for a read

Page 94: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

94

Connecting files and devices

Page 95: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

95

Special files

• Special files are not ordinary files– e.g. directories, devices, pipes, message queues,

remote files, mounted directories, etc.

• They are marked by flags in the file descriptor

• The read and write operations are directed to code for that type of special file– a case statement determines how to handle a read

or write operation

Page 96: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

96

Fork data structure changes

Page 97: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

97

System call data structure changes

Page 98: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

98

Duplicate data structure changes

Page 99: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

99

Pipe data structure changes

Page 100: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

100

Avoiding data copies

Page 101: File Management. 2 Operating System Components Processor(s)Main MemoryDevices Process & Resource Manager Memory Manager Device Manager File Manager Computer

101

Path name lookup algorithm