操作系统--文件系统详解
目标
解释文件系统的功能
描述文件系统的接口
讨论文件系统设计权衡,包括
访问方法,
文件共享,
文件锁定,以及
目录结构
探索文件系统保护
文件概念
文件概念
连续的逻辑地址空间
类型:
数据
数字
字符
二进制
计划
文件结构
无单词、字节序列
简单的记录结构
行
固定长度
可变长度
l Variable length
可变长度
n Complex Structures
复杂的结构
l Formatted document
格式化文件
l Relocatable load file
可重定位装载文件
n Can simulate last two methods with first method by
可以用第一种方法模拟最后两种方法
inserting appropriate control characters (CR, LF)
插入适当的控制字符
n Who decides:
谁决定:
l Operating system
操作系统
l Program
计划
File Attributes
文件属性
n Name only information kept in humanreadable form
以可读形式保存的姓名信息
n Identifier unique tag (number) identifies file within file
标识符唯一标签(编号)标识文件中的文件
system
系统
n Type needed for systems that support different types
支持不同类型的系统所需的类型
n Location pointer to file location on device
指向设备上文件位置的位置指针
n Size current file size
大小当前文件大小
n Protection controls who can do reading, writing,
保护控制谁可以阅读、书写,
executing
执行
n Time, date, and user identification data for protection,
用于保护的时间、日期和用户身份数据,
security, and usage monitoring
安全性和使用监控
n Information about files are kept in the directory structure, which is maintained on the disk
文件信息保存在目录结构中,该结构保存在磁盘上
File Operations
文件操作
n File is an abstract data type
文件是一种抽象数据类型
n Create
创建
n Write
写作
n Read
阅读
n Reposition within file
在文件中重新定位
n Delete
删除
n Truncate
截断
n Open(Fi ) search the directory structure on disk for entry Fi , and move the content of entry to memory
打开(Fi)在磁盘上的目录结构中搜索条目Fi,并将条目内容移动到内存中
n Close (Fi ) move the content of entry Fi in memory to
关闭(Fi)将内存中条目Fi的内容移动到
directory structure on disk
磁盘上的目录结构
Open Files
打开文件
n Several pieces of data are needed to manage
需要管理多项数据
open files:
打开文件:
l File pointer: pointer to last read/write location, per process that has the file open
文件指针:文件打开的每个进程的最后读/写位置的指针
l Fileopen count: counter of number of times a file is
文件打开次数:文件打开的次数
open to allow removal of data from openfile table
打开以允许从openfile表中删除数据
when last process closes it
当最后一个进程关闭时
l Disk location of the file: cache of data access
文件的磁盘位置:数据访问缓存
information
信息
l Access rights: perprocess access mode information
访问权限:进程访问模式信息
Open File Locking
打开文件锁定
n Provided by some operating systems and file systems
由一些操作系统和文件系统提供
n Shared Lock: several processes can acquire the lock
共享锁:多个进程可以获得该锁
concurrently (like a reader lock)
并发(像一个阅读器锁)
n Exclusive Lock: Only one process at a time can acquire such
排他锁:一次只能有一个进程获得该锁
a lock (like a writer lock)
锁(像写字锁)
n Mandatory or advisory file locking mechanisms:
强制性或咨询性文件锁定机制:
l Mandatory Once a process acquires an exclusive lock, the OS will prevent any other process from accessing the locked file.(Windows)
强制一旦进程获得排他锁,操作系统将阻止任何其他进程访问锁定的文件。(Windows)
l Advisory The OS will not prevent a process from
咨询操作系统不会阻止流程
acquiring access to a locked file.Rather, the process
获取对锁定文件的访问权限。相反,这个过程
must be written so that it manually acquiring the lock
必须被写入以便它手动获取锁
before accessing the file.(UNIX)
在访问文件之前。(UNIX)
File Locking Example Java API
文件锁定示例
import java.io.*;
导入Java . io . *;
import java.nio.channels.*;
导入Java . nio . channels . *;
public class LockingExample {
公共类锁定示例{
public static final boolean EXCLUSIVE = false;
public static final boolean EXCLUDE = false;
public static final boolean SHARED = true;
public static final boolean SHARED = true;
public static void main(String arsg[]) throws IOException { FileLock sharedLock = null;FileLock exclusiveLock = null;try {
public static void main(String arsg[])引发IOexception { FileLock shared Lock = null;FileLock exclusiveLock = null尝试{
RandomAccessFile raf = new RandomAccessFile(“file.txt”, “rw”);
RandomAccessFile RAF = new RandomAccessFile(" file . txt “,” rw ");
// get the channel for the file
//获取文件的通道
FileChannel ch = raf.getChannel();
file CHannel ch = RAF . GetCHannel();
// this locks the first half of the file exclusive
//这会独占锁定文件的前半部分
exclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE);
exclusiveLock = ch.lock(0,raf.length()/2,EXCLUSIVE
/** Now modify the data …*/
/**现在修改数据。。。*/
// release the lock
//松开锁
exclusiveLock.release();
exclusivelock . release();
10.10
10.10
File Locking Example Java API (cont)
文件锁定示例
// this locks the second half of the file shared sharedLock = ch.lock(raf.length()/2+1, raf.length(), SHARED);
//这样锁定了shared LOck = ch . lock(raf.length()/2+1,RAF . length(),SHARED)文件的后半部分;
/** Now read the data …*/
/**现在读数据。。。*/
// release the lock sharedLock.release();
//释放锁shared lock . release();
} catch (java.io.IOException ioe) { System.err.println(ioe);
} catch(Java . io . IOexception ioe){ System . err . println(ioe);
}finally {
}最后{
if (exclusiveLock != null)
if (exclusiveLock!= null)
exclusiveLock.release();
exclusivelock . release();
if (sharedLock != null)
if (sharedLock!= null)
sharedLock.release();
shared lock . release();
}
}
}
}
}
}
File Types Name, Extension
文件类型名称,扩展名
访问方法
n Sequential Access
顺序访问
read next
阅读下一页
write next
写下一个
reset
重新设定
n Direct Access
直接访问
read n
读取n
write n
写n
position to n
位置到n
read next
阅读下一页
write next
写下一个
rewrite n
重写n
n = relative block number
n =相对块数
10.13
10.13
Sequentialaccess File
顺序存取文件
Simulation of Sequential Access on Directaccess File
直接存取文件的顺序存取仿真
目录结构
n A collection of nodes containing information
包含信息的节点集合
about all files
关于所有文件
Both the directory structure and the files reside on disk
目录结构和文件都驻留在磁盘上
Backups of these two structures are kept on tapes
这两种结构的备份保存在磁带上
Disk Structure
磁盘结构
n Disk can be subdivided into partitions
磁盘可以细分为多个分区
n Disks or partitions can be RAID protected against failure
磁盘或分区可以进行磁盘阵列保护以防故障
n Disk or partition can be used raw without a file system,
没有文件系统,磁盘或分区可以直接使用,
or formatted with a file system
或者用文件系统格式化
n Partitions also known as minidisks, slices
分区也称为微型磁盘、片
n Entity containing file system known as a volume
包含称为卷的文件系统的实体
n Each volume containing file system also tracks that file
包含文件系统的每个卷也跟踪该文件
system’s info in device directory or volume table of
设备目录或卷表中的系统信息
contents
目录
n As well as generalpurpose file systems there are many
除通用文件系统外,还有许多
specialpurpose file systems, frequently all within the same operating system or computer (Solaris)
特殊目的文件系统,通常都在同一操作系统或计算机(Solaris)中
A Typical Filesystem Organization
典型的文件系统组织
Operations Performed on Directory
对目录执行的操作
n Search for a file
搜索文件
n Create a file
创建文件
n Delete a file
删除文件
n List a directory
列出目录
n Rename a file
重命名文件
n Traverse the file system
遍历文件系统
Organize the Directory (Logically) to Obtain
组织目录(逻辑上)以获得
n Efficiency locating a file quickly
快速查找文件的效率
n Naming convenient to users
方便用户的命名
l Two users can have same name for different files
两个用户可以对不同的文件使用相同的名称
l The same file can have several different names
同一文件可以有几个不同的名称
n Grouping logical grouping of files by properties, (e.g., all Java programs, all games, …)
分组按属性对文件进行逻辑分组(例如,所有Java程序、所有游戏……)
SingleLevel Directory
单层目录
n A single directory for all users
面向所有用户的单一目录
l Naming problem
命名问题
l Grouping problem
分组问题
10.22
10.22
TwoLevel Directory
两级目录
n Cn have the same file name for different user
不同用户可以使用相同的文件名
n Efficient searching
高效搜索
n No grouping capability
没有分组能力
10.23
10.23
TreeStructured Directories
树形结构目录
TreeStructured Directories (Cont)
树形结构目录(续)
n Efficient searching
高效搜索
n Grouping Capability
分组能力
n Current directory (working directory)
当前目录(工作目录)
lcd /spell/mail/prog
CD/拼写/邮件/程序
ltype list
类型列表
TreeStructured Directories (Cont)
树形结构目录(续)
n Absolute or relative path name
绝对或相对路径名
n Creating a new file is done in current directory
在当前目录下创建新文件
n Delete a file
删除文件
rm
rm <文件名>
n Creating a new subdirectory is done in current directory
在当前目录下创建新的子目录
mkdir
mkdir
Example: if in current directory
示例:如果在当前目录中
mkdir count
mkdir计数
Deleting “mail” Þ deleting the entire subtree rooted by “mail”
删除“邮件”Þ删除以“邮件”为根的整个子树
AcyclicGraph Directories
无环图目录
n Have shared subdirectories and files, for joined project,
有共享的子目录和文件,对于加入的项目,
for example
例如
AcyclicGraph Directories (Cont.)
无环图目录(续。)
n Allows directories to share subdirectories and files.The
允许目录共享子目录和文件。这
same file or subdirectory may be in two different
相同的文件或子目录可能位于两个不同的
directories
名录
n Shared files and subdirectories can be implemented in
共享文件和子目录可以在
several ways.
几种方式。
n Create a new directory entry Link
创建新的目录条目链接
l Link a pointer to another file or subdirectory.A link may be implemented as an absolute or a relative path
将指针链接到另一个文件或子目录。链接可以实现为绝对路径或相对路径
name.
名字。
l Resolve the link using that path name to locate the real file.Links are easily identified by their format in the directory entry and are effectively indirect
使用该路径名解析链接,找到真实文件。链接很容易通过它们在目录条目中的格式来识别,并且实际上是间接的
pointers.
指针。
General Graph Directory
通用图形目录
n A serious problem with using acyclicgraph structure is ensuring that there is no cycles.
使用无环图结构的一个严重问题是确保没有循环。
n However, when we add links, the tree structure is destroyed,
然而,当我们添加链接时,树结构被破坏了,
resulting in a simple graph structure.
导致简单的图形结构。
General Graph Directory (Cont.)
通用图形目录(续。)
n If cycles are allowed to exist in the directory
如果目录中允许存在循环
l An infinite loop continually searching through
不断搜索的无限循环
the cycle
周期
l When a file can be deleted ?
何时可以删除文件?
4A Garbage collection scheme is used to determine
4A垃圾收集方案用于确定
when the last reference has been deleted and the
当最后一个引用被删除并且
disk space can be reallocated.
可以重新分配磁盘空间。
n Every time a new link is added use a cycle
每次添加新链接时,使用一个循环
detection algorithm to determine whether it is OK
确定是否正常的检测算法
File System Mounting