ustc-21000201-operating systems; fall 2014; instructor: chi zhang 1 welcome to —21000201—...

47
USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Upload: jerome-stafford

Post on 19-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1

Welcome to

—21000201—

Operating Systems

Part 4: File Management

Fall 2014

Page 2: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 2

Part 4: File Management

Overview of File Management User Interface: Files User Interface: Directories File System Implementation

Page 3: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Overview of File Management The need for long-term storage

it must be possible to store a very large amount of information• memory is too small to hold large databases of

records, for example airline reservations, bank accounts, etc.

the information must survive the termination of the processes using it • it must also not go away if the computer crashes

multiple processes must be able to access the information concurrently• for example, a phone directory should not be only

stored inside the address space of a single process

store information on disk, and group it in units called files

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 3

Page 4: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Overview of File Management

Chart of Operating System Responsibilities : The OS is responsible for providing a

uniform logical view of information storage the OS defines a logical unit of storage, the file,

and groups files in a hierarchy of directories the OS supports primitives for manipulating files

and directories (create, delete, rename, read, write, etc.)

the OS ensures data confidentiality and integrity the OS implements files on stable (nonvolatile)

storage media the OS keeps a mapping of the logical files onto

the physical secondary storageUSTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 4

Page 5: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Overview of File Management

The file system is the most visible aspect of an OSfiles are managed by the OShow files are

• structured• named• accessed• used• protected• implemented . . . are major topics in operating system design

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 5

Page 6: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Overview of File Management

Users’ standpoint vs. designers’ standpoint for the OS users

• the most important aspect is how files appear to them

• how files are named and protected• what operations are allowed, etc.

for the OS designers• must decide whether to implement files with

linked lists, tables, etc.• how to map file blocks to disk sectors• how to keep track of free storage, etc.

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 6

Page 7: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 7

Part 4: File Management

Overview of File Management User Interface: Files

File naming File structure File types File attributes File operations

User Interface: Directories File System Implementation

Page 8: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

User Interface: Files

Files are an abstraction mechanism the concept of “file” is the central element of

the file system a file is a complete collection of data (as text

or a program) treated by a computer as a unit especially for purposes of input and output

files provide a convenient way to store information on the disk and read it back later

they shield the user from the details of where the information is stored and how the disk works

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 8

Page 9: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Naming Naming is the most important aspect of

abstraction when a process creates a file, it gives it a

name; when it terminates, the file continues to exist

naming rules vary from system to system• allowed name length can go from 8 to 255

characters• UNIX systems distinguish between uppercase

and lowercase, MS-DOS and Windows do not• many systems support two-part, period-

separated naming: the second part is called the extension

• in UNIX, the extension is a user convention; not enforced

• Windows is extension-aware and associates files with specific applications

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 9

Page 10: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Naming Common file types & extensions

10

Page 11: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Logical Structure A file can be internally structured in several ways

pure byte sequence: OS doesn’t care about the contents; all meaning imposed by user application; generic OS (UNIX, Win)

record sequence: fixed or variable-length records with internal structure; historical 80-column punch card systems

tree: key-accessible records; mainframes commercial data processing

11

closer todatabasesystemtechniques

Three types of file structure 域:基本数据单元记录:一个数据元素文件:相关记录组成数据库

无结构字符流

有结构记录式文件

Page 12: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

文件组织和访问

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 12

Page 13: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

文件组织和访问

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 13

Page 14: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

索引表

为提供更有效的访问,还可以使用多级索引

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 14

键值 指针An Qi

Bao Rong

Chen Lin

索引表

逻辑文件

姓名 其它信息

An Qi

An Shang

An Zhang

Bao Rong

Page 15: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Types

An OS supports different types of files regular files

• the files that contain user information, ASCII or binary

directories (directory files)• system files that contain information about the

file system organization character special files

• used to model serial (character-mode) I/O devices: terminals, network

block special files• used to model parallel (block-mode) I/O devices:

disksUSTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 15

Windows

UNIX

Page 16: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Attributes

The OS associates management information with files in addition to its name and data, a file also has file

attributes the list of attributes varies considerably from system

to system, but typically:• file’s owner and protection• various bit flags: hidden, read/write, etc.• record length, key, etc. for record-structured files• timestamps: created, accessed, modified, etc.• size values

just as process control blocks (PCBs), the OS maintains file control blocks (FCBs) → see file system implementationUSTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 16

Page 17: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Attributes Some possible file attributes

17

Page 18: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Operations Most common system calls related to files

create/delete• creates a file with no data, initializes file attributes

open/close• loads file attributes and disk addresses in memory

read/write, append• transfers data from/to a buffer starting at a current

position seek

• in random access files: repositions file pointer for read/write

get/set attributes, rename• some attributes are user-settable (name, protection

flags)USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 18

Page 19: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Operations 文件的“打开”和“关闭”操作

当前 OS 所提供的大多数文件操作,其过程大致都是这样两步:• 检索文件目录来找到指定文件的属性及其在外存上的位置;• 文件实施相应的操作,如读文件或写文件等。

打开文件:是指系统将指明文件的属性(包括该文件在外存上的物理位置)从外存拷贝到内存打开文件表的一个表目中,并将该表目的编号(或称索引号)返回给用户。

作用:以后,当用户再要求对该文件进行相应操作时,便可利用该索引号向系统提出操作请求。这时系统便可直接利用该索引号到打开文件表中去查找,从而避免了对该文件的再次检索。这样不仅节省了大量的检索时间,也显著提高了对文件的操作速度。

关闭文件:如果用户已不再需要对该文件操作时,可利用“关闭” (Close) 系统调用来关闭此文件, OS 将会从打开文件表中把该文件对应的表目删除。

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 19

Page 20: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 20

Part 4: File Management

Overview of File Management User Interface: Files User Interface: Directories

Single-level structure Two-level structure Tree structure Directory operations

File System Implementation

Page 21: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

User Interface: Directories Directories are special files that keep track of

other files the collection of files is systematically organized first, disks are split into partitions that create logical

volumes (can be thought of as “virtual disks”) second, each partition contains information about the

files within this information is kept in entries in a device directory

(or volume table of contents) the directory is a symbol table that translates file

names into their entries in the directory• it has a logical structure• it has an implementation structure (linked list, table,

etc.)USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 21

Page 22: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure A collection of nodes containing information about all

files Both the directory structure and the files reside on

disk

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 22

F 1 F 2 F 3F 4

F n

Directory

Files

Page 23: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure

Single-level directory structure simplest form of logical organization: one global or

root directory containing all the files problems

• global namespace: unpractical in multiuser systems• no systematic organization, no groups or logical

categories of files that belong together

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 23

Page 24: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure

Two-level directory structure in multiuser systems, the next step is to give each

user their own private directory avoids filename confusion however, still no grouping: not satisfactory for

users with many files

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 24

Page 25: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure Tree-structured directory structure

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 25

Page 26: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure

Tree-structured directory structure natural extension of the two-level scheme provides a general hierarchy, in which files can be

grouped in natural ways good match with human cognitive organization:

propensity to categorize objects in embedded sets and subsets

navigation through the tree relies on pathnames• absolute pathnames start from the root, example:

/doursat/academic/teaching/cs446/assignment4/grades• relative pathnames start at from a current working

directory, example: assignment4/grades• the current and parent directory are referred to as . and

..USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 26

Page 27: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure

27

Page 28: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

目录操作 Common system calls related to directory

operations create/delete

• creates or deletes an empty directory (except for . and ..)

opendir/closedir• loads directory attributes in memory

readdir• reads the entries in a directory (more abstract than

read) rename

• renames a directory like a file link/unlink

• shares files by making them appear in more than one dirUSTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 28

Page 29: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Directory Structure

Acyclic-graph (shared file) directory structure allows for different users to work on the same files

while keeping their own view of the files (implemented with links)

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 29

Page 30: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 30

Part 4: File Management

Overview of File Management User Interface: Files User Interface: Directories File System Implementation

On-disk layout Record blocking File Block Allocation

Page 31: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File System Implementation

The file system implementation relies on several “on-disk” and “in-core” structures the on-disk structures contain persistent (static)

information:• how to boot an OS stored in the partition• number of blocks and free blocks• directory structure• individual files

the in-core (memory) structures are used for process-related file management and performance improvement via caching• tables of open files (system-wide and per-process)• recently opened directories

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 31

Page 32: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File System Implementation

The central elements are the File Control Blocks In UNIX, a File Control Block is called an i-node

(“index-node”) each file has a corresponding i-node structure, which

contains information describing the file on-disk i-node (file system dependent)

• persistent accounting information: user & group ownership, time stamps, etc.

• information to locate the disk blocks holding the file’s data

in-core i-node (file system independent)• transient management information: access flags

(locked, modified), processes holding it, read/write pointer, etc.

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 32

Page 33: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

On-disk layout 在磁盘上的文件布局 Layout of Disk Partitions

the disk can be divided up into several partitions that each hold an independent file system

block (sector) 0 of the disk contains the Master Boot Record (MBR 主引导记录 ), which is read in by the BIOS to boot the computer

then, the MBR locates the active partition in a table, loads and executes its “boot block” ( 引导块 ), in block 0

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 33

Page 34: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

On-disk layout Layout of file system inside a partition within one file system, the on-disk structures

include block 0, the “boot block” — information to boot the OS block 1, the “superblock” — partition details all the i-nodes all the file and directory data, split in blocks

34

OS dependentFile System

Page 35: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Elements of File Management

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 35

Page 36: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Record Blocking 记录组块 Fixed blocking 固定组块

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 36

Page 37: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Record Blocking Variable blocking: unspanned 可变长非跨越式组块

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 37

Page 38: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Record Blocking Variable blocking: spanned 可变长跨越式组块

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 38

Page 39: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

File Block Allocation

File block allocation methods Contiguous allocation Chained allocation Indexed allocation

方法比较

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 39

Page 40: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Contiguous Allocation each file is stored as a contiguous sequence of disk

blocks analogous to dynamic memory partitioning, except on

disk same advantages: simplicity + access speed (high

locality) but also same flaws: fragmentation + need to

declare size however, widely used in CD-ROMs! no

fragmentation in R-only

40

Page 41: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Chained Allocation each file is scattered in blocks: same idea as

memory paging! one way to keep track of the blocks is to link

them to each other

41

Page 42: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Chained Allocation

advantages: no fragmentation, file can change size by appending or removing blocks

main problem: access time! effective for sequential-access files, but not random-access

to find the i-th block, one must start at the beginning and follow all the pointers

other problem: slight waste of disk space, as a pointer of 4 bytes occupies ~1% of a block of 512 bytes

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 42

Page 43: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

显式链接 instead of scattering

block pointers, gather them in one global table: the file allocation table

each block entry points to the next block in the chain

end blocks get -1, free blocks get 0

used in MS-DOS and OS/2

problem: size and caching of table in memory 43

Page 44: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Indexed Allocation a global table is too big: so we are back to

distributing block pointers into blocks; but this time, we keep them together in one location per file: the index block

local table of scattered pieces; ex: 512b block holds 128 #’s

problem: what if a file is bigger then 128 blocks?

44

Page 45: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Indexed Allocation Indexed Allocation with Variable-Length Portions

45

Page 46: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

Multilevel Indexing keep the first 10 block pointers in the i-node structure then export the next 128 into a block accessed

through single indirection; and the next 16184 into a block of 128 blocks, etc.

46

Page 47: USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 1 Welcome to —21000201— Operating Systems Part 4: File Management Fall 2014

USTC-21000201-OPERATING SYSTEMS; FALL 2014; INSTRUCTOR: CHI ZHANG 47

After the class…

Reading: 教材第 12 章:文件管理 ( 12.1 节到 12.7 节) A. S. Tanenbaum: Modern Operating Systems (3rd

Edition), Chapter 4: File System ( Section 4.1~Section 4.3 )

Homework : P571: Problems 12.1, 12.2, 12.3, 12.9, 12.10 (7th

Edition)