【作業系統】Multithreaded Programming Part 2

朱痕染跡璧有瑕

--

https://pixabay.com/photos/farbenspiel-pattern-decor-design-4682289/

本文為清華大學開放式課程上課心得整理。

Thread 屬於 shared-memory programming,執行速度會比較快,當然也有同步化、死結與快取一致性(cache coherence)的問題要解決,後面會有專章介紹。

shared-memory programming 通常會用以下幾種技術實現:

・Parallelizing compiler:透過相關函式庫執行 shared memory,這是最方便的做法。

・Unix Processes:用 Processes 執行 Thread 的功能,即時創建 segment,將資料共享。

・Threads(Pthread,Java)

Pthread

早期作業系統在開發時,為了能讓相同的程式盡量多適應一些作業系統,因此訂立了標準編譯程序,只要遵循這些規定開發,程式便可以在不修改的狀況下安裝在同一個編譯標準的作業系統上。以 UNIX-like 作業系統來說,它遵循的標準稱為 POSIX,而透這套標準撰寫而成的 thread 就稱為 Pthread。

Pthread 的構造非常簡單,除了 create、terminate 以及一些溝通用的 function call 之外,其他的內容都由程式設計者自己決定。

https://www.youtube.com/watch?v=rmK8gwazWqc&list=PL9jciz8qz_zyO55qECi2PD3k6lgxluYEV&index=30,12 分 31 秒截圖

pthread_create 參數的意義分別為:

・&thread1:return 值,是一個 pointer,指向 thread,用來標示與哪個 thread 進行互動。

・NULL:attribute,用來控制 thread 的行為。

・func1:function pointer,用來標示要執行的 function。

・&arg:function 的參數,為了保持簡潔,參數數量限制為 1,如果需要多個參數,要先將參數包裝好再傳入。

Pthread Joining & Detaching

pthread_create 會 return 一個 pointer,因此需要呼叫 pthread_join 來取得 pointer 的值。如果在 create thread 之後不需讀取資料,作業系統通常會要求程式設計者呼叫 pthread_detach,告訴系統在 thread 結束之後可以直接釋出 thread 所佔用的資源。

同上,18 分 41 秒截圖

Java Threads

Java Thread 本身即有其函示庫。由於 Java 本身有自己的虛擬機設定,因此與各種不同作業系統之間的相性都比較好。

同上,22 分 52 秒截圖

Linux Threads

Linux Threads 指的是 kernel 管理 thread 的方法。Linux Threads 不支援多線程(換句話說就是 process),但是 programmer 會有創造 thread、共享資源的需求,為了有別於 process 的 fork,Linux 便另外創造 clone 方法(system call),讓使用者可以控制要分享哪些資訊,用來模擬 thread 的行為。

Thread 與 Process 的區別

雖然 Thread 與 Process 非常相似,但在某些行為上還是有所不同,需要多加注意。

Semantics of fork() and exec()

process 的 fork 方法會複製一隻 process 出來,但是當 process 中含有複數 thread 時,會產生兩種可能性:第一個是將所有的 threads 完整複製,另外一種則是只複製呼叫 fork 的 thread。

至於執行則是系統設計,系統可以支援上述兩種作法,或是只提供其中一種,端看設計者的規劃。

https://www.youtube.com/watch?v=rAaZcgCn41s&list=PL9jciz8qz_zyO55qECi2PD3k6lgxluYEV&index=33,2 分 21 秒截圖

Thread Cancellation

當 thread 的執行被取消時,會回到 main thread,此時 main thread 會收回由 thread 使用的資源,這裡通常有兩種實作方式:

・Asynchronous Cancellation:main thread 執行到某個階段才執行清除

・Deferred Cancellation(default option):target thread 預設只有某幾個點可以被中斷,過程中即使被取消,也會執行到指定的點才停止。

Signal Handling

由於一個 process 會有很多 threads,因此在接收到訊號時,作業系統必須先判斷這個信號是給單一 thread 還是給所有的 threads,這裡也存在幾種可能性:

・只有創造訊號的 thread 收到返回訊息

・Process 的所有 threads 都會收到訊息

・將信號傳遞給指定的 thread

・由 main thread 處理訊息

同上,8 分 37 秒截圖

Thread Pools

thread 的觀念是當成是需要平行執行任務時創造,使用完畢之後刪除,因此很有可能會頻繁的重複創造與刪除,Thread Pool 的觀念就是一開始便創造很多 threads,讓程式在執行過程中重複使用。

Thread Pool 的好處是不需要一直即時創造 thread,可以加速處理請求;此外,由於 Pool 的大小是可以控制的,因此可以控管資源的使用量,避免 thread 佔用太多資源而作業系統指令無法順暢執行。

同上,13 分 02 秒截圖

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response