Framist's Little House

◇ 自顶而下 - 面向未来 ◇

0%

【操作系统概念 - 作业 11】File-System Interface

Operating System Concepts Exercises 11

File-System Interface

操作系统作业 11

  • 11.2, 11.3, 11.5, 11.10, 11.11, 11.12

每题最后一个引用块是老师提供的参考答案

Practice Exercises

11.2, 11.3, 11.5, 11.10, 11.11, 11.12

11.2 Why do some systems keep track of the type of a file, while others leave it to the user and others simply do not implement multiple file types? Which system is “better?”

为什么有些系统会记录文件的类型,而其他系统则让用户自己来记录,还有一些系统干脆不实现多文件类型?哪个系统 “更好”?

答:

各有优劣,区分以适应不同用途。

Some systems allow different file operations based on the type of the file (for instance, an ascii file can be read as a stream while a database file can be read via an index to a block).

Other systems leave such interpretation of a file’s data to the process and provide no help in accessing the data.

The method that is “better” depends on the needs of the processes on the system, and the demands the users place on the operating system.

If a system runs mostly database applications, it may be more efficient for the operating system to implement a database-type file and provide operations, rather than making each program implement the same thing (possibly in different ways).

For general- purpose systems it may be better to only implement basic file types to keep the operating system size smaller and allow maximum freedom to the processes on the system.

一些系统允许根据文件的类型进行不同的文件操作(例如,一个 ascii 文件可以作为一个流来读,而一个数据库文件可以通过一个索引来读一个块)。
其他系统则把对文件数据的这种解释留给进程,并且不提供访问数据的帮助。
哪种方法 “更好”,取决于系统中进程的需求,以及用户对操作系统的要求。
如果一个系统主要运行数据库应用程序,操作系统实现数据库类型的文件并提供操作可能更有效,而不是让每个程序实现同样的东西(可能以不同的方式)。
对于通用系统来说,只实现基本的文件类型可能更好,这样可以使操作系统的规模更小,并允许系统中的进程获得最大的自由。

11.3 Similarly, some systems support many types of structures for a file’s data, while others simply support a stream of bytes. What are the advantages and disadvantages of each approach?

同样,有些系统支持许多类型的文件数据结构,而另一些系统只是支持字节流。每种方法的优势和劣势是什么?

答:

An advantage of having the system support different file structures is that the support comes from the system; individual applications are not required to provide the support. In addition, if the system provides the support for different file structures, it can implement the support presumably more efficiently than an application.

The disadvantage of having the system provide support for defined file types is that it increases the size of the system. In addition, applications that may require different file types other than what is provided by the system may not be able to run on such systems.

An alternative strategy is for the operating system to define no support for file structures and instead treat all files as a series of bytes. This is the approach taken by UNIX systems. The advantage of this approach is that it simplifies the operating system support for file systems, as the system no longer has to provide the structure for different file types. Furthermore, it allows applications to define file structures, thereby alleviating the situation where a system may not provide a file definition required for a specific application.

让系统支持不同的文件结构的一个好处是,这种支持来自于系统;不需要个别应用程序提供支持。此外,如果系统提供对不同文件结构的支持,它可以比应用程序更有效地实现支持。
让系统为定义的文件类型提供支持的缺点是,它增加了系统的大小。此外,可能需要不同的文件类型而不是系统提供的文件类型的应用程序可能无法在这种系统上运行。
另一种策略是操作系统不定义对文件结构的支持,而是将所有文件视为一系列的字节。这就是 UNIX 系统所采取的方法。这种方法的优点是,它简化了操作系统对文件系统的支持,因为系统不再需要为不同的文件类型提供结构。此外,它允许应用程序定义文件结构,从而缓解了系统可能不提供特定应用程序所需的文件定义的情况。

11.5 Explain the purpose of the open() and close() operations.

解释open()close()操作的目的。

答:

打开一个文件,关闭一个文件

Purpose of open( ) operation: The purpose of open( ) operation is open an active file. For this purpose the operating system maintain an open-file table, it contains information about the all open files. The open() operation can perform three task: fist take the file name, search the entire directory, and finally copy the directory entry into the open-file table. The open( ) operation can also validate the access modes. They are read only, write only, read/write, and so on. When the request mode is acceptable, then the file is opened for the process.

Purpose of close ( ) operation: The purpose of close() operation is when the process completes its operation on an open file then the close() operation performed. The close() operation decreases the open count, which could be associated with each file to indicate how many processes have the file open. When open count reaches the zero, the file is no longer in use, and the file entry is removed from the open-file table.

open( ) 操作的目的。open( ) 操作的目的是打开一个活动文件。为此,操作系统维护一个开放文件表,它包含所有开放文件的信息。open() 操作可以执行三个任务:取文件名,搜索整个目录,最后将目录条目复制到 open-file 表中。open() 操作还可以验证访问模式。它们是只读、只写、读/写,等等。当请求模式可以接受时,那么该文件就被打开,供进程使用。

close()操作的目的。close() 操作的目的是当进程完成了对一个开放文件的操作后,执行 close() 操作。close() 操作减少了开放计数,它可以与每个文件相关联,以表明有多少进程打开了该文件。当打开次数达到零时,该文件就不再被使用,该文件条目就会从打开文件表中删除。

11.10 The open-file table is used to maintain information about files that are currently open. Should the operating system maintain a separate table for each user or maintain just one table that contains references to files that are currently being accessed by all users? If the same file is being accessed by two different programs or users, should there be separate entries in the open-file table? Explain.

open-file 表是用来维护当前打开的文件信息的。操作系统应该为每个用户维护一个单独的表,还是只维护一个包含所有用户当前正在访问的文件的表?如果同一个文件被两个不同的程序或用户访问,在开放文件表中是否应该有单独的条目?解释一下。

答:

By keeping a central open-file table, the operating system can perform the following operation that would be infeasible otherwise. Consider a file that is currently being accessed by one or more processes. If the file is deleted, then it should not be removed from the disk until all processes accessing the file have closed it. This check can be performed only if there is centralized accounting of number of processes accessing the file.

On the other hand, if two processes are accessing the file, then two separate states need to be maintained to keep track of the current location of which parts of the file are being accessed by the two processes. This requires the operating system to maintain separate entries for the two processes.

通过保留一个中央开放文件表,操作系统可以执行以下操作,否则是不可行的。考虑到一个文件目前正被一个或多个进程访问。如果该文件被删除,那么在所有访问该文件的进程都关闭该文件之前,它不应该从磁盘上删除。只有在对访问该文件的进程数量进行集中核算时,才能进行这种检查。
另一方面,如果有两个进程在访问该文件,那么就需要维持两个独立的状态,以跟踪文件的哪些部分正在被两个进程访问的当前位置。这就要求操作系统为这两个进程维护不同的条目。

11.11 What are the advantages and disadvantages of providing mandatory locks instead of advisory locks whose use is left to users’ discretion?

提供强制锁,而不是由用户自行决定使用的建议锁,有什么优缺点?

答:

Advisory Locking is a cooperative locking scheme where the participating processes need to follow/obey a locking protocol. As long as the processes follow the locking protocol/API and respect its return values, the underlying API takes care that file locking semantics work correctly. In other words, this is not an enforced locking scheme.

Mandatory Locking does not need any type of cooperation between processes. The OS guarantees the blocking/failure of system calls of a process that attempts to do any concurrent modification to a file which is already locked by another process.

In such situations, the mandatory locks would limit the flexibility in how files could be accessed and might also increase the overheads associated with accessing files.

咨询式锁定是一种合作式锁定方案,参与的进程需要遵循/遵守锁定协议。只要进程遵循锁定协议/API 并尊重其返回值,底层 API 就能保证文件锁定语义的正常工作。换句话说,这不是一个强制的锁定方案。
强制锁定不需要进程之间的任何类型的合作。操作系统保证对试图对已经被另一个进程锁定的文件进行任何并发修改的进程的系统调用的阻塞/失败。

在这种情况下,强制锁将限制对文件的访问方式的灵活性,也可能增加与访问文件相关的开销。

11.12 Provide examples of applications that typically access files according to the following methods:

提供通常按照以下方法访问文件的应用程序的例子。

Sequential

顺序

答:

Applications that access documents SUCCESSIVELY - word processors, video players, music players and web administrations.

连续访问文件的应用程序–文字处理器、视频播放器、音乐播放器和网络管理。

Random

随机

答:

Applications that access documents ARBITRARILY - databases, video and sound editors.

任意访问文件的应用程序 - 数据库、视频和声音编辑器。

Exercises

None


注:

翻译:deepl

参考资料:

[1] Operating System Concepts – 9th Edition 及其答案