操作系統恐龍書第十版課後答案 ch03

3.1 Using the program shown in Figure 3.30, explain what the output will be at LINE A.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 5;
int main()
{
pid t pid;
pid = fork();
if (pid == 0) { /* child process */
value += 15;
return 0;
}
else if (pid > 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE A */
return 0;
}
}

結果仍然是5,因為子進程更新了它的value的副本。當控制返回給父進程時,它的值仍然是5。

3.2 Including the initial parent process, how many processes are created by the program shown in Figure 3.31?
#include <stdio.h>
#include <unistd.h>
int main()
{
/* fork a child process */
fork();
/* fork another child process */
fork();
/* and fork another */
fork();
return 0;
}

ANS:8

3.3 Original versions of Apple’s mobile iOS operating system provided no means of concurrent processing. Discuss three major complications that concurrent processing adds to an operating system.

a. CPU排程程序必須意識到不同的並行處理程序,並選擇適當的演算法來排程這些並行處理程序。

b. 並行處理程序可能需要彼此通訊,因此操作系統必須開發一個或多個方法來提供進程間通訊。

c. 由於移動設備通常具有有限的記憶體,一個管理記憶體不當的進程將對其他並行處理程序產生負面影響。因此,操作系統必須管理記憶體以支援多個並行處理程序。

3.4 Some computer systems provide multiple register sets. Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets. What happens if the new context is in memory rather than in a register set and all the register sets are in use?

CPU目前的寄存器組指針被更改,以指向包含新上下文的寄存器組,這個過程需要非常少的時間。如果上下文位於內存中,則必須選擇要移至內存的寄存器組中的一個上下文,並從內存中載入新的上下文到該寄存器組中。這個過程比具有單一寄存器組的系統需要更多時間,具體時間取決於如何選擇替換的受害者。

3.5 When a process creates a new process using the fork() operation, which of the following states is shared between the parent process and the child process?
a. Stack
b. Heap
c. Shared memory segments
只有共享記憶體段在父進程和新建立的子進程之間共享。堆疊和堆疊的副本會為新建立的進程製作。
3.6 Consider the “exactly once”semantic with respect to the RPC mechanism. Does the algorithm for implementing this semantic execute correctly even if the ACK message sent back to the client is lost due to a network problem? Describe the sequence of messages, and discuss whether “exactly once” is still preserved.

“正好一次”語義確保遠程過程將被執行正好一次,而且只有一次。確保這一點的一般算法結合了一個確認(ACK)方案與時間戳(或其他遞增計數器,允許伺服器區分重複的消息)。

一般策略是客戶端將RPC與時間戳一起發送到伺服器。客戶端還會啟動一個超時時鐘。然後客戶端將等待兩種情況中的一種發生:(1)從伺服器收到一個ACK,指示已執行遠程過程,或者(2)超時。如果客戶端超時,它會假設伺服器無法執行遠程過程,因此客戶端將再次調用RPC,發送一個較新的時間戳。客戶端可能不會收到ACK的原因有兩種:(1)原始RPC從未被伺服器接收,或(2)RPC已被伺服器正確接收和執行,但ACK丟失了。在情況(1)中,使用ACK允許伺服器最終接收並執行RPC。在情況(2)中,伺服器將收到重複的RPC,並使用時間戳將其識別為重複,以便不再執行第二次RPC。重要的是要注意,伺服器必須向客戶端發送第二個ACK,以通知客戶端已執行RPC。

3.7 Assume that a distributed system is susceptible to server failure. What mechanisms would be required to guarantee the “exactly once” semantic for execution of RPCs?

伺服器應該在穩定存儲(例如磁盤日誌)中跟蹤關於已接收的RPC操作、它們是否成功執行以及與這些操作相關的結果的信息。當伺服器崩潰時,如果收到一條RPC消息,伺服器可以檢查這個RPC是否曾經被執行過,從而保證RPC的執行具有“正好一次”語義。

3.8 Describe the actions taken by a kernel to context-switch between processes.

一般來說,作業系統必須保存當前運行的進程的狀態並恢復預定要運行的進程的狀態。保存進程的狀態通常包括所有 CPU 寄存器的值,以及內存分配。上下文切換還必須執行許多特定於架構的操作,包括清除數據和指令緩存。

3.9 Including the initial parent process, how many processes are created by the program shown in Figure 3.32?
#include <stdio.h>
#include <unistd.h>
int main()
{
int i;
for (i = 0; i < 4; i++)
fork();
return 0;
}

十六個進程已被創建。該程序包括printf()語句,以更好地解釋已經創建了多少個進程。

3.10 Using the program in Figure 3.33, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid t pid, pid1;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d",pid); /* A */
printf("child: pid1 = %d",pid1); /* B */
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d",pid); /* C */
printf("parent: pid1 = %d",pid1); /* D */
wait(NULL);
}
return 0;
}

Answer: A = 0, B = 2603, C = 2603, D = 2600

3.11 Give an example of a situation in which ordinary pipes are more suitable than named pipes and an example of a situation in which named pipes are more suitable than ordinary pipes.

簡單的通信可使用普通管道(ordinary pipes)實現。例如,假設我們有一個進程,該進程使用普通管道來計算文件中的字符數。生產者將文件寫入管道,而消費者則讀取文件並計算文件中的字符數。對於一個更適合使用命名管道的示例,考慮以下情況:多個進程可能需要寫消息到一個日誌中。當一個進程需要將消息寫入日誌時,它將消息寫入命名管道。一個服務器從命名管道中讀取消息並將其寫入到日誌文件中。

3.12 Using the program shown in Figure 3.34, explain what the output will be at lines X and Y.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] = {0,1,2,3,4};
int main()
{
int i;
pid t pid;
pid = fork();
if (pid == 0) {
for (i = 0; i < SIZE; i++) {
nums[i] *= -i;
printf("CHILD: %d ",nums[i]); /* LINE X */
}
}
else if (pid > 0) {
wait(NULL);
for (i = 0; i < SIZE; i++)
printf("PARENT: %d ",nums[i]); /* LINE Y */
}
return 0;
}

因為子進程是父進程的副本,子進程所做的任何更改都會發生在它的數據副本中,不會反映在父進程中。因此,在X行輸出的值為0、-1、-4、-9、-16。在Y行輸出的值為0、1、2、3、4。

3.13 What are the benefits and the disadvantages of each of the following? Consider both the system level and the programmer level.
a. Synchronous and asynchronous communication
b. Automatic and explicit buffering
c. Send by copy and send by reference
d. Fixed-sized and variable-sized messages

a. 同步和異步通信 - 同步通信的好處是它允許發送方和接收方之間的會合。阻塞發送的缺點是可能不需要會合,消息可以異步傳遞。因此,消息傳遞系統通常提供同步和異步兩種同步形式。

b. 自動緩衝和顯式緩衝 - 自動緩衝提供一個具有無限長度的隊列,從而確保發送方永遠不必在等待複製消息時阻塞。關於自動緩衝的提供方式並無具體規定;其中一種方案可能會保留足夠大的內存,其中大部分內存被浪費。明確緩衝指定緩衝區的大小。在這種情況下,發送方可能會在等待隊列中有可用空間時阻塞。但是,明確緩衝的情況下較不浪費內存。

c. 按拷貝發送和按引用發送 - 按拷貝發送不允許接收方更改參數的狀態;按引用發送允許這樣做。按引用發送的好處是它允許程序員編寫分散版本的集中應用程序。Java的RMI提供了兩種方式;但是,通過引用發送參數需要將該參數聲明為遠程對象。

d. 固定大小和可變大小的消息 - 這方面的影響主要涉及緩衝問題;使用固定大小消息,具有特定大小的緩衝區可以容納已知數量的消息。這種緩衝區能容納多少個可變大小消息是未知的。考慮Windows 2000如何處理這種情況:對於固定大小消息(小於 256 字節),消息從發送方的地址空間複製到接收進程的地址空間。較大的消息(即可變大小消息)使用共享內存來傳遞消息。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值