東南大學《操作系統》課程作業 - 第六章


1 - The first known correct software solution to the critical-section problem for two processes was developed by Dekker. The two processes, P0 and P1, share the following variables:

boolean flag[2]; /* initially false */
int turn;

The structure of process Pi (i == 0 or 1) is shown in the following Figure. The other process is Pj (j == 1 or 0). Prove that the algorithm satisfies all three requirements for the critical-section problem.

while (true) {
  flag[i] = true;
  while (flag[j]) {
  if (turn == j) {
    flag[i] = false;
  while (turn == j)
    ; /* do nothing */
  flag[i] = true;
  }
}
/* critical section */
  turn = j;
  flag[i] = false;
/* remainder section */
}

該算法滿足三條要求。
(1)互斥。Pi在進入臨界區前,會等待直到turn為自己。Pi在退出臨界區后,會將turn交還給Pj。這樣,自己的flag只會在turn為自己時才會成功修改,實現了互斥。
(2)進步。Pi退出臨界區后,會將turn交還給Pj。如果Pi要再次進入臨界區,必須等Pj釋放turn。這樣便實現了進步,選擇Pj進入臨界區不會無限推遲。
(3)有限等待。該算法每次進入臨界區的進程由turn決定,而turn會在每次退出臨界區時發生交還,因此Pi與Pj會輪流獲得turn的許可,不會發生無限等待。

2 - Explain why implementing synchronization primitives by disabling interrupts is not appropriate in a single-processor system if the synchronization primitives are to be used in user-level programs.
如果用戶級程序可以禁用中斷,則它可以關閉用於指示上下文切換的中斷,造成上下文切換不發生,從而無法進行處理器上的進程切換。

3 - Explain why interrupts are not appropriate for implementing synchronization primitives in multiprocessor systems.
利用中斷在多處理器系統上實現同步原語是不合適的。需要同步的進程只能關閉在當前處理器上的中斷(關閉所有處理器的中斷很困難,也會嚴重影響性能),使得在當前處理器上運行的其他進程不會與該進程發生同步問題,但無法保證在其他處理器上運行的進程與該進程保持互斥。

4 - Servers can be designed to limit the number of open connections. For example, a server may wish to have only N socket connections at any point in time. As soon as N connections are made, the server will not accept another incoming connection until an existing connection is released. Explain how semaphores can be used by a server to limit the number of concurrent connections.
定義信號量,初始化為N。每當有新客戶端接入時,調用wait占據一個信號量,如果當前信號量數量小於等於0,則不斷等待;每當有客戶端退出時,調用signal釋放一個信號量。

5 - Show that, if the wait() and signal() semaphore operations are not executed atomically, then mutual exclusion may be violated.
以wait為例,如果它的執行不是原子性的,假設當前信號量為1,兩個進程可能同時發現這一情況,並將信號量減1,認為自己申請到了信號量。這將破壞互斥性。

6 - The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.
可以用信號量方法描述該問題。對於顧客,有兩個資源,一是n把chairs,二是1位barber。對於理發師,有一個資源,即等待理發的顧客,當沒有這個資源時,理發師睡眠。另外,對於臨界區資源people的修改必須是互斥的。

semaphore barber = 1; // 理發師信號量
semaphore chair = 0; // 已用的等待椅信號量
semaphore mutex = 1; // 互斥信號量
int people = 0; // 數據,等待理發的顧客數
// 理發師進程
void barber() {
  while (true) {
    wait(chair); // 理發師等待顧客,沒有顧客就睡覺
    wait(mutex); // 准備服務顧客,進入臨界區
    people -= 1; // 修改臨界區數據
    signal(mutex); // 釋放
    /* 進行理發 */
    signal(barber); // 理發完了
  }
}
// 顧客進程
void customer() {
  wait(mutex); // 進入臨界區
  if (people < n) {
    people += 1; // 還有空位,就坐進去
    signal(mutex); // 完成臨界區數據修改
    signal(chair); // 如果理發師在睡覺,就叫醒他
    wait(barber); // 等待理發師資源(可能正在給別人理發)
    /* 進行理發 */
  } else {
    signal(mutex); // 沒有空位,不能進店
  } 
}

7 - Write a bounded-buffer monitor in which the buffers (portions) are embedded within the monitor itself.
我們用一個簡單的生產者-消費者模型作為管程提供的功能。

monitor BoundedBuffer {
  DataType buffer[BUF_LEN];
  int pointer = 0;
  condition x, y;
  void produce(DataType val) {
    while (pointer == BUF_LEN) x.wait(); // 緩沖區滿,掛起x
    buffer[pointer++] = val;
    y.signal(); // 喚醒y
  }
  DataType consume() {
    while (pointer == 0) y.wait(); // 緩沖區空,掛起y
    DataType val = buffer[--pointer];
    x.signal(); // 喚醒x
    return val;
  }
}

8 - How does the signal() operation associated with monitors differ from the corresponding operation defined for semaphores?
管程的signal調用時,如果沒有掛起進程,就沒有作用;信號量的signal調用時,總會增加該信號量的值,供后續進程使用。

9 - Consider a system consisting of processes P1, P2, ..., Pn, each of which has a unique priority number. Write a monitor that allocates three identical line printers to these processes, using the priority numbers for deciding the order of allocation.

monitor ThreePrinters {
  int nAvailablePrinter = 3;
  Process queuedProccess[PROC_COUNT]; // Process: {pid:int, priority: int}
  int nQueuedProcess = 0;
  condition x;

  void acquirePrinter(Process process) {
    if (nAvailablePrinter > 0) {
      nAvailablePrinter -= 1;
      return; // 如果有空余打印機,直接分配
    }
    // 否則進入等待隊列,按優先級分配
    queuedProcess[nQueuedProcess++] = process;
    sort(queuedProcess, /* 優先級降序排列 */);
    while (nAvailablePrinter == 0 || queuedProcess[0] != process) {
      x.wait(); // 沒有空余打印機,或者有但是還沒輪到自己,就掛起
    }
    // 將隊首出隊,分配打印機
    queuedProcess[0] = queuedProcess[nQueuedProcess – 1];
    nQueuedProcess -= 1;
    nAvailablePrinter -= 1;
    // 更新隊列
    sort(queuedProcess, /* 優先級降序排列 */);
  }

  void releasePrinter() {
    nAvailablePrinter += 1;
    x.signal(); // 喚醒x
  }
}


免責聲明!

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



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