本系列博文是《現代操作系統(英文第三版)》(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 ruining the file system. Note: Do not try this on a shared system without first getting permission 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完全沒反應,我采取了強制重啟虛擬機的解決方法。