操作系統導論期末複習題

進程

1、When a process makes a system call to transmit a TCP packet over the network, which of the following steps do NOT occur always?

A、The process moves to kernel mode.

B、The program counter of the CPU shifts to the kernel part of the address space.

C、The process is context-switched out and a separate kernel process starts execution.

D、The OS code that deals with handling TCP/IP packets is invoked.

正確答案: C
在這裏插入圖片描述
2、Which of the following C library functions do NOT directly correspond to (similarly named) system calls? That is, the implentations of which of these C library functions are NOT straightforward invocations of the underlying system call?

A、system, which executes a bash shell command.

B、fork, which creates a new child process.

C、exit, which terminates the current process.

D、strlen, which returns the length of a string.

正確答案: AD
在這裏插入圖片描述
3、Consider a parent process that has forked a child in the code snippet below.

int count = 0;
ret = fork();
if(ret == 0) {
printf(“count in child=%d\n”, count);
} else {
count = 1;
}

The parent executes the statement ”count = 1” before the child executes for the first time. Now, what is the value of count printed by the code above? Assume that the OS implements a regular fork (not a copy-on-write fork).

正確答案:
第一空: 0
在這裏插入圖片描述

fork一次調用,兩次返回,在子進程中,返回;在父進程中返回新創建的子進程ID;如果出現錯誤,返回一個負值。這兩個進程的變量都是獨立的,存在不同的地址中,不是共用的。

4、Consider a parent process P that has forked a child process C in the program below.

int a = 5;
int fd = open(…) //opening a file
int ret = fork();
if(ret > 0) {
close(fd);
a = 6;

} else if (ret == 0) {
printf(“a=%d\n”, a);
read(fd, something);
}

After the new process is forked, suppose that the parent process is scheduled first, before the child process. Once the parent resumes after fork, it closes the file descriptor and changes the value of a variable as shown above. Assume that the child process is scheduled for the first time only after the parent completes these two changes.
(a)____ is the value of the variable a as printed in the child process, when it is scheduled next.
(b) Will the attempt to read from the file descriptor succeed in the child? Yes or No

正確答案:
第一空: 5
第二空: Yes
在這裏插入圖片描述

5、Can two processes be concurrently executing the same program executable?

正確答案: √
在這裏插入圖片描述
6、Can two running processes share the complete process image in physical memory (not just parts of it)?

正確答案: ×
在這裏插入圖片描述
7、A process in user mode cannot execute certain privileged hardware instructions.

正確答案: √
在這裏插入圖片描述
8、A context switch can occur only after processing a timer interrupt, but not after any other system call or interrupt.

正確答案: ×
在這裏插入圖片描述

上下文切換(有時也稱做進程切換或任務切換)是指 CPU 從一個進程或線程切換到另一個進程或線程。上下文切換隻能發生在內核態中。內核態是CPU的一種有特權的模式,在這種模式下只有內核運行並且可以訪問所有內存和其他系統資源。其他的程序,如應用程序,在最開始都是運行在用戶態,但是他們能通過系統調用來運行部分內核的代碼。
上下文切換在多任務操作系統中是一個必須的特性。多任務操作系統是指多個進程運行在一個 CPU 中互不打擾,看起來像同時運行一樣。這個並行的錯覺是由於上下文在高速的切換(每秒幾十上百次)。當某一進程自願放棄它的 CPU 時間或者系統分配的時間片用完時,就會發生上下文切換。
上下文切換有時也因硬件中斷而觸發。硬件中斷是指硬件設備(如鍵盤、鼠標、調試解調器、系統時鐘)給內核發送的一個信號,該信號表示一個事件(如按鍵、鼠標移動、從網絡連接接收到數據)發生了。

9、A process undergoes a context switch every time it enters kernel mode from user mode.

正確答案: ×
在這裏插入圖片描述

當系統調用發生時 CPU 切換到內核態,這應該叫做模式切換而不是上下文切換,因爲沒有改變當前的進程。

10、A C program cannot directly invoke the OS system calls and must always use the C library for this purpose.

正確答案: ×
在這裏插入圖片描述

進程調度策略和內存虛擬化機制

1.假設一個簡單的分段系統支持兩個段:一個用於代碼和堆(正增長),一個用於堆棧(負增長)。虛擬地址空間大小是128個字節。物理內存大小是512個字節。段寄存器的信息:段0的基址(正向增長):0,界限:20(十進制)。段1的基址(負向增長):0x200(十進制512),界限:20(十進制)。下面哪些是有效的虛擬內存訪問?

A、0x1d (十進制: 29)

B、0x17b (十進制: 123)

C、0x10 (十進制: 16)

D、0x5a (十進制: 90)

E、0x0a (十進制: 10)

正確答案: BCE

2.下面關於多層反饋隊列(MLFQ)調度的描述,哪些是可能的(正確的)?

A、MLFQ瞭解有關運行作業的一些信息

B、MLFQ會使長期運行的作業捱餓

C、MLFQ爲作業們使用了不同長度的時間片

D、MLFQ使用了輪轉(round robin)

E、MLFQ有時候會忘記它已經瞭解到的有關作業的信息

正確答案: ACDE

3.對於下面的作業,使用FIFO調度器,並且只有一個CPU。每個作業有一個“需要的”運行時間,即完成作業需要多少個CPU時間單元。作業A在時刻0到達,需要X個時間單元,作業B在時刻5到達,需要Y個時間單元,作業C在時刻10到達,需要Z個時間單元。假設平均週轉時間在10到20(包含)之間,下面A、B和C的運行時間,哪些是可能的?

A、A=10, B=10, C=10

B、A=20, B=20, C=20

C、A=5, B=10, C=15

D、A=20, B=30, C=40

E、A=30, B=1, C=1

正確答案: AC

4.對於虛擬化內存來說,最簡單的技術是動態重定位,或者“基址-界限”,假設有以下的系統特徵:一個1KB的地址空間,基址寄存器被設爲10000,界限寄存器被設爲100。下面哪些物理內存的位置能夠被運行程序合法地訪問?

A、0

B、1000

C、10000

D、10050

E、10100

正確答案: CD

memory virtualization

1.Consider a system with a 6 bit virtual address space, and 16 byte pages/frames. The mapping from virtual page numbers to physical frame numbers of a process is (0,8), (1,3), (2,11), and (3,1). Translate the following virtual addresses to physical addresses. Note that all addresses are in decimal. Write your answer in decimal.
(a) 20
(b) 40

正確答案:
第一空: 52
第二空: 184
答案解析:
(a) 20 = 01 0100 = 11 0100 = 52

(b) 40 = 10 1000 = 1011 1000 = 184

頁面內作爲偏移量所需的位數:log2(16)= 4位用於偏移;
在虛擬地址的6位中,4位用於偏移,這意味着每個進程都有2^2 = 4虛擬頁面;
由(1,3),知01對應11;
由(2,11),知10對應1011;
在這裏插入圖片描述

2.Consider a simple system running a single process. The size of physical frames and logical pages is 16 bytes. The RAM can hold 3 physical frames. The virtual addresses of the process are 6 bits in size. The program generates the following 20 virtual address references as it runs on the CPU: 0, 1, 20, 2, 20, 21, 32, 31, 0, 60, 0, 0, 16, 1, 17, 18, 32, 31, 0, 61. (Note: the 6-bit addresses are shown in decimal here.) Assume that the physical frames in RAM are initially empty and do not map to any logical page.

(a) Translate the virtual addresses above to logical page numbers referenced by the process.
That is, write down the reference string of 20 page numbers corresponding to the virtual address accesses above. Assume pages are numbered starting from 0, 1, …
answer example: 0, 1, 1, 0, 1, 2, 3, 2, 1, 2, 0, 1, 1, 0, 1, 2, 3, 2, 1, 2

(b) Calculate the number of page faults genrated by the accesses above, assuming a FIFO page replacement algorithm.

© Repeat (b) above for the LRU page replacement algorithm.

(d) What would be the lowest number of page faults achievable in this example, assuming an optimal page replacement algorithm were to be used?

正確答案:

第一空: 0, 0, 1, 0, 1, 1, 2, 1, 0, 3, 0, 0, 1, 0, 1, 1, 2, 1, 0, 3
第二空: 8
第三空: 6
第四空: 6
答案解析:
(a) For 6 bit virtual addresses, and 4 bit page offsets (page size 16 bytes), the most significant 2 bits of a virtual address will represent the page number. So the reference string is 0, 0, 1, 0, 1, 1, 2, 1, 0, 3 (repeated again).

2 (b) Page faults with FIFO = 8. Page faults on 0,1,2,3 (replaced 0), 0 (replaced 1), 1 (replaced 2), 2 (replaced 3), 3.© Page faults with LRU = 6. Page faults on 0, 1, 2, 3 (replaced 2), 2 (replaced 3), 3.

(d) The optimum algorithm will replace the page least likely to be used in future, and would look like LRU above.
在這裏插入圖片描述

3.Consider an operating system that uses 48-bit virtual addresses and 16KB pages. The system uses a hierarchical page table design to store all the page table entries of a process, and each page table entry is 4 bytes in size. What is the total number of pages that are required to store the page table entries of a process, across all levels of the hierarchical page table?

Please use 2^10 to imply power, for example,2 to the power of 10 is 2^10

Answer example: 220+220+2
正確答案:

第一空: 222+210+1
答案解析:

Page size = 2^14bytes. So, the number of page table entries = 2^48/2 14= 2^34. Each page can store 16KB/4 = 2^12 page table entries. So, the number of innermost pages = 2^34 / 2^12 = 2^22 .

Now, pointers to all these innermost pages must be stored in the next level of the page table, so the next level of the page table has 2^22 / 2^12 = 2^10 pages. Finally, a single page can store all the 2^10 page table entries, so the outermost level has one page.

So, the total number of pages that store page table entries is 2^22 + 2^10 + 1.

二.判斷題(共5題,30.0分)

  1. TLB是現代分頁系統中的一個關鍵部分。假設有下面的系統:頁大小是64個字節,TLB包含了4項,TLB替換策略是LRU(最近最少使用)。下面的每一個代表了一個虛擬內存地址軌跡,即,一個程序引用的一系列虛擬內存地址。在下面的軌跡中,可能通過TLB加速執行的打“√”,不能加速的打“×”。

0, 100, 200, 1, 101, 201, … (repeats in this pattern)

(6.0分)
我的答案:√
2.
3.TLB是現代分頁系統中的一個關鍵部分。假設有下面的系統:頁大小是64個字節,TLB包含了4項,TLB替換策略是LRU(最近最少使用)。下面的每一個代表了一個虛擬內存地址軌跡,即,一個程序引用的一系列虛擬內存地址。在下面的軌跡中,可能通過TLB加速執行的打“√”,不能加速的打“×”。

0, 100, 200, 300, 0, 100, 200, 300, … (repeats)
正確答案:√

4.TLB是現代分頁系統中的一個關鍵部分。假設有下面的系統:頁大小是64個字節,TLB包含了4項,TLB替換策略是LRU(最近最少使用)。下面的每一個代表了一個虛擬內存地址軌跡,即,一個程序引用的一系列虛擬內存地址。在下面的軌跡中,可能通過TLB加速執行的打“√”,不能加速的打“×”。

0, 1000, 2000, 3000, 4000, 0, 1000, 2000, 3000, 4000, … (repeats)
正確答案:×

5.TLB是現代分頁系統中的一個關鍵部分。假設有下面的系統:頁大小是64個字節,TLB包含了4項,TLB替換策略是LRU(最近最少使用)。下面的每一個代表了一個虛擬內存地址軌跡,即,一個程序引用的一系列虛擬內存地址。在下面的軌跡中,可能通過TLB加速執行的打“√”,不能加速的打“×”。

0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, … (repeats)
正確答案:√

6.TLB是現代分頁系統中的一個關鍵部分。假設有下面的系統:頁大小是64個字節,TLB包含了4項,TLB替換策略是LRU(最近最少使用)。下面的每一個代表了一個虛擬內存地址軌跡,即,一個程序引用的一系列虛擬內存地址。在下面的軌跡中,可能通過TLB加速執行的打“√”,不能加速的打“×”。

300, 200, 100, 0, 300, 200, 100, 0, … (repeats)
正確答案:√

併發

1下面是一個併發程序(多線程的),pthread create() 和pthread join()運行正確,不返回錯誤。
在這裏插入圖片描述
該程序的可能輸出是什麼?

A、0

B、1000

C、999

D、998

E、1002

正確答案: CD

2Assume the following list insertion code, which inserts into a list pointed to by shared global variable head:
int List_Insert(int key) {
node_t * n = malloc(sizeof(node_t));
if (n == NULL) { return -1; }
n->key = key;
n->next = head;
head = n;
return 0;
}

This code is executed by each of three threads exactly once, without adding any synchronization primitives (such as locks). Assuming malloc() is thread-safe (i.e., can be called without worries of data races) and that malloc() returns successfully, how long might the list be when these three threads are finished executing? (assume the list was empty to begin)

A、0

B、1

C、2

D、3

E、4

正確答案: BCD
答案解析:

Assumes list empty at beginning. Because there is a race to include a node into the list, it is possible that a node gets dropped during insert. If we have three threads doing an insert, they call could succeed (they get serialized for some reason), or only one could succeed (with the other two updates lost). In no case can all get lost.
譯:假設列表開頭是空的。因爲爭相將節點包括在列表中,所以在插入過程中可能會刪除節點。如果我們有三個線程進行插入,則它們調用可能會成功(由於某種原因而被序列化),或者只有一個可能成功(其他兩個更新丟失)。在任何情況下都不會迷路。

在這裏插入圖片描述

3 Here is some more multi-threaded code:
void * printer(void * arg) {
char * p = (char * ) arg;
printf("%c", * p);
return NULL;
}
int main(int argc, char * argv[]) {
pthread_t p[5];
for (int i = 0; i < 5; i++) {
char * c = malloc(sizeof(char)); * c = ’a’ + i; // hint: ’a’ + 1 = ’b’, etc.
pthread_create(&p[i], NULL, printer, (void * ) c);
}
for (int i = 0; i < 5; i++)
pthread_join(p[i], NULL);
return 0;
}
A、abcde

B、edcba

C、cccde

D、eeeee

E、aaaaa

正確答案: AB
答案解析:

Each thread is created, and handed a unique argument with a letter in it: ’a’, or ’b’, or ’c’, or ’d’, or ’e’. The thread may run in any order, thus any output with one letter each, but in arbitrary order,is possible.
每個線程都會被創建,並傳遞一個帶有字母的唯一參數:“ a”,“ b”,“ c”,“ d”或“e”。線程可以以任何順序運行,因此任何輸出,每個輸出只有一個字母,但是可以以任意順序運行。

4.One way to avoid deadlock is to schedule threads carefully. Assume the following characteristics of threads T1, T2, and T3:
T1 (at some point) acquires and releases locks L1, L2
T2 (at some point) acquires and releases locks L1, L3
T3 (at some point) acquires and releases locks L3, L1, and L4
For which schedules below is deadlock possible?

A、T1 runs to completion, then T2 to completion, then T3 runs

B、T1 and T2 run concurrently to completion, then T3 runs

C、T1, T2, and T3 run concurrently

D、T3 runs to completion, then T1 and T2 run concurrently

E、T1 and T3 run concurrently to completion, then T2 runs

正確答案: C
答案解析:
The key to this problem: do threads that CAN deadlock ever run at the same time? If so, they might deadlock. If not, it isn’t. From the above, only T2 and T3 can deadlock, because they each grab two locks (L1 and L3) in some order.

5.Assume the following multi-threaded memory allocator, roughly sketched out as follows:
#define MAX_HEAP_SIZE 400
int bytes_left = MAX_HEAP_SIZE;
pthread_cond_t c;
pthread_mutex_t m;
void * allocate(int size) {
pthread_mutex_lock(&m);
while (bytes_left < size)
pthread_cond_wait(&c, &m);
void * ptr = …; // get mem from internal data structs
bytes_left -= size;
pthread_mutex_unlock(&m);
return ptr;
}
void free(void * ptr, int size) {
pthread_mutex_lock(&m);
bytes_left += size;
pthread_cond_signal(&c);
pthread_mutex_unlock(&m);
}
Assume all of memory is used up (i.e., bytes left is 0). Then:
One thread (T1) calls allocate(100)
Some time later, a second thread (T2) calls allocate(1000)
Finally, some time later, a third thread (T3) calls free(200)
Assuming all calls to thread library functions work as expected, which of the following are possible just after this sequence of events has taken place?

A、T1 and T2 remain blocked inside allocate()

B、T1 becomes unblocked, gets 100 bytes allocated, and returns from allocate()

C、T2 becomes unblocked, gets 1000 bytes allocated, and returns from allocate()

D、T3 becomes blocked inside free()

E、T1, T2, and T3 become deadlocked

正確答案: AB

答案解析:
The problem with this code example is that when free() signals a waiting thread, there is no guarantee it wakes “the right” thread, i.e., it may wake a thread that is waiting for too much memory. In this case, we have T1 waiting for 100 bytes, T2 waiting for 1000 bytes, and then T3 freeing only 200 bytes (not enough for T2 to succeed in allocation, but enough for T1). Thus:

A. Possible Possible, because the signal may wake T2, which then rechecks its condition, and goes back to sleep, potentially forever.
B. Possible If we’re luck (instead), T1 gets awoken by T3, and then succeeds in its allocation request.
C. Not Possible There aren’t 1000 bytes free, so this should not happen.
D. Not Possible Not possible (really) because there is nothing to get permanently blocked upon. That said, the lock() acquisition could take a little while …
E. Not Possible Only one lock here, so no deadlock can arise.

二.判斷題(共10題,50.0分)
1.Is it necessary for threads in a process to have separate stacks?

正確答案:√

2.Is it necessary for threads in a process to have separate copies of the program executable?

正確答案:×

3.Can one have concurrent execution of threads/processes without having parallelism?

正確答案:√

4.Consider N threads in a process that share a global variable in the program. If one thread makes a change to the variable, this change visible to other threads.

正確答案:√

5.Consider N threads in a process. If one thread passes certain arguments to a function in the program, these arguments are visible to the other threads.

正確答案:×

6.A Semaphore is a useful synchronization primitive. Which of the following statements are true of semaphores?
Each semaphore has an integer value

正確答案:√
答案解析:
By definition, each semaphore has a value.

7.A Semaphore is a useful synchronization primitive. Which of the following statements are true of semaphores?
If a semaphore is initialized to 1, it can be used as a lock

正確答案:√
答案解析:
This is called a binary semaphore.

8.A single lock and condition variable can be used in tandem to implement a semaphore

正確答案:√
答案解析:
This is true, along with a state variable to track the value of the semaphore.

9.Calling sem post() may block, depending on the current value of the semaphore

正確答案:×
答案解析:
Only sem wait() blocks, sem post() just does its work and returns.

sem_post函數(函數原型 int sem_post(sem_t *sem);) 作用是給信號量的值加上一個“1”。
當有線程阻塞在這個信號量上時,調用這個函數會使其中一個線程不在阻塞,選擇機制是有線程的調度策略決定的。 sem_wait函數(函數原型
int sem_wait(sem_t * sem);)
它的作用是從信號量的值減去一個“1”,但它永遠會先等待該信號量爲一個非零值纔開始做減法。

10.Semaphores can be initialized to values higher than 1

正確答案:√
答案解析:
This is useful in some cases as described in the book.

Persistance持久化

1.Consider a file system with 512-byte blocks. Assume an inode of a file holds pointers to 2 direct data blocks, and a pointer to a single indirect block. Further, assume that the single indirect block can hold pointers to 4 other data blocks. What is the maximum file size that can be supported by such an inode design?

A、512

B、1024

C、3072

D、4096

正確答案: C
在這裏插入圖片描述
2.Consider a FAT file system where disk is divided into 512 byte blocks, and every FAT entry can store an 4 bit block number. What is the maximum size of a disk partition that can be managed by such a FAT design?

A、4096

B、8192

C、2048

D、512

正確答案: B
答案解析:
2^N∗ M
在這裏插入圖片描述
3.Consider a secondary storage system of size 2 TB, with 512-byte sized blocks. Assume that the filesystem uses a multilevel inode datastructure to track data blocks of a file. The inode has 64 bytes of space available to store pointers to data blocks, including a single indirect block, a double indirect block, and several direct blocks. What is the maximum file size that can be stored in such a file system?

A、16526

B、72704

C、8461312

D、9306112

正確答案: C
答案解析:
Number of data blocks = 241/29= 2 32, so 32 bits or 4 bytes are required to store the number of a data block.

Number of data block pointers in the inode = 64/4 = 16, of which 14 are direct blocks. The single indirect block stores pointers to 512/4 = 128 data blocks. The double indirect block points to 128 single indirect blocks, which in turn point to 128 data blocks each.

So, the total number of data blocks in a file can be 14 + 128 + 128128 = 16526, and the maximum file size is 16526512 bytes.

數據塊的數量= 2 ^ 41/2 ^ 9 = 2 32,因此需要32位或4個字節來存儲數據塊的數量。
inode中數據塊指針的數量= 64/4 = 16,其中14個是直接塊。單個間接塊存儲指向512/4 =
128個數據塊的指針。雙間接塊指向128個單個間接塊,每個間接塊依次指向128個數據塊。
因此,一個文件中的數據塊總數可以爲14 + 128 + 128 * 128 = 16526,最大文件大小爲16526 * 512字節。

在這裏插入圖片描述

二.判斷題(共2題,40.0分)
1 Consider a file D1/F1 that is hard linked from another parent directory D2. Then the directory entry of this file (including the filename and inode number) in directory D1 must be exactly identical to the directory entry in directory D2.

正確答案:×
答案解析:
F (the file name can be different)
在這裏插入圖片描述

2.A soft link can create a link between files across different file systems, whereas a hard link can only create links between a directory and a file within the same file system.

正確答案:√
答案解析:
T (becasue hard link stores inode number, which is unique only within a file system)
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章