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

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

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

 

概念名称回顾

  硬件简介:处理器、存储器、磁盘、磁带、I/O设备、总线

  各式各样的操作系统:大型机OS、服务器OS、多进程OS、个人电脑OS、手持电脑OS、嵌入式OS、实时OS、智能卡OS

  操作系统概念:进程、地址空间、文件、I/O、保护策略、Shell

  系统调用

  操作系统结构:单一系统、层次化系统、微内核、C/S、虚拟机、外内核

 

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

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

 

2.编写一个简单的shell

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

#define TRUE 1

while(TRUE) {                  /* repeat forever */
  type_prompt();               /* display prompt on the screen */
  read_command(command,parameters);    /* read input from terminal */
  if(fork()!=0) {               /* fork off child process */
    /* Parent code */
    waitpid(-1,&status,0);         /* wait for child to exit */
   } else {
    /* Child code */
    execve(command,parameters,0);    /* execute command */
  }

}

 

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 is simply 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 conceptually a 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), two entries have the same i-number (70) and thus refer to the same file. If either one is later removed, using the unlink system call, the other one remains. If both are removed, UNIX 00sees that no entries to the file exist (a field in the i-node keeps track of the number of directory entries pointing to the file), so the file is removed from 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 minimalist interface to the system-provided `link' function. *Note Hard Links: (libc)Hard Links. It avoids the bells and whistles of the more commonly-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 is run again and again, the trap will always occur at exactly the same position in the instruction stream. An interrupt is caused by an external event and its timing is not reproducible.

分析:

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

  顺便复习下trap和interrupt,前者用于从用户态转化到内核态,以便于执行一些需要特权才能完成的操作;后者指处理器接收到来自硬件或软件的信号,提示发生了某个事件,应该被注意,这种情况就称为中断(维基链接)。在我看来,它们的共同之处在于都需要进行现场保存、并在处理完返回。

 

16.Is there any reason why you might want to mount a file system on a nonempty directory? If so, what is it?

Answer:

  Mounting a file system makes any files already in the mount point directory inaccessible, so mount points are normally empty. However, a system admin-istrator might want to copy some of the most important files normally located in the mounted directory to the mount point so they could be found in their

normal path in an emergency when the mounted device was being repaired.

分析:

  将文件系统挂载在某个目录下会导致该目录原先的文件全部被隐藏而不可访问,而在卸载后恢复。解答中的场景我不是很明白,下面是第二版中文版的答案

装配文件系统将使得装配目录中已有的任何文件都不可访问,因此装配点通常都是空的。然而,系统管理人员可能需要将某些位于被装配目录中的非常重要的文件复制到装配点,使得他们在进行设备检查或修理时,可以在紧急事件中的普通路径上找到这些文件。

  试了下,以/home/wy/test和/home/wy/test2为例,其中前者包括1.txt、2.txt两个文件,后者只有1.txt这个文件,并且三者内容都不同。执行

sudo mount -B /home/wy/test /home/wy/test2

  这时,test是被装配目录,test2是装配点,无论哪个路径都是test中的内容。执行

cp 1.txt /home/wy/test2

  提示:cp: “1.txt” 及 “/home/wy/test2/1.txt” 为同一文件

  那么使用

cp 1.txt /home/wy/test2/test1.txt

  执行卸载

sudo umount /home/wy/test /home/wy/test2

  可以发现,原来的test2中并无任何变化,新拷贝的文件仍在test里。个人理解解答的意思只是为了方便使用而已。

  另,mount到root下只能重启。

 

22.What is the essential difference between a block special file and a character special file?

译:块特殊文件和字符特殊文件的基本区别是什么?

Answer:

  Block special files consist of numbered blocks,each of which can be read or written independently of all the other ones. It is possible to seek to any block and start reading or writing. This is not possible with character special files.

分析:

  特殊文件(special file)将I/O设备抽象成了文件。

 

24.The client-server model is popular in distributed systems. Can it also be used in a single-computer system?

译:客户/服务器模型在分布式系统中普遍使用,而对于单机系统是否可以使用?

Answer:

  Yes it can, especially if the kernel is a message-passing system.

分析:

  把程序之间的信息交互视为通信,那么答案显而易见。这就好比我们可以用socket()实现不同主机的进程通信,也可以实现本机不同进程的通信一样。

 

30. Write a shell that is similar to Fig. 1-19 but contains enough code that it actually works so you can test it. You might also add some features such as redirection of input and output, pipes, and background jobs.

分析:

  把图1-19也就是本文第2条的简易shell进行实现。可以增加一些特性如输入输出重定向、管道、后台作业。

  这个实现过程打算单独成文。

 

31. If you have a personal UNIX-like system (Linux, MINIX, Free BSD, etc.) available that you can safely crash and reboot, write a shell script that attempts to create an unlimited number of child processes and observe what happens. Before running the experiment, type sync to the shell to flush the file system buffers to disk to avoid ruin­ing the file system. Note: Do not try this on a shared system without first getting per­mission from the system administrator. The consequences will be instantly obvious so you are likely to be caught and sanctions may follow.

译:在UNIX中写个shell脚本,创建无限个子进程并观察发生了什么。请确保你的行为获得的了系统管理员的许可。

分析:

  完成这个工作可以使用著名的fork炸弹,其代码如下:

:() { :|:& };:

  原理可以参考http://www.ha97.com/2618.html(备份链接:http://blog.csdn.net/ppby2002/article/details/6542399)。

  输入后我的XShell显示[1] 20773并显示提示符,但是输入ps -a完全没反应,我采取了强制重启虚拟机的解决方法。

posted @ 2013-11-02 18:45  五岳  阅读(8467)  评论(1编辑  收藏  举报
回到顶部