《现代操作系统》精读与思考笔记 第一章 引论

  本系列博文是《现代操作系统(英文第三版)》(Modern Operating Systems,简称MOS)的阅读笔记,定位是正文精要部分的摘录和课后习题精解,因此不会事无巨细的全面摘抄,仅仅根据个人情况进行记录和推荐。由于是英文版,部分内容会使用英文原文。

  课后习题的选择标准:尽量避免单纯的概念考察(如:What is spooling?)或者简单的数值计算,而是能够引起思考加深理解的题目。为了保证解答的正确性,每道题都会附上作者的原文解答,而中文部分会适当加入自己的见解。原书答案下载地址:(需注册)

1.以count = read(fd,buffer,nbytes)为例,解析系统调用发生的过程

  原书P50~52,左边是原书调用流程表示,右边是我根据正文进行的整理。一些翻译可能不准确,标记了英文原文进行对照。

2.编写一个简单的shell

  主要是为了展示进程操作的系统调用;同时,shell的基本工作方式通过这个解剖过程不再神秘。下面伪码来自于原书P54图1-19。

#define TRUE 1  type_prompt();                 read_command(command,parameters);              waitpid(-   } else {        execve(command,parameters,  }}

3.link原理

  最初接触unix时,是按照windows中“快捷方式”的形式理解link的。当然,这个是不全面不正确的。先看看MOS是上link的使用和原理介绍吧。(P57~58)

To see how link works, consider the situation of Fig. 1-21(a). Here are two users, ast and jim, each having his own directory with some files. If ast now executes a program containing the system call

link(“/usr/jim/memo”, “/usr/ast/note”);

the file memo in jinn’s directory is now entered into ast’s directory under the name note. Thereafter, /usr/jim/memo and /usr/ast/note refer to the same file. As an aside, whether user directories are kept in /usr, /user, /home, or somewhere else issimply a decision made by the local system administrator.

Understanding how link works will probably make it clearer what it does. Every file in UNIX has a unique number, its i-number, that identifies it. This i-number is an index into a table of i-nodes, one per file, telling who owns the file,where its disk blocks are, and so on. A directory is simply a file containing a set of (i-number, ASCII name) pairs. In the first versions of UNIX, each directory entry was 16 bytes-2 bytes for the i-number and 14 bytes for the name. Now a more complicated structure is needed to support long file names, but conceptuallya directory is still a set of (i-number, ASCII name) pairs. In Fig. 1-21, mail has i-number 16, and so on. What link does is simply create a new directory entry with a (possibly new) name, using the i-number of an existing file. In Fig. 1-21(b), twoentries have the same i-number (70) and thus refer to the same file. If either oneis later removed, using the unlink system call, the other one remains. If both areremoved, UNIX 00sees that no entries to the file exist (a field in the i-node keepstrack of the number of directory entries pointing to the file), so the file is removedfrom the disk.

  概括地说,是这样的:每个UNIX下的文件都有一个独一无二的索引节点号i-number,并用它进行区分。这个i-number是索引节i-node的索引,而i-node标识了文件信息:拥有者、硬盘块号等等。link后的产生文件与原文件具有相同的i-number,而文件名可以不同。实际上是对同一个文件对象的引用。rm和unlink都只是把引用计数减一,直到为0时才真正的从硬盘上删除。

  我在实践时发现rm和unlink对于link出的文件行为相同,查阅了下资料进行理解:linux shell中,unlink和rm命令有什么区别呢?

  另外上文提到的link,具体到Linux环境中,是硬链接。不过想起Linux一般用ln建立链接(默认为硬链接,带-s选项是软链接/符号链接),ln和link又有什么异同?根据终端中输入man link后的提示,输入info coreutils ‘link invocation’可见:

`link’ creates a single hard link at a time. It is a minimalistinterface to the system-provided `link’ function. *Note Hard Links:(libc)Hard Links. It avoids the bells and whistles of the morecommonly-used `ln’ command (*note ln invocation::).

  关于硬链接和软链接/符号链接的区别,在这里不赘述。

4.虚拟机(P67~71)

  这个概念如果只学操作系统这门课最多有个印象,如果和实际中最常见的应用VMware和JVM相联系,就非常好理解了。这里不贴原文。

课后习题选14.What is the key difference between a trap and an interrupt?

译:trap指令和中断的关键区别是什么?)

Answer:

  A trap is caused by the program and is synchronous with it. If the program isrun again and again, the trap will always occur at exactly the same position inthe instruction stream. An interrupt is caused by an external event and itstiming is not reproducible.

分析:

  trap是程序本身引起的,因此它是可重现的,每次执行到这个程序某一处都会发生;而中断是外部事件导致的,因此它发生的时机是不可重复的(除非人为干预)。

就会犯错误,就会有无数次让自己跌倒的机会出现,

《现代操作系统》精读与思考笔记 第一章 引论

相关文章:

你感兴趣的文章:

标签云: