現代操作系統期末復習


答案僅供參考,不保證全部正確

第一章 引論

1. What are the two main functions of an operating system?

1.為應用程序(程序員)提供資源集的清晰抽象
2.管理硬件資源

9. There are several design goals in building an operating system, for example, resource
utilization, timeliness, robustness, and so on. Give an example of two design goals that
may contradict one another.

標答:
Consider fairness and real time. Fairness requires that each process be allocated
its resources in a fair way, with no process getting more than its fair
share. On the other hand, real time requires that resources be allocated based
on the times when different processes must complete their execution. A realtime
process may get a disproportionate share of the resources.

15. Consider a computer system that has cache memory, main memory (RAM) and disk,
and an operating system that uses virtual memory. It takes 1 nsec to access a word
from the cache, 10 nsec to access a word from the RAM, and 10 ms to access a word
from the disk. If the cache hit rate is 95% and main memory hit rate (after a cache
miss) is 99%, what is the average time to access a word?

t = (0.95 * 1ns)(命中緩存)
    + (0.05 * 0.99 * 10ns)(沒命中緩存但命中內存)
	+ (0.05 * 0.01 * 10ms)(都沒命中) = 5001.445 ns

23. A file whose file descriptor is fd contains the following sequence of bytes:
3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5. The following system calls are made:
lseek(fd, 3, SEEK SET);
read(fd, &buffer, 4);
where the lseek call makes a seek to byte 3 of the file. What does buffer contain after
the read has completed?

a[]={3,1,4,1,5,9,2,6,......}
a[0]=3
lseek(,3,)之后:a[0+3]=a[3]=4
read(,,4)就是取a[3]~a[3+4-1]
即1 5 9 2

33. Here are some questions for practicing unit conversions:
(a) How long is a nanoyear in seconds?
(b) Micrometers are often called microns. How long is a megamicron?
(c) How many bytes are there in a 1-PB memory?
(d) The mass of the earth is 6000 yottagrams. What is that in kilograms?

翻書,嗯背,我也不會
mmnpfazy
KMGTPEZY
image


第二章 進程與線程

A computer has 4 GB of RAM of which the operating system occupies 512 MB.
The processes are all 256 MB (for simplicity) and have the same characteristics.
If the goal is 99% CPU utilization, what is the maximum I/O wait that can be tolerated?

(中文書p54)利用率=1-pow(p,n)
因為cpu占用率99%,0.99=1-pow(p,n),pow(p,n)=0.01
題目需要算p(I/O wait)
每個進程256MB,當前還能裝得下(4096MB-512MB)/256MB=14個進程(這個算的是n)
pow(p,14)=0.01
p=0.72

18. What is the biggest advantage of implementing threads in user space? What is the
biggest disadvantage?

最大的優點是高性能,無需陷入內核切換線程;
最大的缺點是一旦某個線程被阻塞,會導致整個進程阻塞

45. Five batch jobs. A through E, arrive at a computer center at almost the same time.
They hav e estimated running times of 10, 6, 2, 4, and 8 minutes. Their (externally determined)
priorities are 3, 5, 2, 1, and 4, respectively, with 5 being the highest priority.
For each of the following scheduling algorithms, determine the mean process
turnaround time. Ignore process switching overhead.
(a) Round robin.
(b) Priority scheduling.
(c) First-come, first-served (run in order 10, 6, 2, 4, 8).
(d) Shortest job first.
For (a), assume that the system is multiprogrammed, and that each job gets its fair
share of the CPU. For (b) through (d), assume that only one job at a time runs, until it
finishes. All jobs are completely CPU bound.

(a)輪轉調度(每次從剩余的進程中選出最小的,所有減去最小的,直到全部完成)
每個進程平均完成時間(10 + (10+8) + (10+8+6) + (10+8+6+4) + (10+8+6+4+2))/5 = 22mins
順序:(
10 6 2 4 8 (cost 10mins)
8 4 0 2 6 (cost 8mins)
6 2 0 0 4 (cost 6mins)
4 0 0 0 2 (cost 4mins)
2 0 0 0 0 (cost 2mins)
0 0 0 0 0
)

(b)優先級調度(按題目要求先執行優先級大的再執行小的)
順序:6 8 10 2 4
t = (6 + 14 + 24 + 26 + 30) / 5 = 20mins

(c)先來先服務(按順序即可)
t = (10 + 16 + 18 + 22 + 30) / 5 = 19.2mins

(d)最短作業優先(從小到大執行)
順序:2 4 6 8 10
t = (2 + 6 + 12 + 20 + 30) / 5 = 14mins

60. Suppose that a university wants to show off how politically correct it is by applying the
U.S. Supreme Court’s ‘‘Separate but equal is inherently unequal’’ doctrine to gender as
well as race, ending its long-standing practice of gender-segregated bathrooms on campus.
However, as a concession to tradition, it decrees that when a woman is in a bathroom,
other women may enter, but no men, and vice versa. A sign with a sliding
marker on the door of each bathroom indicates which of three possible states it is currently
in:
• Empty
• Women present
• Men present
In some programming language you like, write the following procedures:
woman wants to enter, man wants to enter, woman leaves, man leaves. You
may use whatever counters and synchronization techniques you like.

開放題,不知道這樣寫對不對

semaphore mutex = 1;
int flag = 0;
int man = 0, woman = 0;

void man_wants_to_enter()
{
	while(flag != 1)
		P(mutex), flag = 1;
	man++;
}

void woman_wants_to_enter()
{
	while(flag != 2)
		P(mutex), flag = 2;
	woman++;
}

void man_leaves()
{
	if (flag == 1)
	{
		man--;
		if (!man)
			flag = 0, V(mutex);
	}
}

void woman_leaves()
{
	if (flag == 2)
	{
		woman--;
		if (!woman)
			flag = 0, V(mutex);
	}
}


第三章 內存管理

4.Consider a swapping system in which memory consists of the following hole sizes in
memory order: 10 MB, 4 MB, 20 MB, 18 MB, 7 MB, 9 MB, 12 MB, and 15 MB.
Which hole is taken for successive segment requests of
(a) 12 MB
(b) 10 MB
(c) 9 MB
for first fit? Now repeat the question for best fit, worst fit, and next fit

First fit takes 20 MB, 10 MB, 18 MB. Best fit takes 12 MB, 10 MB, and 9
MB. Worst fit takes 20 MB, 18 MB, and 15 MB. Next fit takes 20 MB, 18
MB, and 9 MB.

首次適配:從前往后找到第一個可以放入的塊
先把12MB放入20MB,現在變成10 MB, 4 MB, 8 MB, 18 MB, 7 MB, 9 MB, 12 MB, and 15 MB
再10MB放入10MB,變成0 MB, 4 MB, 8 MB, 18 MB, 7 MB, 9 MB, 12 MB, and 15 MB
最后把9MB放入18MB,變成0 MB, 4 MB, 8 MB, 9 MB, 7 MB, 9 MB, 12 MB, and 15 MB

最佳適配:找到可以放入的最小塊
先把12MB放入12MB,現在變成10 MB, 4 MB, 20 MB, 18 MB, 7 MB, 9 MB, 0 MB, and 15 MB.
再10MB放入10MB,變成0 MB, 4 MB, 20 MB, 18 MB, 7 MB, 9 MB, 0 MB, and 15 MB.
最后把9MB放入9MB,變成0 MB, 4 MB, 20 MB, 18 MB, 7 MB, 0 MB, 0 MB, and 15 MB.

最差適配:找到可以放入的最大塊
先把12MB放入20MB,現在變成10 MB, 4 MB, 8 MB, 18 MB, 7 MB, 9 MB, 12 MB, and 15 MB.
再10MB放入18MB,變成10 MB, 4 MB, 8 MB, 8 MB, 7 MB, 9 MB, 12 MB, and 15 MB.
最后把9MB放入15MB,變成10 MB, 4 MB, 8 MB, 8 MB, 7 MB, 9 MB, 12 MB, and 6 MB.

下次適配:從上次適配結束的點向前找到第一個適配的塊
先把12MB放入20MB,現在變成(10 MB, 4 MB, )8 MB, 18 MB, 7 MB, 9 MB, 12 MB, and 15 MB
再10MB放入18MB,變成(10 MB, 4 MB,) (8 MB, )8 MB, 7 MB, 9 MB, 12 MB, and 15 MB
最后把9MB放入9MB,變成(10 MB, 4 MB,) (8 MB, )(8 MB, 7 MB,) 0 MB, 12 MB, and 15 MB

)

19. A computer with a 32-bit address uses a two-level page table. Virtual addresses are
split into a 9-bit top-level page table field, an 11-bit second-level page table field, and
an offset. How large are the pages and how many are there in the address space?

偏移量=32-9-11=12位
所以頁面大小為pow(2,12)B=4KB
地址空間中的頁面個數:pow(2,9+11)=1M個
(一級頁表總共指向pow(2,11)個頁面,二級頁表指向pow(2,9)個一級頁表)

28. If FIFO page replacement is used with four page frames and eight pages, how many
page faults will occur with the reference string 0172327103 if the four frames are initially
empty? Now repeat this problem for LRU.

image

32. In the WSClock algorithm of Fig. 3-20(c), the hand points to a page with R = 0. If
τ = 400, will this page be removed? What about if τ = 1000?

The age of the page is 2204 − 1213 = 991. 
If τ = 400, it is definitely out of the working set and was not recently referenced so it 
will be evicted.
The τ = 1000 situation is different(991 <= 1000 it is in the working set). Now the page falls 
within the working set (barely), so it is not removed.

36. A computer has four page frames. The time of loading, time of last access, and the R
and M bits for each page are as shown below (the times are in clock ticks):
image
(a) Which page will NRU replace?
(b) Which page will FIFO replace?
(c) Which page will LRU replace?
(d) Which page will second chance replace?

(中文書p118)
NRU removes page 2.(優先刪R=0,m=0(00)的,然后是01,再是10,最后是11)
FIFO removes page 3.(刪載入時間最早的)
LRU removes page 1.(刪最近訪問時間最早的)
Second chance removes page 2.(
按照載入時間依次入隊
然后從隊首開始
如果R為1,則置為0,放到隊尾
如R為0,刪掉它,流程結束
)

47. We consider a program which has the two segments shown below consisting of instructions
in segment 0, and read/write data in segment 1. Segment 0 has read/execute protection,
and segment 1 has just read/write protection. The memory system is a demand-paged virtual
memory system with virtual addresses that have a 4-bit page number, and
a 10-bit offset. The page tables and protection are as follows (all numbers in the table
are in decimal):
image
For each of the following cases, either give the real (actual) memory address which results
from dynamic address translation or identify the type of fault which occurs (either
page or protection fault).
(a) Fetch from segment 1, page 1, offset 3
(b) Store into segment 0, page 0, offset 16
(c) Fetch from segment 1, page 4, offset 28
(d) Jump to location in segment 1, page 3, offset 32

(a)不會出錯,地址為11100000000011(or 1110 0011 or 0xD3)
(b)保護錯誤: Write to read/execute segment
(c)缺頁錯誤
(d)保護錯誤: Jump to read/write segment

第四章 文件系統

21. Name one advantage of hard links over symbolic links and one advantage of symbolic
links over hard links.

硬鏈接不需要額外的磁盤空間,只需要i-node上的一個counter去持續記錄有多少目錄項指向它。
符號鏈接需要空間去存儲它指向文件的名稱,並且符號鏈接可以指向其他機器或者網絡上的文件。

23. Consider a 4-TB disk that uses 4-KB blocks and the free-list method. How many block
addresses can be stored in one block?

4TB/4KB = pow(2,30)塊,32是比它大又最接近的2進制位(lower_bound),因此每個塊地址可以是32位(4Byte).
每個塊可以存儲4KB/4B = 1024個塊地址

36. Consider the idea behind Fig. 4-21, but now for a disk with a mean seek time of 6
msec, a rotational rate of 15,000 rpm, and 1,048,576 bytes per track. What are the data
rates for block sizes of 1 KB, 2 KB, and 4 KB, respectively?

中文書(p168):假設磁盤每道1MB,其旋轉時間為8.33ms,平均尋道時間為5ms。
讀取一個k個字節的塊所需的時間是尋道時間、旋轉延遲和傳送時間之和:5 + 4.165 + (k/1MB)*8.33

旋轉時間(轉一圈要花的時間): 60s/15000rpm = 4ms/r
尋道時間 :6ms
每道大小 :1048576B
套公式得: 6 + 4/2 + (k/1048576) * 4

For blocks of 1 KB, 2 KB, and 4 KB(把這3個當成k代入), the access times are about 8.0039 msec, 
8.0078msec, and 8.0156 msec, respectively (hardly any different).

傳輸1KB需要8.0039ms,那么1s傳輸1000/8.0039*1 KB/s
同理。。。

40. A UNIX file system has 4-KB blocks and 4-byte disk addresses. What is the maximum
file size if i-nodes contain 10 direct entries, and one single, double, and triple indirect
entry each?

The i-node holds 10 pointers. The single indirect block holds 1024 pointers.
The double indirect block is good for 10242 pointers. The triple indirect block
is good for 10243 pointers. Adding these up, we get a maximum file size of
1,074,791,434 blocks, which is about 16.06 GB.

4KB/4B = 1024塊地址/每塊

直接表項:10
一次間接塊:1024
二次間接塊:1024*1024
三次間接塊:1024*1024*1024

相加得:1074791434塊,每塊4KB(solution給的是16.06GB,不知道是不是我理解的有問題)


第五章 I/O

13. Explain how an OS can facilitate installation of a new device without any need for
recompiling the OS.

對每種設備類型,操作系統定義一組驅動程序必須支持的函數。驅動程序通常包含一張table,具有這些函數指向驅
動程序自身的指針。當新的驅動程序裝載時,操作系統記錄這張表格的地址,在表中新建一個條目,並將指向驅動程
序的函數指針填入。

28. Consider a magnetic disk consisting of 16 heads and 400 cylinders. This disk has four
100-cylinder zones with the cylinders in different zones containing 160, 200, 240. and
280 sectors, respectively. Assume that each sector contains 512 bytes, average seek
time between adjacent cylinders is 1 msec, and the disk rotates at 7200 RPM. Calculate
the (a) disk capacity, (b) optimal track skew, and (c) maximum data transfer rate.

容量(disk capacity): 磁頭數 * 柱面數 * 扇區數

Capacity of zone 1: 16 × 100 × 160 × 512 = 131072000 bytes
Capacity of zone 2: 16 × 100 × 200 × 512 = 163840000 bytes
Capacity of zone 3: 16 × 100 × 240 × 512 = 196608000 bytes
Capacity of zone 4: 16 × 100 × 280 × 512 = 229376000 bytes
Sum = 131072000 + 163840000 + 196608000 + 229376000 = 720896000

最優磁道斜進(中文書p211): 尋道時間 / 通過每個扇區需要的時間

A rotation rate of 7200 means there are 120 rotations/sec.
In the 1 msec track-to-track seek time, 0.120 of the sectors are covered.//1ms轉0.12圈


//4個柱面通過每個扇區需要的時間不同,分別計算
In zone 1, the disk head will pass over 0.120 × 160 sectors in 1 msec, so, optimal track
skew for zone 1 is 19.2 sectors.(1ms轉0.12*160 = 19.2個扇區)

In zone 2, the disk head will pass over
0.120 × 200 sectors in 1 msec, so, optimal track skew for zone 2 is 24 sectors.

In zone 3, the disk head will pass over 0.120 × 240 sectors in 1 msec,
so, optimal track skew for zone 3 is 28.8 sectors.

In zone 4, the disk head will pass over 0.120 × 280 sectors in 1 msec, so, optimal track skew for zone 3 is 33.6 sectors.


最大數據傳輸率:
	數據傳輸率=轉速 \* 扇區數 \* 每個扇區的大小
	4個柱面轉速相同,每個扇區的大小相同,那么扇區數選最大的:120\*280\*512=17203200 Byte/s

31. Disk requests come in to the disk driver for cylinders 10, 22, 20, 2, 40, 6, and 38, in
that order. A seek takes 6 msec per cylinder. How much seek time is needed for
(a) First-come, first served.
(b) Closest cylinder next.
(c) Elevator algorithm (initially moving upward).
(d) 改進的電梯算法
In all cases, the arm is initially at cylinder 20.


(a)FCFS:按照給的順序把距離求出來相加然后乘尋道時間即可
	sum = (20-10)+(22-10)+(22-20)+(20-2)+(40-2)+(40-6)+(38-6) = 146ms
	6 * sum = 876 ms
(b)SSF:由於從20開始,每次找離當前位置最近的,求和乘尋道時間
	6 * (2+12+4+4+36+2)=360ms
	順序:20 22 10 6 2 38 40
(c)elevator(中文書P214):從開始位置先向上遍歷完所有上面的請求再往下跑
	6*(2+16+2+30+4+4)=348ms
	順序:20 22 38 40 10 6 2
(d)改進:從開始位置先向上遍歷完所有上面的請求  再去最低的沒有完成的請求處往上遍歷
	6*(2+16+2+38+4+4)=396ms
	順序:20 22 38 40 2 6 10

50. A thin-client terminal is used to display a Web page containing an animated cartoon of
size 400 pixels × 160 pixels running at 10 frames/sec. What fraction of a 100-Mbps
Fast Ethernet is consumed by displaying the cartoon?

consumed = fps * 長 * 寬 * 像素大小 / 帶寬 * 100%
	 = 10*400*160* 3Byte(24bit) / (100Mb/s) = 15.36%


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM