本系列博文是《现代操作系统(英文第三版)》(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 1while(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完全没反应,我采取了强制重启虚拟机的解决方法。

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

  1. 《实用机器学习》(孙亮 黄倩.著)笔记——第一章 引论

    机器学习的主要类型: 一.回归问题 二.分类问题 三.推荐问题 四.排序问题 1.1什么是机器学习 从概念上讲,在机器学习中,我们的目标是从给定的数据集中学习出一个模型f,使得它能够有效地从输入数据中 ...

  2. 【王道考研】操作系统 笔记 第一章

    特此鸣谢王道考研 本文参考王道考研的相关课程 若有侵权请联系,立删 其余笔记链接: [王道考研]操作系统笔记 第一章_才疏学浅743的博客-CSDN博客 [王道考研]操作系统 笔记 第二章上 进程调度 ...

  3. 计算机操作系统 (第四版汤小丹老师) 复习笔记第一章

    教材为西安电子科技大学 汤小丹老师 第四版 1.1操作系统目标和作用 1.目标 目前存在着多种类型的OS,不同类型的OS,其目标各有所侧重.通常在计算机硬件上配置的OS,其目标有以下几点: 方便性 便 ...

  4. 《Go语言圣经》学习笔记 第一章 Go语言入门

    Go语言圣经学习笔记 第一章 Go语言入门 目录 Hello, World 命令行参数 查找重复的行 GIF动画 获取URL 并发获取多个URL Web服务 本章要点 注:学习<Go语言圣经&g ...

  5. 2010计算机知识点总结,2010年全国职称计算机考试:知识点笔记第一章

    第一章 信息技术与计算机文化 1. 信息技术与计算机 1) 信息技术的概念:利用科学的原理.方法及先进的工具和手段,有效地开发和利用信息资源的技术体系.包括微电子技术.计算机技术.软件技术.通信技术等 ...

  6. 《MAC OS X 技术内幕》读书笔记第一章:MAC OS X的起源

    <MAC OS X 技术内幕>读书笔记第一章:MAC OS X的起源 前言 1 System x.x系列 1.1System 1.0(1984年1月24日) 1.2System 2.x(1 ...

  7. Android群英传神兵利器读书笔记——第一章:程序员小窝——搭建高效的开发环境

    Android群英传神兵利器读书笔记--第一章:程序员小窝--搭建高效的开发环境 目录 1.1 搭建高效的开发环境之操作系统 1.2 搭建开发环境之高效配置 基本环境配置 基本开发工具 1.3 搭建程 ...

  8. 微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关

    微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关 1. 微服务简介 1.1 服务架构演变 1.2 SpringCloud ...

  9. 深入理解 C 指针阅读笔记 -- 第一章

    上周末,我在图书馆看到了这本很薄的书 -- <深入理解 C 指针>       这本书中写的内容,个人感觉适合一个初学者,内容不是很难.我也读了下,对每一章都做了笔记,笔记都是用代码的形式 ...

最新文章

  1. cufflinks基于dataframe数据绘制股票数据:直方图、时序图
  2. Data Lake Analytics + OSS数据文件格式处理大全
  3. Python Model : glob 文件路径查找
  4. 高中计算机应用基础试讲,试讲计算机应用基础.ppt
  5. Supervisor使用教程
  6. 如何在ORACLE CLOUD中创建和访问容器集群丨内附官方文档链接
  7. 2014 网选 上海赛区 hdu 5047 Sawtooth
  8. intext:企业_企业中的微服务:敌是友?
  9. java-String类
  10. java 快速排序 递归_Java递归快速入门
  11. Web.config文件中的location元素
  12. 非科班出身,如何成为程序员?
  13. ECMAScript5新特性总结
  14. 如何看待腾讯市值(按 2012 年 8 月 17 日股价)超过 Facebook?
  15. AUTOMATE THE BORING STUFF WITH PYTHON读书笔记 - 第4章:LISTS
  16. unity 调整画布大小_使用画布,拖放和File API调整图像大小
  17. WPS解决账号登录验证问题(软件抓包重写过验证通用方法)
  18. 使用Mapper代理开发解决硬编码问题
  19. 管程法实现生产者消费者问题
  20. 给Ubuntu安装驱动(nvidia)保姆级教程(方法一)

热门文章

  1. 黄学长模拟day1 球的序列
  2. python安装xlrd和xlwt及应用
  3. 利用CMD在vim中直接编译
  4. 利用数据库来填充UltraWebTree
  5. Report Builder 3.0连接ORACEL数据库
  6. 云栖社区,诚邀技术同仁一同入驻
  7. redis相关(搭建和数据落盘)
  8. 《C++编程惯用法——高级程序员常用方法和技巧》——2.7 Const
  9. Java多线程时内存模型
  10. 安装配置Collabnet_Subversion