[Operating System Cheat sheat] — Thread

Leon .
6 min readApr 4, 2021

--

  1. Thread
  2. Process v.s Thread
  3. user thread v.s kernel thread
  4. Multi-threading Model
  5. Multi-threading issue
  6. Pthread library example

Thread

又叫Lightweight Process ,是OS分配CPU Time的對象單位

Thread is basic unit of CPU utilization

Thread建立之後,其私有的(Private)內容組成含有

  1. Program Counter
  2. CPU register value
  3. Stack
  4. Thread ID
  5. Thread State

Note:
Record in Thread control block

Shared Portion

在同一個Process之下的thread們,會共享以下資源

  1. Code Section
  2. Data Section
    以上兩點合稱Memory Space
  3. Other O.S. Resource (e.g. open files…)

Thread Advantage

有 4 項比較八股的優點,如下 :

  1. Responsiveness
    當 Process內執行中的Thread被Blocked,則CPU可以切給此Process內其他 Available Thread 執行,故整個Process 不會被 Blocked,仍持續 going,所以若將 Multithreading 用在 User-Interactive Application,可增加對User之回應程度。
  2. Resource sharing
    因為 Process內之多條 Threads 共享此 Process Code Section,所以
    在同一個 MemorySpace上,可有多個工作同時執行
  3. Economy
    因為同一個Process內之不同Threads彼此共享此Process 的memory 及other OS resources,所以 Threads之私有成份量少,故Thread 之 Creation 、 Context Switching fork:…等 Thread Management
    Cost is cheap
  4. Scalability (Utilize of Multiprocessors Architecture)
    可以作到同一個Process內之不同Threads 可以在不同 CPUs上平行執行,可以增加對Multiprocessors System之平行程度提升。

Thread v.s Process

User Thread / Kernel Thread

如果我們將Thread進一步地分類,由Thread Management 的工作做區分

Thread creation, destroy suspend,wakeup ,scheduling, context switching
是由誰負責的角度去劃分,可以分為User Thread 以及 Kernel Thread.

User Thread

Thread Management 是由在 user site 之 Thread library 提供 APIs,
供 user process 呼叫使用。

Kernel 完全不知道 (is unaware of ) user thread 之存在

Note :
只知道有 process(single thread) Thread Management 不需 kernel介入干預

[優點]
Thread creation 、 context switching 等管理較快,成本較低。

[缺點]
1. 當process內某條執行中的user thread is blocked,會導致整個process亦被 blocked
(即使 process 内還有其他available Threads)

2. Multiprocessors架構效益發揮較差
(因kemel只看到一個process、無法作到 Process内多條user threads 平行執行)

3.例:舉凡Thread library 皆是 user threads
Ex : POSIX 的 PThread library 、Mach 的 C-Thread library 、Solaris 2 以上的UI Thread library 、 Green Thread library

Kernel Thread

  1. Thread 管理完全是由kernel負責,
    (kernel知道每一條Thread 的存在,並進行管理)
  2. 優缺點:與User Thread 相反
  3. 例:大部分OS皆支持: Windows系列(2000 NT)、 UNIXLinuxSolaris

Kernel Thread 與 User Thread有三種Model做對應 :

Thread Model

  1. Many-to-One Model
    his model maps many user Thread to one kernel Thread. Thread
    Management is done in user space
    (二)優缺點:同user-Thread
    (三) 例:Thread library 皆是
  2. One-to-One Model
    (ㅡ) Def: This model maps one user Thread to one kernel Thread. Thread
    Management is done in user space
    (二)優缺點:同kernel-Thread
    (三)例:現代個人電腦大都採用此 Model: Windows NT 、2000 、
    OS 、 “Linux”
  3. Many-to- Many Model
    (-) Def : This model maps many user Thread to a smaller or equal number of kernel Thread. Thread Management is done in user space
    (ニ)優點:User thread 的第1、2點,再加上3.負擔不若one-to-one重
    缺點 :User thread的第1點,再加上2.製作設計上較複雜
    (三)例:Solaris 2 以上(two level modeling)

--

--

Leon .
Leon .

Written by Leon .

Record my life experience and knowledge , keep enthiusastic attitude to explore the world , that’s what I live for.

No responses yet