Wildsky F.

Easy things should be easy, and hard things should be possible.



作業系統 Ch3 – Process (上)

Posted on

因為篇幅有點長,所以這一章節我分成上下兩篇文來寫。本篇會包含這些:

  • Process vs Program
  • Thread
  • Process State
  • Process Control Block
  • Context Switch

Process vs Program

什麼是 Process?和 Program 差在哪?

  • Program = passive entity – 只是存在 disk 的 binary 檔
  • Process = active entity – 存在 memory 中、正在執行的 program

簡單地說,差別就是在是否「正在被執行」。

一個 Process 包含了:

  1. code segment (text section) => 不是一般的 code,是 instruction
  2. data section => 這裡放 global variable
  3. stack => 這裡放 local variable,因為用完可以直接 pop 掉
  4. heap => 動態分配記憶體空間給變數或 class 時會用到的
  5. current activity (program counter, register counter)
  6. 相關資源的 set(e.g., open file handlers)

2,3,4 就是寫程式時用到的變數,

一個 process 可用的 memory 是有限的,所以如果遞迴 call 太多層或是 allocate 太多記憶體,都會跟你說 out of memory,因為 memory 的使用量是有上限的。

Thread

  • thread 也稱為 lightweight process
  • 內容物與 process 相同
  • 不同之處為,多個 thread 間可以共用某一塊 memory
  • 因為 thread 的 parent process 在 create thread 時就會做空間的管理
    → 所以就省掉了部分空間管理的程式處理
    → 所以是 lightweight
  • thread 是使用 CPU 的最小單位
  • 同一個 process 下的 thread 可以共用:
    • code section
    • data section
    • OS resources (open files & signals)
  • 但下面這些是各自獨立:
    • thread ID
    • program counter
    • register set: 因為 thread 在執行 instruction 時的位置可能不同,執行的狀態也是獨立的
    • stack: 各自只摸得到自己的 local variable

之後的章節會再講到更細節,這邊只簡單介紹 Thread 是什麼。

Process State

介紹 process 在 OS 中的 life cycle,共有五個狀態

1- New:

  • 把 program load 進記憶體,並在記憶體中把剛剛講的各個 section 都 initialize
  • admit: OS 在此時會確認有沒有足夠的資源去 create process,也會看你有沒有權限去執行該 process,都 OK 才會進去 ready

2- Ready:

OS 是透過 queue 來管理 CPU 資源的競爭,而 process 在 queue 中等著被 CPU 處理的過程就屬於這個狀態。

3- Running:

  • 進去 CPU 中,正在被執行的狀態
  • 有可能跑著跑著就被 OS interrupt 回 ready 狀態,因為 OS 要確保他有掌控權,所以會時間一到,就把 CPU switch 給 scheduler

4- Waiting:

  • 在進行非 CPU 處理時(e.g., I/O),就會進入這個狀態
  • 等到處理完了,就會回到 ready 等著被 CPU 執行
  • 雖然在 memory 但是還沒有到能立即被 CPU 執行的狀態

Process State 的流程圖

Process Control Block

在 memory 中,用來記錄剛剛那些資訊的 Table。

當 process 被 create 時,OS 就會幫他建一個 table 叫做 PCB,裡面存著一些 process 在執行中的狀態的相關資訊,比較重要的是:

  1. 指到 queue 的下一個 PCB 的 pointer:當我們說「把 process 放進 queue」時,其實是把 PCB 放進去,而這邊的資料結構是 Linked list,就需要 pointer 來指到 queue 中的下一個 PCB
  2. process state: 記錄 process 的當前狀態
  3. Program counter
  4. CPU register

這些東西是放在 memory,而且是 kernel space 的地方,由 OS 進行處理。

其他還有

  1. CPU scheduling information (e.g., priority)
  2. memory management information (base & limit register): 只有 process 在執行時才會把這兩個值從 memory load 進 CPU 的 register。
  3. I/O status/information
  4. accounting information

Context Switch

定義:把 CPU 上正在執行的 process,switch 成另外一個 process 的動作就叫做 context switch。

CPU 中的程式在切換的流程如下:

  1. process p0 正在 CPU 中被執行
  2. 出現一個 interrupt,p0 就要準備進入 idle
  3. 進行 Context Switch:
    1. 把 CPU 中 p0 的狀態 load 進 PCB0
    2. load PCB1(即將被切換到的 process1 的 PCB)
    3. context switch 完成
  4. 開始執行 p1
    • context switch 是 overhead(因為在 switch 時,其實是在浪費 CPU)
    • 但因為很常需要 switch,所以我們能做的只有盡可能地縮短時間,而這與下列這些事情相關:
      • memory 的速度
      • register 的數量(越少的話需要存取的數量就少)
      • 合併 load & save PCB 的 instruction,讓他一次做完
      • 硬體支援:提供多組 set 的 register,讓他不用在 register 和 memory 之間切換

關於 Process 的後續講解,請見 Process(下) 這篇文章

Ref