一個 CPU core (處理器核心)可以同時執行幾個 thread?

 

一個 CPU core (處理器核心)在同一個時刻只能執行一個 thread(執行緒或線程)但是在不同時刻(分時多工)可以執行多個 thread,這是透過 CPU 在不同的 thread 之間進行快速切換,使得在多個 thread 之間共享 CPU 資源,從而實現並行處理。這種快速切換的技術稱為上下文切換(context switching),由作業系統負責控制。當一個 thread 被暫停時或未執行完前,CPU 可以切換到另一個 thread 上,進行執行。由於切換 thread 需要額外的 CPU 運算資源,因此在同一個 core 上執行多個 thread 會帶來額外的開銷(overhead ),對效能會有一定的影響。但是,當多個 thread 需要共享資源時,同一個 core 上執行多個 thread 可以提高系統的效率。

A CPU core can only execute one thread at a time, but it can execute multiple threads at different times through fast switching between threads, which enables sharing of CPU resources among multiple threads and achieves parallel processing. This rapid switching technology is called context switching, which is controlled by the operating system. When one thread is paused, the CPU switches to another thread for execution. Since switching threads requires additional CPU computing resources, running multiple threads on the same core will bring additional overhead and have a certain impact on performance. However, when multiple threads need to share resources, running multiple threads on the same core can improve system efficiency.

 

現代大多數處理器都擁有多個核心,每個核心可以通過“多執行緒(multi-threading)”技術同時運行多個線程。

有兩種多執行緒技術:硬體(hardware-based)多執行緒和軟體(software-based)多執行緒。硬體多執行緒常被稱為“超線程”(Hyper-Threading,HT),這是由英特爾開發的一項技術。它允許單個物理核心(single physical core)通過在物理核心內創建虛擬核心(virtual cores)來同時運行多個線程。每個虛擬核心(virtual core)可以處理自己的指令流(instruction stream),從而使處理器能夠在一定的時間內執行更多指令。

另一方面,軟體多執行緒是通過編程技術(如多線程庫或並行處理框架)來實現,可以將任務分解為較小的子任務,這些子任務可以在多個核心或線程上同時執行。

現代處理器可以具有多個核心,每個核心可以通過硬件多緒程(超線程)或軟件多緒程技術同時運行多個線程。

 

Most modern processors have multiple cores, and each core is capable of running multiple threads simultaneously through a technology called "multi-threading."

There are two types of multi-threading: hardware-based and software-based. Hardware-based multi-threading is often referred to as "Hyper-Threading" (HT), which is a technology developed by Intel. It allows a single physical core to run multiple threads simultaneously by creating virtual cores within the physical core. Each virtual core can handle its own instruction stream, allowing the processor to execute more instructions in a given amount of time.

Software-based multi-threading, on the other hand, is achieved through programming techniques such as multi-threaded libraries or parallel processing frameworks. This allows a program to break down a task into smaller sub-tasks that can be executed simultaneously on multiple cores or threads.

In summary, modern processors can have multiple cores, and each core can run multiple threads simultaneously through hardware-based (Hyper-Threading) or software-based (multi-threading) techniques.

 

 

 

Thread是什麼?

 

在作業系統中,一個thread是一個執行單元,代表進程中的單個控制流。 它是一個輕量級、獨立的執行路徑,可以與Process中的其他thread並行運行。 每個thread都有自己的program計數器(counter)、堆棧(stack)和其他資源。

thread可視為輕量(ligthweight)process;thread是program去使用CPU的最基本、最小執行單位。

thread-concept-周志遠教授.PNG

thread是program將自身劃分為兩個或多個並發任務的一種方式。 作業系統單獨調度每個thread,因此每個thread獨立於其他thread運行。 這允許多個thread在單個處理器上同時運行,從而可以提高Program的性能和響應速度。

thread可以通過共享內存(shared memory)、信號量(semaphores)或其他方式相互通信。 它們還可以共享相同的文件描述符(file descriptor),例如在讀取文件時,但仍具有單獨的讀/寫指針(pointers)。

thread是作業系統的重要組成部分,因為它們用於通過允許同時執行多個任務(multiple tasks)來提高性能。 例如,Web 服務器可以使用thread並行處理多個客戶端請求,而不是按順序處理每個請求,這會導致延遲和性能不佳。

thread也用於並行編程(parallel programming),其目標是使用多個處理器同時(simultaneously)執行多個任務。 與順序編程(sequential programming)相比,這可以顯著提高性能,在sequential programming中任務一個接一個地執行。

 

thread vs process 有何不同?

thread是存在於進程中的輕量級獨立執行單元。 process是包含一個或多個thread的重量級獨立執行單元。 每個process都有自己的內存空間和系統資源,而同一個process內所有的threads共享內存空間和資源。 這意味著process中的thread可以比process之間更容易地共享數據和相互通信。 此外,thread有時被稱為輕量級process,它們不需要自己獨立的內存空間。

 

同一process中的線程共享相同的內存空間,這意味著它們可以輕鬆地共享數據並通過讀取和寫入相同的內存位置來相互通信。 它們還共享相同的文件描述符表(file descriptor table),這意味著它們對打開的文件(open files)、套接字(sockets)和其他資源具有相同的訪問權限。 它們還共享相同的系統資源,例如 CPU 時間和網絡帶寬。 此外,它們共享相同的環境變數(environment variables)、信號處理程序(signal handlers)和當前工作目錄(working directory)。

 

thread線程還共享相同的process heap 記憶體,這是process用於動態內存分配的內存區域。 這意味著它們可以在heap上分配和釋放內存,並從同一process中的其他thread訪問動態分配的內部記憶體。 其中Stack及Heap的空間是動態變動的,其餘data(gloabal variables)及text(code)的大小是固定的。

process-concept-2-周志遠教授.PNG

值得注意的是,thread在共享這些資源(Code section, data section及OS的資源)的同時,也有自己的stack,這是一塊內存區域,用於存放函數調用幀(function call frames)、局部變量等臨時數據(local variables),這個stack是每個thread專有的。

一個parent process下的所有threads共享內部記憶體(Code section, data section及OS的資源),但每一個thread會有自己的thread ID, program counter, register set 及Stack。

thread-concept-周志遠教授.PNG

 

program、process、thread有何差別?

program、process、thread都是計算機科學中的相關概念,但指的是不同的東西。

program是計算機可以執行以執行特定任務的一組指令。 一個program可以用多種“編程”語言編寫,如C、C++、Java、Python等,並可以存儲在計算機的硬盤或其他存儲設備上。

process是正在運行的program的實例(instance)。 當一個program被執行時,操作系統會創建一個新的process來運行這個program。 每個process都有自己的內存空間(memory space),獨立於其他process運行。 一個process可以有多個thread。

thread是process中的執行單元。 它是一個輕量級、獨立的執行路徑,可以與process中的其他thread並行運行。同一個process內的所有的threads共享相同的內存空間(memory space),但不同thread有自己的program計數器(counter)、堆棧(stack)和其他資源。

總之, program是計算機可以執行以執行特定任務的一組指令。process是一個正在運行的program的實例,它有自己的內存空間,獨立於其他process運行。thread是process中的一個執行單元,它是一個輕量級的、獨立的執行路徑,可以與process中的其他thread並行運行。
通過在一個process中創建多個thread,一個program可以同時執行多個任務,這就是所謂的多thread。 這可以提高program的性能和響應能力。

 

舉例說明,假設您有一個名為“照片編輯器(Photo Editor)”的program,允許用戶編輯和增強照片。 當用戶啟動“照片編輯器”program時,作業系統會創建一個新的process來運行該program。 process有自己的內存空間,獨立於其他process運行。

現在,假設“照片編輯器”program具有允許用戶同時對照片應用多個濾鏡的功能。 為此,program在process中創建了多個thread。 每個thread負責對照片應用特定的濾鏡。 作業系統分別調度每個thread,因此它們可以相互並行運行。 這允許program同時對照片應用多個濾鏡,從而提高program的性能和響應能力。

在這個例子中,“照片編輯器”是program,正在運行program的instance 是process,thread是process中的執行單元,可以與其他thread並行運行,以提高program的性能和響應能力。

總而言之,program是一組可以由計算機執行的指令,process是正在運行的program的實例(instance),thread是process中的執行單元 可以與其他thread並行運行以提高性能。

 

 

 

各大銀行海外匯款(匯入及匯出)手續費及電報費比較表

黃大偉理財研究室 市場利率、存股、儲蓄險的專家,按讚收到更多好文章

 

 

 

 

 

 

 

 

 
arrow
arrow
    全站熱搜

    黃大偉 發表在 痞客邦 留言(0) 人氣()