Spooling and Device Reservation

Basic Concept

Spooling 和 device reservation 都是在處理一種 I/O 問題:有些 device 不適合讓多個 process 的 requests 交錯執行。Printer 是最典型例子。多個 process 可以同時想印東西,但 printer 一次只能印一份 job;如果 OS 把不同 process 的 bytes 交錯送到 printer,紙上內容會混在一起。

Spooling

Spool 是一種專門給 device output 使用的 buffer,通常放在 secondary storage。以 printer 為例:

  1. 多個 application 同時要求 print。
  2. OS 不直接把 bytes 混著送到 printer,而是攔截每個 print output。
  3. 每個 job 被寫成自己的 spool file。
  4. Spooling system 把 spool files 排成 queue。
  5. Printer 一次只處理一個 spool file。

這樣 application 可以很快把 print job 交給 OS,而 printer 仍然保持一次只印一份完整文件。

Device Reservation

有些 device 不能被有效 multiplex,例如 tape drive 或某些 printer。除了 spooling,OS 也可以提供 explicit device allocation:

  1. Process 要求 allocate idle device。
  2. OS 標記 device 被該 process 使用。
  3. 其他 process 必須等待或失敗。
  4. 使用完後 process deallocate device。

某些 OS 也用「同一時間只能有一個 open file handle」的方式達到類似效果