slide 13-1 copyright © 2004 pearson education, inc. operating systems: a modern perspective,...

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

Upload: madeline-nelson

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-1

Copyright © 2004 Pearson Education, Inc.

13FileManagement

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-2

Copyright © 2004 Pearson Education, Inc.

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 3: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-3

Copyright © 2004 Pearson Education, Inc.

• 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: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-4

Copyright © 2004 Pearson Education, Inc.

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 5: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-5

Copyright © 2004 Pearson Education, Inc.

Information Structure

Records

Applications

Structured Record Files

Record-Stream Translation

Stream-Block Translation

Byte Stream Files

Storage device

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-6

Copyright © 2004 Pearson Education, Inc.

Byte Stream File Interface

fileID = open(fileName)close(fileID)read(fileID, buffer, length)write(fileID, buffer, length)seek(fileID, filePosition)

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-7

Copyright © 2004 Pearson Education, Inc.

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 8: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-8

Copyright © 2004 Pearson Education, Inc.

Structured Files

Records

Record-Block Translation

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-9

Copyright © 2004 Pearson Education, Inc.

Record-Oriented Sequential Files

Logical Record

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

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-10

Copyright © 2004 Pearson Education, Inc.

Record-Oriented Sequential Files

...H byte header k byte logical record

Logical Record

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-11

Copyright © 2004 Pearson Education, Inc.

Record-Oriented Sequential Files

...H byte header k byte logical record

...

FragmentPhysical Storage Blocks

Logical Record

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-12

Copyright © 2004 Pearson Education, Inc.

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 13: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-13

Copyright © 2004 Pearson Education, Inc.

Indexed Sequential File (cont)

Account #012345123456294376...529366...965987

Index

ik

j

index = i

index = k

index = j

Application structure

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-14

Copyright © 2004 Pearson Education, Inc.

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 15: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-15

Copyright © 2004 Pearson Education, Inc.

Low-level File System Architecture

b0 b1 b2 b3 bn-1 … …

Block 0

...

Sequential Device Randomly Accessed Device

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-16

Copyright © 2004 Pearson Education, Inc.

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

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-17

Copyright © 2004 Pearson Education, Inc.

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 18: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-18

Copyright © 2004 Pearson Education, Inc.

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 19: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-19

Copyright © 2004 Pearson Education, Inc.

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 20: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-20

Copyright © 2004 Pearson Education, Inc.

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

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

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-21

Copyright © 2004 Pearson Education, Inc.

Contiguous Allocation

• Maps the N blocks into N contiguous blocks on the secondary storage device

• Difficult to support dynamic file sizes

Head position 237…First block 785Number of blocks 25

File descriptor

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-22

Copyright © 2004 Pearson Education, Inc.

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

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-23

Copyright © 2004 Pearson Education, Inc.

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 24: Slide 13-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 13 13 File Management

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-24

Copyright © 2004 Pearson Education, Inc.

DOS FAT Files

DiskBlock

File Descriptor

DiskBlock

DiskBlock

…43

107254

File Access Table (FAT)

DiskBlock

DiskBlock

DiskBlock

…43

107

10743

254

254

File Descriptor

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-25

Copyright © 2004 Pearson Education, Inc.

UNIX FilesDatamode

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

inode

Data

Data

Index

Data

DataIndexIndex

IndexData

Data

Index

Index

IndexIndex

Index

Data

Data

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-26

Copyright © 2004 Pearson Education, Inc.

Directories

• A set of logically associated files and sub directories

• File manager provides set of controls:– enumerate– copy– rename– delete– traverse– etc.

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

Operating Systems: A Modern Perspective, Chapter 13

Slide 13-27

Copyright © 2004 Pearson Education, Inc.

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.