ke shi hust 9lsometimes, the stored form has a life its own 4pictures of your friends, videos, songs...

103
Chapter 9 文件系统 Ke Shi HUST

Upload: others

Post on 24-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

Chapter 9 文件系统

Ke Shi HUST

Page 2: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.2Principles of Operating System Ke Shi , http://www.iothust.org/courses

内容

n 背景

n 文件及文件系统的基本概念

n 文件的逻辑组织与存储方法

n 文件的物理结构

n 空闲存储空间的管理

n 文件目录

n 共享与安全

n 文件操作

n 文件系统实现实例Ke Shi HUST

Page 3: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.3Principles of Operating System Ke Shi , http://www.iothust.org/courses

Storing Information

n We often need to store informationl Sometimes, the stored form has a life its own

4 Pictures of your friends, videos, songs

l Sometimes, the stored data is for recovery4 In case you want to restart the game later4 Or perhaps your application is a little fragile and crashes

now and then

l Files are a good way for processes to cooperate4 You do X, I’ll do Y and we’ll merge the results

Ke Shi HUST

Page 4: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.4Principles of Operating System Ke Shi , http://www.iothust.org/courses

Storing Information

n Applications can store it in the process address space

n Why is it a bad idea?l Size is limited to size of virtual address space

4 May not be sufficient for airline reservations, banking, etc.

l The data is lost when the application terminates4 Even when computer doesn’t crash!

l Multiple process might want to access the same data4 Imagine a telephone directory part of one process

Ke Shi HUST

Page 5: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.5Principles of Operating System Ke Shi , http://www.iothust.org/courses

File Systems

n Three criteria for long-term information storage:l Should be able to store very large amount of informationl Information must survive the processes using itl Should provide concurrent access to multiple processes

n Solution:l Store information on disks in units called filesl Files are persistent, and only owner can explicitly delete itl Files are managed by the OS

n File Systems: How the OS manages files!Ke Shi HUST

Page 6: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.6Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件 File

n Files abstract information stored on diskl You do not need to remember block, sector, …l We have human readable names

n 文件是一种抽象机制,它提供了一种把信息保存在存储介质上,而且便于以后存取的方法,用户不必关心实现细节

n How does it work?l Process creates a file, and gives it a name

4 Other processes can access the file by that name

n 文件定义

l 文件是具有符号名的信息(数据)项的集合。

l 文件是具有符号名的记录的集合。Ke Shi HUST

Page 7: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.7Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件命名 File Naming

n文件名

l每个文件必须有一个唯一的文件名

l系统管理、用户使用都使用文件名

l文件名是一个有限长度的字符串

l Naming conventions are OS dependent4 Usually names as long as 255 characters is allowed4 Digits and special characters are sometimes allowed4 MS-DOS and Windows are not case sensitive, UNIX

family isKe Shi HUST

Page 8: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.8Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件扩展名 File Extensions

n Name divided into 2 parts, second part is the extension

n On UNIX, extensions are not enforced by OSl However C compiler might insist on its extensions

4 These extensions are very useful for C

n Windows attaches meaning to extensionsl Tries to associate applications to file extensionsl You can see and even change these if you like

Ke Shi HUST

Page 9: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.9Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件类型 File Typen 5 types of files

l Regular files 普通文件 : contain user informationl Directories 目录文件 : system files for maintaining structure

of FSl Character special files 特别文件(字符设备): for serial I/O

in UNIXl Block special files 特别文件(块设备): to model disks in

UNIXl Symbolic links 符号连接

n Regular files are usually:l ASCII files: lines of text

4 Useful for editing, portability across applications

l Binary files: usually have an internal structure4 Look at executables and archives in UNIX4 Every OS needs a way to recognize its own executable!

Ke Shi HUST

Page 10: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.10Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件属性 File Attributes

n File-specific info maintained by the OSl File size, modification date, creation time, etc.l Varies a lot across different OSes

n Some examples:l Name – only information kept in human-readable forml Identifier – unique tag (number) identifies file within file systeml Type – needed for systems that support different typesl Location – pointer to file location on devicel Size – current file sizel Protection – controls who can do reading, writing, executingl Time, date, and user identification – data for protection,

security, and usage monitoringKe Shi HUST

Page 11: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.11Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统

n 文件系统是操作系统中负责管理和存取文件信息的

软件机构,它是由管理文件所需的数据结构和相应

的管理软件以及访问文件的一组操作组成。

n 从系统的角度看

文件系统是负责文件存储空间管理的机构。

n 从用户的角度看

文件系统是用户在计算机上存储信息、使用信息的

接口,实现“按名存取”Ke Shi HUST

Page 12: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.12Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统

n 通用功能

l 提供用户对文件操作的命令;

l 提供用户共享文件的机制;

l 管理文件的存储介质;

l 提供文件的存取控制的机制,保障文件及文件系统的安全性;

l 提供文件及文件系统的备份和恢复功能;

l 提供对文件的加密和解密功能。

Ke Shi HUST

Page 13: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.13Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件结构 File Structure

n 文件结构研究的两种观点:

l 用户观点:

4研究 “思维”中的文件。称为逻辑文件。

4研究的侧重点在于为用户提供一种逻辑结构清晰、使用简便的逻

辑文件形式。用户将按照这种形式去存储、检索文件。

l 实现观点:

4研究存储“介质”中的实际文件,称实际文件。

4研究的侧重点是选择一些工作性能良好、介质利用率高的

文件物理结构。系统将按照这种结构在存储介质上存放文

件信息。Ke Shi HUST

Page 14: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.14Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件结构 File Structure

n 文件的逻辑结构

l 文件的逻辑结构是指用户思维中文件的结构。

n 文件的物理结构

l 文件的物理结构是指文件在存储介质上的结构。

l 文件存储介质:磁盘(软盘、硬盘)、光盘、磁带

Ke Shi HUST

Page 15: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.15Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的逻辑结构

n 流式文件(无结构) Byte Sequence: unstructuredl 流式文件是相关的字符的集合,文件的长度为所含字符数

l Unix、Linux、Windows系统中的普通文件均属流式文件

n 记录式文件(Record structure)l Lines,Fixed length,Variable length

l 记录式文件是记录的集合,每个记录由相关的域构成。

Ke Shi HUST

Page 16: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.16Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存取方法 File Access

n 顺序存取 Sequential access:

l 后一次存取操作总是在前次的基础上进行的。每次操作不

必给出操作开始的位置。

l read all bytes/records from the beginningl cannot jump around, could rewind or forwardl convenient when medium was magnetic tape

Ke Shi HUST

Page 17: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.17Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存取方法 File Access

n 随机存取 Random access:

l 每次存取操作都要指定操作的开始位置。

l bytes/records read in any orderl essential for database systemsl 2 possible reads

4 Specify disk block in read4 move file marker (seek), then read or

Ke Shi HUST

Page 18: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.18Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存取实例

n read(fd, buffer, size)

n write(fd, buffer, size)

n lseek (fd, offset, mode)

position = lseek (fd, 500, 2);

n=read(fd,buffer,500);

例:读文件的最后500字节。fd

wrp

fd

wrp500

Ke Shi HUST

Page 19: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.19Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 文件在存储介质上的实际组织形式

n 与存储介质相关(磁盘,Disk)l 磁盘格式化后就分成许多大小相等的单位-磁盘块(物理块)

l 基本单位为字节(byte),具体1块的大小可能为512byte,4Kbyte等等

l 每个磁盘块有个编号,称磁盘块号(物理块号)Ke

Shi HUST

Page 20: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.20Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 连续文件

l 文件的内容存放在连续编号的磁盘块中

l 实例中磁盘块大小为512byte98 99 100 101 102 103 104 105 106 107 108 109

File 1 ••• 2000 100File 2 ••• 1200 105

文件目录

••• Ke Shi HUST

Page 21: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.21Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n Contiguous Allocation: allocate files contiguously on disk

Ke Shi HUST

Page 22: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.22Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 连续文件

n Pros:l 结构简单,实现容易,不需要额外的开销

l Simple: state required per file is start block and sizel Performance: entire file can be read with one seek

n Cons:l Fragmentation: external is bigger problem,空间利率低l Usability: user needs to know size of file,用户创建文件时要给出文件的大小,不利于文件的动态增加和修改

n Used in CDROMs, DVDsKe Shi HUST

Page 23: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.23Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 串联文件

l 文件的内容存放在若干不要求连续编号的磁盘块中

l 一个文件占用的磁盘块链接成一个磁盘块链,链接指针存放在每磁盘块的最末一个字(或第一个字)。

Ke Shi HUST

Page 24: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.24Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 串联文件

文件目录

•••

•••••••••

109

76

76

111

111

Λ

file1 109

78

200

200

756

756

12

12

Λ

user1 78

1203

200

1203Ke Shi HUST

Page 25: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.25Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 串联文件

n Pros:l No space lost to external fragmentation,存储空间利用率高

l 文件创建时用户不必指出文件的大小,文件动态扩充和修改容易

l Disk only needs to maintain first block of each filen Cons:

l Random access is costly,只适合顺序存取,随机存取效率太低

l Overheads of pointers.Ke Shi HUST

Page 26: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.26Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 文件映照

l 即把串联文件中的链接字集中在一结构中,这样既保持了串联文件的优点,也克服了其缺点。

l FAT文件系统采用了文件映照技术4 Using an in-memory table called File Allocation Table

(FAT) to Implement a linked list4 Take pointer away from blocks, store in this table

FAT文件磁盘格式boot

FAT1 根目录区 文件数据区FAT2Ke Shi HUST

Page 27: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.27Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n FAT

File 1 ••• 3 4File 2 ••• 2 2

文件目录

••• 0000

•••

012345

n-6 n-5n-4n-3 n-2n-1

n

FAT

000000000000

0000000000000000000000000000

0000005n-6

FFFF

n- 3

FFFFKe Shi HUST

Page 28: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.28Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n Pros:l Entire block is available for datal Random access is faster since entire FAT is in memory

n Cons:l Entire FAT should be in memory

4 For 20 GB disk, 1 KB block size, FAT has 20 million entries4 If 4 bytes used per entry ⇒ 80 MB of main memory required for FS

Ke Shi HUST

Page 29: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.29Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n随机文件

l 直接地址结构

l 计算寻址结构

Ke Shi HUST

Page 30: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.30Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构n 索引文件

l 当代计算机操作系统中普遍采用的结构,如Unix系统、Linux系统。

File 1 ••• 2048File 2 ••• 1000

文件目录

•••

124

56

7981

226

逻辑块号磁盘块号01

32

56124

7981226

逻辑块号磁盘块号

01

76

176

17676Ke Shi HUST

Page 31: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.31Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件的物理结构

n 索引文件

l 每个文件有一个索引表,登记文件的逻辑块与物理块间的

对应关系。

l 索引表位置:

4文件目录

4文件中

4……

l 索引表大小

4固定大小

4非固定大小

Ke Shi HUST

Page 32: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.32Principles of Operating System Ke Shi , http://www.iothust.org/courses

索引文件结构

n 索引文件既适应于顺序存访问,也适应于随机访问

n 索引表:空间开销

n 文件索引:时间开销

Ke Shi HUST

Page 33: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.33Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存储空间的管理 Disk Space Managementn Files stored as fixed-size blocksn What is a good block size? (sector, track, cylinder?)

l If 131,072 bytes/track, rotation time 8.33 ms, seek time 10 ms, reading time 4.165ms

l To read k bytes block: 10+ 4.165 + (k/131072)*8.33 msl Median file size: 2 KB

33

Block size

Ke Shi HUST

Page 34: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.34Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存储空间的管理

n 空闲空间的管理 Managing Free Disk Spacel 空闲文件目录--连续空闲空间等同于文件

l 空闲块链 Linked list

l 位示图 Bit map

Ke Shi HUST

Page 35: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.35Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存储空间的管理

n 位示图 Bit map

n Easy to get contiguous filesn Bit map requires extra space

l Example:block size = 212 bytesdisk size = 230 bytes (1 gigabyte)n = 230/212 = 218 bits (or 32K bytes)

n Bit vector (n blocks)

…0 1 2 n-1

bit[i] =

67

8 1 ⇒ block[i] free

0 ⇒ block[i] occupied

Ke Shi HUST

Page 36: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.36Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件存储空间的管理

n 空闲块链 Linked listl Cannot get

contiguous space easily

l No waste of spacel Only one block need

to be kept in memoryl 成组连接(Grouping)

Ke Shi HUST

Page 37: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.37Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件目录 File Directoryn 文件目录即文件名址录,它是一张记录所有文件名及其存放地址、

文件的说明和控制信息的表格,每个文件占用一个表目,即每个文

件有一个文件的目录项。

n Directories/folders keep track of filesl Is a symbol table that translates file names to directory entriesl Usually are themselves files

n How to structure the directory to optimize all of the foll.:l Search a filel Create a filel Delete a filel List directoryl Rename a filel Traversing the FS

37

F 1 F 2 F 3F 4

F n

Directory

FilesKe Shi HUST

Page 38: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.38Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件目录 File Directory

n 文件目录项

l 1.文件名

l 2.文件的大小,单位:字节

l 3.文件在物理存储介质中的位置

4具体结构与文件的物理结构相关

l 4.存取控制信息

4文件主和其它用户对该文件的访问权限。

l 5.管理信息

4包含文件创建的日期和时间,最近修改该文件的日期和时间等。

l 6.文件的类型Ke Shi HUST

Page 39: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.39Principles of Operating System Ke Shi , http://www.iothust.org/courses

一级文件目录 Single-level Directory

n One directory for all files in the volumel Called root directory

l Used in early PCs, even the first supercomputer CDC 6600n Pros: simplicity, ability to quickly locate filesn Cons: inconvenient naming (uniqueness, remembering all) 可能重名

39

Ke Shi HUST

Page 40: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.40Principles of Operating System Ke Shi , http://www.iothust.org/courses

二级文件目录 Two-level directoryn Each user has a separate directory

n Solves name collision, but what if user has lots of filesn Files need to be addressed by path names

l Allow user’s access to other user’s filesl Need for a search path (for example, locating system files)

40

Ke Shi HUST

Page 41: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.41Principles of Operating System Ke Shi , http://www.iothust.org/courses

多级(树型)文件目录 Tree-structured Directoryn Directory is now a tree of arbitrary height

l Directory contains files and subdirectoriesl A bit in directory entry differentiates files from subdirectories

41

Ke Shi HUST

Page 42: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.42Principles of Operating System Ke Shi , http://www.iothust.org/courses

路径名 Path Names

n To access a file, the user should either:l Go to the directory where file resides, orl Specify the path where the file is

n Path names are either absolute or relativel Absolute 绝对路径名: path of file from the root directoryl Relative 相对路径名: path from the current working directory

n Most OSes have two special entries in each directory:l “.” for current directory and “..” for parent

42

Ke Shi HUST

Page 43: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.43Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件目录结构

Ke Shi HUST

Page 44: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.44Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件共享 File Sharing

n 定义:一个文件被多个用户或程序使用

n 共享形式:

l 被多个用户使用,由存取权限控制

l 被多个进程使用,但各用自己的读写指针

l 被多个进程使用,但共享读写指针

n 目的

l 节省时间和存储空间,减少了用户工作量

l 进程间通过文件交换信息

Ke Shi HUST

Page 45: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.45Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件共享 File Sharing

Ø 由系统目录实现对文件的共享,用户通过全路径名共享地访问这些文件

Ø 对要共享的文件进行连接,通过“连接(Link)”命令,在用户自己的目录项中对要共享的文件建立起相应的表目,即建立两个文件的等价关系

Ke Shi HUST

Page 46: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.46Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统的安全性 File Protection

n File owner/creator should be able to control:l what can be donel by whom

n Types of accessl Readl Writel Executel Appendl Deletel List Ke

Shi HUST

Page 47: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.47Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统的安全性 File Protection

n 访问控制矩阵(Access Control Matrix/List)l This is a per-file list that tells who can access that filel Pro: Highly expressivel Con: Harder to represent in a compact way

n 存取控制表

n 用户权限表

n 口令

n 密码

Ke Shi HUST

Page 48: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.48Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统的安全性 File Protection

n Categories of Usersl Individual user

4 Log in establishes a user-id4 Might be just local on the computer or could be through

interaction with a network service

l Groups to which the user belongs4 For example, “einar” is in “facres”4 Again could just be automatic or could involve talking to a

service that might assign, say, a temporary cryptographic key

Ke Shi HUST

Page 49: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.49Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统的安全性 File Protection

n Linux Access Rightsl Mode of access: read, write, executel Three classes of users RWX

a) owner access 7 ⇒ 1 1 1RWX

b) group access 6 ⇒ 1 1 0RWX

c) public access 1 ⇒ 0 0 1

l For a particular file (say game) or subdirectory, define an appropriate access.

owner group public

chmod 761 game

Ke Shi HUST

Page 50: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.50Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件系统的安全性 File Protection

Ke Shi HUST

Page 51: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.51Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件操作 File Operation

n File is an Abstract Data Typen Some operations:

l Create a file: find space in FS, add directory entryl Open: system fetches attributes and disk addresses in

memoryl Write a file: locate file and write at current position

4 Might need to increase the size attribute

l Read a file: locate file, read from current position, store in buffer4 Read/write pointer can be stored as per-process file pointer

n 涉及主存和外存之间的数据交换Ke Shi HUST

Page 52: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.52Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件操作 File Operation

n 文件控制块 File Control Blockl FCB has all the information about the filel Unix/Linux systems call these i-node structuresl 文件打开前在外存(目录),打开后在主存(FCB)。

l 文件目录内容+传输控制信息

Ke Shi HUST

Page 53: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.53Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件操作 File Operationn 打开文件表,打开文件的FCB

n 使用文件的第一步,任何一个文件使用前都要先打开,即把FCB送到内存

fd=open(文件路径名,打开方式)

l 根据文件路径名查目录,找到FCB主部;

l 根据打开方式、共享说明和用户身份检查访问合法性;

l 根据文件号查系统活动文件表,看文件是否已被打开,是→共享计数加1,否则→将外存中的FCB主部等信息填入活动文件表空表项,共享计数置为1;

l 返回信息:fd-文件描述符,是一个非负整数,用于以后读写文

件。

外存 目录

主存 打开文件表

Ke Shi HUST

Page 54: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.54Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统

n 三种文件类型

l 普通文件

l 目录文件

l 特别文件

n 树型目录结构

n 目录文件

l 目录文件是由文件目录项组成的文件。

l 文件目录项由文件名和文件的磁盘索引节点号组成(I节点)。

文件名 磁盘索引结点号Ke Shi HUST

Page 55: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.55Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统

n 支持安装拆卸(mount/unmount)n Mount allows two FSes to be merged into one

l For example you insert your floppy/USB disk into the root FS

mount(“/dev/fd0”, “/mnt”, 0)

Ke Shi HUST

Page 56: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.56Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统的目录和组织

n Linux ext2目录结构

struct ext2_dir_entry_2 {_u32 inode; /* Inode number */_u16 rec_len; /* directory entry length */_u8 name_len; /* name length */_u8 file_type; /* file type */char name[EXT2_NAME-LEN]; /* File Name */

};

Ke Shi HUST

Page 57: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.57Principles of Operating System Ke Shi , http://www.iothust.org/courses

文件索引、文件目录间关系

Ke Shi HUST

Page 58: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.58Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件存储空间管理

UNIX系统中每个子文件系统(一片

软盘、一个硬盘的逻辑分区,一卷磁

带)格式化后的结构

0 1 2 k k+1

i节点区 文件数据区

n

对换区

引导区

特别块 特别块:存放子文件系统的管理信息的数据结构。

Ke Shi HUST

Page 59: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.59Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件存储空间管理n 特别块

l 在磁盘格式化时建立,当一个子系统安装到系统中时,系统将申请一个内存缓冲区来存放特别块。

struct filsys {

int s_isize; /* I节点区的大小,单位是块 */

int s_fsize; /* 文件区磁盘总块数 */

int s_nfree; /* 空闲块索引表中空闲块数 */

int s_free[100]; /* 空闲块索引表 */

int s_ninode; /* 空闲I节点数 */

int s_inode[100]; /* 空闲I节点索引表 */

char s_flock; /* 空闲块索引表封锁标志 */

char s_ilock; /* 空闲I节点表封锁标志 */

char s_fmod; /* 修改标志 */

char s_ronly; /* 只读标志 */

int s_time[2]; /* 最近一次更新的时间 */

int pad[50]; /* 空闲 */

}

Ke Shi HUST

Page 60: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.60Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件存储空间管理

n 空闲I节点和空闲块的管理均使用成组连接法n 空闲I节点

l s-nfree 空闲块数l s_free[100] 空闲块块号l s_flock 锁位

n 空闲块

l s-ninode 空闲块数l s_inode[100] 空闲块块号l s_ilock 锁位

Ke Shi HUST

Page 61: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.61Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件存储空间管理

Ke Shi HUST

Page 62: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.62Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件存储空间管理alloc() {

s_nfree --;if(s_nfree = = 0) {

if(s_free[0] = “∧”)sleep(pri,s_flock);

a = s_free[0];将s_free[0]块读到filsys;s_nfree=100;return(a);

} elsereturn(s_free[s_nfree]);

}

free(){

if(s_nfree < 100){ s_free[s_nfree] = 释放磁盘块号;

s_nfree ++;} else

{ 将s_free[]写到释放磁盘块中;s_nfree = 1;s_free[0] = 释放磁盘块号;

}return();

}Ke Shi HUST

Page 63: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.63Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX系统的索引文件结构

n 小型文件采用直接索引

n 大型文件采用间接索引

n 既保证绝大多数的文件有高的存取效率,又能适应

存取一些大型文件。

(既保证了系统高效率,又有很宽的适应面)。

Ke Shi HUST

Page 64: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.64Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX系统的索引文件结构

Ke Shi HUST

Page 65: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.65Principles of Operating System Ke Shi , http://www.iothust.org/courses

124 56 7981 226

null

null

nullnull

null

null

null

null

null

124

56

7981

226

File 1 1235文件目录

•••0

1

2

3

4

5

6

7

8

9

10

11

12

•••

File1 i 结点 1235

文件大小

1890

直接索引

假设:

磁盘块大小:512字节

磁盘块号:4字节

Ke Shi HUST

Page 66: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.66Principles of Operating System Ke Shi , http://www.iothust.org/courses

0

1

2

3

4

5

6

7

8

9

10

11

12

•••

File2 i 结点 896

文件大小

6250

File 2 896File 1 1235文件目录

•••134156

798

426

566

164

59

791

826

296

891 832 596

一次索引块

999

999

891832596null

nullnull

•••

null

null

直接索引

一次间接索引

101112

Ke Shi HUST

Page 67: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.67Principles of Operating System Ke Shi , http://www.iothust.org/courses

File 1 1235

文件目录

File 3 18960

1

2

3

4

5

6

7

8

9

10

11

12

•••

File3 i 结点 1896

文件大小

137200

138

136

536

♯♯ ♯

♯♯ ♯♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

678

788

二次索引块

788

null

直接索引

一次间接索引

1678978null

•••

二次间接索引

Ke Shi HUST

Page 68: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.68Principles of Operating System Ke Shi , http://www.iothust.org/courses

0

1

2

3

4

5

6

7

8

9

10

11

12

•••

File4 i 结点796

文件大小

8460288 File 1 1235

文件目录

File 4 796877

421

523

♯♯ ♯

♯♯ ♯♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

4323

8460288÷ 512 =16524

Ke Shi HUST

Page 69: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.69Principles of Operating System Ke Shi , http://www.iothust.org/courses

0

1

2

3

4

5

6

7

8

9

10

11

12

•••

File4 i 结点796

文件大小

8460288

877

421

523

♯♯ ♯

♯♯ ♯♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

4323

1788 Ke Shi HUST

Page 70: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.70Principles of Operating System Ke Shi , http://www.iothust.org/courses

0

1

2

3

4

5

6

7

8

9

10

11

12

•••

File4 i 结点796

文件大小

8460288 File 1 1235

文件目录

File 4 796877

421

523

♯♯ ♯

♯♯ ♯♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

♯♯ ♯

4323

17883324

Ke Shi HUST

Page 71: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.71Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统的打开文件管理机构

n 用户在使用文件之前必须要打开文件,为什么?

n 文件是存放在磁盘上的,CPU不能直接访问。这样,在使用文

件之前,就要在内存中建立存取文件的结构,文件控制块(

FCB)。然后,通过对FCB的控制达到对文件的访问。

Ke Shi HUST

Page 72: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.72Principles of Operating System Ke Shi , http://www.iothust.org/courses

n 内存中建立FCBn FCB:

l 文件目录的信息

l 文件存取操作的信息

l 文件及目录所在的位置信息

FCB

内存

n = open()

{ •••;}

标志

文件的位置

WRP012

n-1

用户打开文件表

文件目录项

文件目录

Unix的FCB??

文件逻辑结构

0

Ke Shi HUST

Page 73: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.73Principles of Operating System Ke Shi , http://www.iothust.org/courses

打开文件的结构

n i_flag打开文件的标志l ILOCK:锁位l IUPD:修改标志l IMOUNT:安装点l IWANT:有进程等待l ITEXT:纯正文段

n i_count:访问计数n i_dev:文件所在设备号n i_number:i节点号

活动(内存)I节点

Ke Shi HUST

Page 74: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.74Principles of Operating System Ke Shi , http://www.iothust.org/courses

打开文件的结构

n系统打开文件表file.hl f_flag:标志

4FREAD:读4FWRITE:写4FPIPE:管道

l f_count:访问计数l f_inode:指向活动 I节点指针

l f_offset:读写指针Ke Shi HUST

Page 75: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.75Principles of Operating System Ke Shi , http://www.iothust.org/courses

打开文件的结构

n 进程打开文件表

l 每个进程有一个进程打开文件表,存放user结构中,u.u_ofile[NOFILE]。

l NOFILE=15,表示一个进程最多能同时打开15个文件,这个参数是可配置的。

l 在Windows系统中叫用户文件描述符表。表的大小在config.sys中配置。(files=30)

Ke Shi HUST

Page 76: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.76Principles of Operating System Ke Shi , http://www.iothust.org/courses

打开文件结构之间的关系

Ke Shi HUST

Page 77: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.77Principles of Operating System Ke Shi , http://www.iothust.org/courses

•••••• ••••••

计数:1

操作:R

RWP:0

内存I结点

进程打开文件表

系统打开文件表

•••计数:1

操作:R

RWP:0

进程A0 1 2 3 4 5 13 14…

进程B0 1 2 3 4 5 13 14…

进程C0 1 2 3 4 5 13 14…

计数: 1

I结点:001

……

/dev/con

计数:1

操作:W

RWP:0

计数: 1

I结点:002

……

/dev/disp

计数:2

操作:R

RWP:0

计数:2

操作:W

RWP:0

计数: 1

I结点:185

……

/user/usebb

计数: 2

I结点:185

……

计数:1

操作:W

RWP:0

计数:1

操作:R

RWP:0

计数:1

操作:R

RWP:0

计数: 1

I结点:001

……/home/loo

/user/loo

计数: 2

I结点:001

……

Ke Shi HUST

Page 78: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.78Principles of Operating System Ke Shi , http://www.iothust.org/courses

打开文件结构

n文件共享

父子进程共享同一文件,子进程在创建时继承了父进程的所有打开的文件,体现在打开文件表目的共享;

Ke Shi HUST

Page 79: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.79Principles of Operating System Ke Shi , http://www.iothust.org/courses

n 不同的用户打开了同一个文件(以不同的文件名)

l 进程C open(“/user/a.out”,READ) l 进程B open(“/hust/as”,WRITE)

Ke Shi HUST

Page 80: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.80Principles of Operating System Ke Shi , http://www.iothust.org/courses

n 3.不同的进程以不同的用途打开同一个文件l 进程A open(“/user1/li”,READ)l 进程C open(“/user1/li”,WRITE)

Ke Shi HUST

Page 81: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.81Principles of Operating System Ke Shi , http://www.iothust.org/courses

Overall Structure of File System

User Process

File System Cache (“buffer”) Inode cache

Disk DriverI/O

requests

Copy of superblock

User Process

File System Module (kernel)

Physical Storage Layer

User mode

Ke Shi HUST

Page 82: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.82Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX系统

Ke Shi HUST

Page 83: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.83Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

n 系统调用 openfd = open(pathname, flags);

– 功能:打开一个指定的文件。

– 输入参数:pathname要打开文件的路径名

flags 指示打开文件的类型(读或写)

– 返回参数:≧0 文件描述符(一个小的整型数)

= -1 表示系统调用失败。

Ke Shi HUST

Page 84: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.84Principles of Operating System Ke Shi , http://www.iothust.org/courses

namei(pathname)搜索要打开的文件并建内存I节点

存在吗?

Return(错误码)

N

分配一个file表表项;

设置访问计

数、读写指

Y

分配相应进程的u.u-ofile表项,并将file表项的首地址写入其中.

清文件?

Y清除原文件内容。N

内存I节点解锁;Return(fd);

fd=open(pathname,flags);

Ke Shi HUST

Page 85: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.85Principles of Operating System Ke Shi , http://www.iothust.org/courses

n open执行过程:l ①调用namei()将文件的路径名转换成I节点;

l ②文件不存在或非法访问,返回-1;

l ③申请打开文件表项,f_count++;f_offset置0;f_inode指向I节点;

l ④分配用户文件描述表项,将打开文件表首地址填入;

l ⑤若打开文件的类型是清文件,则释放该文件占用的所有磁盘块;

l ⑥返回用户描述表项的编号。

UNIX文件系统调用

Ke Shi HUST

Page 86: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.86Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

n 程序A执行了下列程序段:……;fd1=open(“/hust”,O_WRONLY);fd2=open(“/etc/passwd”,O_RDONLY);……;

n 程序B执行了以下的程序段:……;fd3=open(“/hust”,O_RDWR);……;

n 其中,O_RDONLY 仅读;O_WRONLY 写;O_RDWR 读写。Ke Shi HUST

Page 87: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.87Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

Ke Shi HUST

Page 88: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.88Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

n 系统调用 creat

fd=creat(pathname,modes);4功能:创建文件。

4输入参数: pathname要创建文件的路径名

modes 指示创建文件的类型(读或写)

4返回参数: fd文件描述符(与open()相同)

Ke Shi HUST

Page 89: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.89Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

n 执行过程:

l ①调用namei()分配文件路径名的I节点;l ②文件存在且不允许访问,则释放I节点,返回-1;l ③文件不存在,则申请分配一I节点;l ④在其父目录中建立目录项(文件名和I节点号);l ⑤分配进程打开文件表项,填入打开文件表首地址;

l ⑥若文件在创建时已存在,则释放该文件占用的所有磁盘块;

l ⑦返回进程打开文件表表目的编号。Ke Shi HUST

Page 90: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.90Principles of Operating System Ke Shi , http://www.iothust.org/courses Open(…)的功能

?文件存在

N

释放内存I节点

Return(错误码)

N

有访问权?

Y

namei(pathname)申请空闲I节点,并建立内存I节点

namei(pathname)搜索要打开的文件并建内存I节点

Y 建立新创建文件的目录项,写入其父目录文件

Ke Shi HUST

Page 91: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.91Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

n系统调用 close

close(fd);4功能:关闭文件;

4输入参数:fd文件描述符

4返回参数:无

Ke Shi HUST

Page 92: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.92Principles of Operating System Ke Shi , http://www.iothust.org/courses

UNIX文件系统调用

n 执行过程:

l ⑴if(f_count-- ==0) return;

l ⑵释放该表目;

l ⑶if(i_count--==0) return;

l ⑷ 如果活动I节点修改,则将有关盘上I节点的内存写回到相应盘上I节点;释放此I节点,return;

l ⑸如果活动I节点未修改,释放此I节点,return。

Ke Shi HUST

Page 93: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.93Principles of Operating System Ke Shi , http://www.iothust.org/courses

f_count= = 0?

根据fd找到file表项

f_count--

NReturn()

y

I_count - -

释放内存I节点释放file表目

I节点修改?

写磁盘I节点

y

N?

I_count= = 0

yN

Ke Shi HUST

Page 94: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.94Principles of Operating System Ke Shi , http://www.iothust.org/courses

程序A执行了下列程序段:……;fd1=open(“/hust”,O_WRONLY);fd2=open(“/etc/passwd”,O_RDONLY);……;

程序B执行了以下的程序段:……;fd3=open(“/hust”,O_RDWR);……;其中,O_RDONLY 仅读;O-WRONLY 写;O_RDWR 读写。

Ke Shi HUST

Page 95: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.95Principles of Operating System Ke Shi , http://www.iothust.org/courses

程序B执行了以下的程序段:

fd3=open(“/hust”,O_RDWR);……;

程序A执行了下列程序段:

fd1=open(“/hust”,O_WRONLY);fd2=open(“/etc/passwd”,O_RDONLY);

……;

close(fd1);

Ke Shi HUST

Page 96: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.96Principles of Operating System Ke Shi , http://www.iothust.org/courses

有一文件系统采用树型目录结构,其目录结构如下图所示,每个目录文件占用一个磁盘块

(1)设文件fd有64个逻辑记录(每个逻辑记录对应一个物理块),采用串联文件结构。若要打开文件fd并读文件中的第20个逻辑记录,问需要多少次磁盘I/O操作?为什么?

(2)在linux超级用户的环境下,可以使用命令mv /f1 /home/keyy/cvdf1/hust将文件“/f1”移到目录“/home/keyy/cvdf1”中并将文件名改为hust,简要说明该命令的实现步骤 Ke

Shi HUST

Page 97: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.97Principles of Operating System Ke Shi , http://www.iothust.org/courses

Ke Shi HUST

Page 98: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.98Principles of Operating System Ke Shi , http://www.iothust.org/courses

n 25次l 读根目录文件;读home目录文件;读keyy目录文件;读cvdf1目录文件;读fghj目录文件;读R0记录;读R1记录……读R19记录。

n 步骤:

l 1. 读根目录文件,获取文件f1的文件信息(即文件控制块);

l 2.依次读取home、keyy、cvdf1的目录文件;l 3. 在cvdf1目录文件中新建一条数据项信息(文件控制块),文件名设为hust;

l 4. 将f1对应的文件控制块信息拷贝到cvdf1新建的数据项中;

l 5.删除根目录文件中f1的数据项。

Ke Shi HUST

Page 99: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.99Principles of Operating System Ke Shi , http://www.iothust.org/courses

某文件系统物理结构采用多级索引结构,在每个文件的索引节点中有12个直接块指针,另外分别有一个一级、二级和三级间接索引指针,假设系统磁盘块大小为2KB,磁盘块指针用32位标识,读写磁盘以磁盘块为单位,一个索引块也分配一个磁盘块。文件A大小为1076000字节,问

(1)在该文件系统中,文件为多大需要使用一级、二级和三级间接索引?支持的最大文件长度是多少?

(2)画出文件A的物理结构。

(3)进程P打开了文件A,内存中除了索引节点没有其他信息,需要独处文件最后3000个字节的数据,需要读多少次磁盘块,给出说明。

Ke Shi HUST

Page 100: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.100Principles of Operating System Ke Shi , http://www.iothust.org/courses

• 文件长度大于12x2k=24k时,需要使用一级索引

• 文件长度大于12x2k+(2048/4x2k)=1048k时,需要使用二级索引。

• 文件长度大于12x2k+(2048/4x2k)+(2048/4x2048/4x2k)=525336k时,需要使用三级索引。

• 文件最大=12x2k+(2048/4x2k)+(2048/4x2048/4x2k)+(2048/4x2048/4x2048/4x2k) =268960792kKe

Shi HUST

Page 101: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.101Principles of Operating System Ke Shi , http://www.iothust.org/courses

Ke Shi HUST

Page 102: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

9.102Principles of Operating System Ke Shi , http://www.iothust.org/courses

• 1076000/2048 = 525 余 800• 3000=152+2048+800,含3块• 从0开始编号,523块,524块,525块

• 直接索引 12 0-11• 一级索引 512 12-523• 二级索引 512(0) 512(0、1) 524 525

• 需要读入一级索引表(1次磁盘块),读取152个字节(一次磁盘块)

• 需要读入二级索引表(1次磁盘块),再读入一级索引表(1次磁盘块),读取2048字节(1次磁盘块)和800字节(1次磁盘块),总共6次磁盘块。Ke

Shi HUST

Page 103: Ke Shi HUST 9lSometimes, the stored form has a life its own 4Pictures of your friends, videos, songs lSometimes, the stored data is for recovery 4In case you want to restart the game

End of Chapter 9

Ke Shi HUST