首頁  >  文章  >  系統教程  >  執行緒與進程的區別:進程是分配資源的基本單位,執行緒是獨立運行的控制流

執行緒與進程的區別:進程是分配資源的基本單位,執行緒是獨立運行的控制流

王林
王林原創
2024-07-23 10:57:541014瀏覽

執行緒與進程的區別:進程是分配資源的基本單位,執行緒是獨立運行的控制流

執行緒:是進程中某個單一順序的控制流統稱為lightweightprocesses)

線程是操作系統才能進行運算調度的最小單位,她被包含在進程之中,是進程中的實際操作單位,一條線程指的是進程中一個單一次序的控制流,一個進程中可以並發多個線程,線程可以作業系統內核調度的內核線程。

同一個進程的多條執行緒將共享該進程中的全部系統資源,如虛擬位址空間,檔案描述符和訊號處理等等

然而同一進程中的多個執行緒有各自的呼叫棧,自己的暫存器環境,自己的執行緒本地儲存。

執行緒與行程的差別在:一般一個行程可以包含多個執行緒linux行程與執行緒核心,她們可以藉助行程所擁有的資源,作業系統中,一般把行程當作分配資源的基本單位,把執行緒作為獨立運作和獨立調度的基本單位,遲疑執行緒比進程更小,基本上不用有系統資源。

一個進程起碼須要一個執行緒作為它的指令執行體,進程管理著資源(例如CPU,顯存,檔案描述子等),執行緒即將分配到CPU上執行。

執行緒模型分為,核心級執行緒和使用者級執行緒兩種執行緒模型,分類的標準主要是執行緒的調度者是在核心內還是在外,後者更有利於並發使用多處理器的資源,前者更多考慮的是上下文切換開支。

内核进程和内核线程_linux进程与线程 内核_linux内核线程和用户线程

linux核心只提供了輕量進程的支持,限制了更有效率的執行緒模型的實作linux進程與執行緒 核心,並且linux注重

雖然linux支援輕量級進程,但並不能說,他就支援核心級進程,由於linux的執行緒和進程實際上處於一個調度層次,共享一個進程標示符空間,這些限制不可能在linux下實現完全意義上的POSIX執行緒機制。

linux核心並不支援真正意義上的線程,linuxthreads使用和普通進程具有同樣內核調度視圖的輕量級進程來實現線程支援的。這種輕量級進程擁有獨立的進程ID,在進程調度,訊號處理,IO等方面享有與普通進程一樣的能力。

linux下的執行緒就是輕量級進程。

每位linuxthread執行緒​​都同時具有執行緒id和進程id,其中進程id就是核心所維護的進程號

,而線程id則由linuxthreads分配和維護

内核进程和内核线程_linux进程与线程 内核_linux内核线程和用户线程

__pthread_initial_thread的線程id為PTHREAD_THREADS_MAX,__pthread_manager_thread的是2*PTHREAD_THREADS_MAX+1linux格式化指令,第一個使用者執行緒的執行緒id為PTHREAD_THREADS_MAX+1linux格式化指令,第一個使用者執行緒的執行緒為PTHREAD_THREADS_MAX+2,接著第n個公式

tid=n*PTHREAD_THREADS_MAX+n+1

These allocation forms ensure that all threads in the process (including those that have already exited) will not have the same thread id, and the type of thread id pthread_t is defined as unsigned long int (unsignedlongint), which also ensures reasonable running time Internal thread IDs will not be repeated.

Searching for the thread data structure from the thread id is completed in the pthread_handle() function. In fact, it is just the thread number modulo PTHREAD_THREADS_MAX, and the result is the index of the thread in __pthread_handles.

5. Creation of threads

After pthread_create() sends a REQ_CREATE request to the management thread, the management thread calls pthread_handle_create() to create a new thread. After allocating the stack and setting the thread attribute, use pthread_start_thread() as the function entry and call __clone() to create and start a new thread. pthread_start_thread() reads its own process ID number and stores it in the thread description structure, and configures scheduling according to the scheduling method recorded therein. After everything is ready, call the real thread execution function, and call pthread_exit() to clear the scene after this function returns.

Three ways to check the number of threads under Linux:

1.cat/proc/pid/status

2.pstree-ppid

3.top-H-ppid

4.psxH, view all existing threads

5.ps-mppid

linux进程与线程 内核_linux内核线程和用户线程_内核进程和内核线程

6.ps-eLf|grep

The second column of the command query result is PID, the third column is PPID, the fourth column is LWP, and the sixth column is NLWP.

jstack30420|less, then search for nid=0x44bf, oh, found it

Shell code

"main"prio=10tid=0x11400nid=0x44bfrunnable[0x0000000040f5c000..0x0000000040f5ced0]

java.lang.Thread.State:RUNNABLE

at.SocketInputStream.socketRead0(NativeMethod)

at.SocketInputStream.read(SocketInputStream.java:129)

at.SocketInputStream.read(SocketInputStream.java:182)

atcom.caucho.server.resin.Resin.waitForExit(Resin.java:524)

atcom.caucho.server.resin.Resin.main(Resin.java:614)

The jstack command has come to an end, let’s not study it for now

以上是執行緒與進程的區別:進程是分配資源的基本單位,執行緒是獨立運行的控制流的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn