OS 筆記 - Ch5 Multithreaded Programming

Ch5 Multithreaded Programming 的整理筆記

  • Introduction
  • Benefits of Multithreading
  • Multithcore Programming
  • User vs. Kernel Threads
  • Multithreading Models
  • Thread libraries
  • OS examples

Introduction

  • 又稱 lightweight process
  • basic unit of CPU utilization
  • 分配 CPU Time 的單位
    • Process 是 分配 Resource 的單位
  • All threads belonging to the same process share
    • Code section
    • Data section
    • OS resources (e.g. open files and signals)
  • 但 擁有自己的 thread control block
    • thread ID
    • program counter
    • register set
    • stack

Single and Multithreaded Processes


  • example : Web Server, RPC server

Multithreaded Server Architecture

Benefits of Multithreading

  • Responsiveness : 即時反應

    • allow a program to continue running even if part of it is blocked or is performing a lengthy operation
  • Resource sharing : 共用 the same address space

    • 用 global variable 溝通 ( code section )
  • Utilization of MP arch. : 多核可平行

    • 又稱 Scalability
  • Economy :

    • Process 的 溝通 cost 要透過 OS,cost 高
    • Create thread 也比 process 快 10幾倍以上,Context switch 也是幾倍
  • (補充) 不適用的情況 : 一個時間只有一個工作可以執行

    • 可能會改用 Symmetric Multi-Threading : 在 Processor 上創造多個 Logic processor 達成平行(SMP)

Multithcore Programming

  • 提供 mechanism for more efficient use of multiple cores and improved concurrency
  • Hardware 已經很進步了,所以再來就是 programmer 的工作。
    • Scheduling algorithms use cores to allow the parallel execution

Challenges in Multicore Programming

  • Dividing activities : 把事情切開來
  • Data splitting : data 也要切

真正難的 :

  • Data dependency : 執行的先後順序是否會造成運算結果錯誤
  • Balance : 執行時間也要都差不多,這件事又很難事先預測
  • Testing and debugging

User vs. Kernel Threads

  • User threads – user-level threads library
  • Kernel threads – kernel (OS) 內建
  • 兩者並無直接關係,
    可能 某個process C.S. 走了之後 kernel thread 沒事做,就有其他 user thread 可以去綁它。

User threads

  • Thread library 提供 create, scheduling, deletion
  • 通常比較快 (沒有 touch syscall)
  • ☆ 如果很多 user thread 都綁到同一個 kernel thread
    其中一個 user thread 把自己 blocked (I/O之類的),kernel thread 也會跟著被 blocked
    User 以為自己 create 一堆 ... 但是是假象

Kernel threads

  • 由 kernel 來 create 和維護 → 所以比較慢
  • kernel thread 之間互不影響

Multithreading Models

  • Many-to-One : 好處是可以共享 global var.
    • Thread management is done in user space, so it is efficient
    • But the entire process will block if a thread makes a blocking system call.
    • Multiple thread 實際上並不能 parallel
  • One-to-One : 最常見,為了方便 implement,反正現在資源多,不怕
    • 不過通常會設一個開的數量上限
  • Many-to-Many : 彈性處理,可能多個輕的給一個 kernel 管
    • run time 做 mapping


Thread libraries

Pthread

  • POSIX (Potable Operating System Interface)
    • standard is specified for portability across Unix-like systems
  • Pthread is the implementation of POSIX standard for thread

  • pthread_join() : 可以拿來接在 thread 內得到的 retrun value
  • 如果不用回傳值,的確是不用 call join;但建議要 call pthread_detach( tid )
    • 這樣就能直接 free thread 的 resource

Java Thread

  • Java threads are implemented using a thread library on the host system
  • Java 本身就是一個 layer,JVM 會做控制
  • Thread mapping depend on implement of JVM
    • Windows 98/NT : one-on-one model
    • Solaris 2 : many-to-many model

OS examples

Linux Threads

  • Linux does not support multithreading
    • 某種程度來說是這樣。你 create 一個 thread 他仍然把它當作 process 看待
  • Pthreads 是在 user-level
  • fork() : create a new process and a copy of the associated data of the parent process
  • clone() : 也是 create,但你可以用參數控制哪些不要 share
    • 因為現在有一些 process 在 share memory,但不見得要 share 給 child.
    • None of the flags is set → clone = fork
    • All flags are set → parent and child share everything


Threading Issues

  • Implicit Threading (名詞介紹) : 由 compolers 和 run-time libraries 負責管理與新增執行緒,並非由程式負責。(主要就是要說 : 讓 programmer 輕鬆一點)

    • Thread Pools : 後面會提到
    • OpenMP : 由compiler處理API,再給C、C++等語言使用
    • Grand Central Dispatch : 麥金塔系列 OS 的處理方式
      • Compiler 會分割在 block 內的 data 到 dispatch queue 中
  • **Semantics of fork() and exec() system calls. Duplicate all the threads or not ? **

    • linux 比較偏向最右邊
    • 沒有誰對誰錯,看 OS 怎麼支援
  • execlp() works the same; replace the entire process

  • Thread cancellation : thread 被 terminate 掉,之後該做什麼 ?
    • Asynchronous cancellation : 某些特定地方、時間點才會做 clean up
      • 容易 implement
    • Deferred cancellation (default option) :
      • target thread 本來就有預設幾個 Cancellation points 才能被中斷
        即使你中途按 Ctrl + C 也是要執行到那邊
    • 結論來說,不管用哪種都不會立刻被 cancel 掉
      • overhead 很大,或是有些指令還沒執行完,可能會有同步問題
  • Signal Handling : notify a process that an event has occurred
    • Synchronous type : 自作自受,e.g. 除以0
    • Asynchronous type : 被他人給砍掉,e.g. Ctrl + C, Time out
    • 是給特定 thread 還是給 所有 thread ? 會分不同情況
      • 給自己 e.g. sleep
      • 給所有
    • 給透過 thread id 指定的
      • main thread by default
  • **Thread Pools **
    • 動態 create, delete 會慢,乾脆直接開一堆
    • 優點 :
      • faster to service
      • 可以 control pool size ( = control degree )
  • (補充) Windows XP Threads
    • one-to-one mapping
      • Also provide support for a fiber library, that provides the functionality of the many-to-many model
    • Each thread contains
      • A thread ID
      • Register set
      • Separate user and kernel stacks
      • Private data storage area

Review

P.17

  • Benefit of multithreading ?
    • Responsive, Economy, resource utilization, resource sharing
  • Challenges of multithreading programming ?
  • User threads & kernel threads ? Differences ?
  • Threading model ?
    • Many-to-one
    • One-to-one
    • Many-to-many

參考

Mage 大的筆記、周志遠教授 PPT 與影片


END

-----------------------------------

2020.01.05