Rumah  >  Artikel  >  Tutorial sistem  >  Perbezaan antara utas dan proses: Proses ialah unit asas untuk memperuntukkan sumber dan utas ialah aliran kawalan yang dijalankan secara bebas.

Perbezaan antara utas dan proses: Proses ialah unit asas untuk memperuntukkan sumber dan utas ialah aliran kawalan yang dijalankan secara bebas.

王林
王林asal
2024-07-23 10:57:541016semak imbas

Perbezaan antara utas dan proses: Proses ialah unit asas untuk memperuntukkan sumber dan utas ialah aliran kawalan yang dijalankan secara bebas.

Thread: It is the control flow of a single sequence in the process (collectively called lightweightprocesses)

Thread is the smallest unit that the operating system can perform calculation scheduling. It is included in the process and is the actual operating unit in the process. A thread refers to a single sequence of control flow in the process. Multiple threads can be concurrently used in a process. Threads can be kernel threads scheduled by the operating system kernel.

Multiple threads of the same process will share all system resources in the process, such as virtual address space, file descriptors, signal processing, etc.

However, multiple threads in the same process have their own call stacks, their own register environments, and their own thread local storage.

The difference between threads and processes is: Generally, a process can contain multiple threadsLinux process and thread kernel. They can make use of the resources owned by the process. In the operating system, the process is generally regarded as the basic unit for allocating resources, and threads are As the basic unit of independent operation and independent scheduling, hesitant threads are smaller than processes and basically require no system resources.

A process needs at least one thread as its instruction execution body. The process manages resources (such as CPU, video memory, file descriptors, etc.), and the thread will be assigned to the CPU for execution.

The thread model is divided into two thread models, core-level threads and user-level threads. The classification standard is mainly whether the thread scheduler is inside the kernel or outside the kernel. The latter is more conducive to the concurrent use of multi-processor resources, and the former is more Considered is the context switching overhead.

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

The Linux kernel only provides support for lightweight processes, limiting the implementation of more efficient threading modelsLinux processes and threads kernel, and Linux focuses on

Although Linux supports lightweight processes, it cannot be said that it supports core-level processes. Since threads and processes in Linux are actually at the same scheduling level and share a process identifier space, these restrictions cannot be fully realized under Linux. sense POSIX threading mechanism.

The Linux kernel does not support threads in the true sense. Linuxthreads uses lightweight processes that have the same kernel scheduling view as ordinary processes to implement thread support. This lightweight process has an independent process ID and enjoys the same capabilities as ordinary processes in terms of process scheduling, signal processing, IO, etc.

Threads under Linux are lightweight processes.

Each linuxthread thread has both a thread id and a process id, where the process id is the process number maintained by the kernel

, while thread ids are assigned and maintained by linuxthreads

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

The thread id of __pthread_initial_thread is PTHREAD_THREADS_MAX, the thread id of __pthread_manager_thread is 2*PTHREAD_THREADS_MAX+1linux formatting command, the thread id of the first user thread is PTHREAD_THREADS_MAX+2, and the thread id of the subsequent nth user thread follows the following formula:

tid=n*PTHREAD_THREADS_MAX+n+1

Borang peruntukan ini memastikan bahawa semua utas dalam proses (termasuk yang telah keluar) tidak akan mempunyai id utas yang sama, dan jenis id utas pthread_t ditakrifkan sebagai unsigned long int (unsignedlongint), yang juga memastikan masa berjalan yang munasabah ID urutan dalaman tidak akan diulang.

Mencari struktur data benang daripada id benang selesai dalam fungsi pthread_handle() Sebenarnya, ia hanyalah modulo nombor benang PTHREAD_THREADS_MAX, dan hasilnya ialah indeks benang dalam __pthread_handles.

5. Penciptaan benang

Selepas pthread_create() menghantar permintaan REQ_CREATE ke urutan pengurusan, urutan pengurusan memanggil pthread_handle_create() untuk mencipta urutan baharu. Selepas memperuntukkan tindanan dan menetapkan atribut utas, gunakan pthread_start_thread() sebagai entri fungsi dan panggil __clone() untuk mencipta dan memulakan utas baharu. pthread_start_thread() membaca nombor ID prosesnya sendiri dan menyimpannya dalam struktur perihalan benang, dan mengkonfigurasi penjadualan mengikut kaedah penjadualan yang direkodkan di dalamnya. Selepas semuanya sedia, panggil fungsi pelaksanaan benang sebenar, dan panggil pthread_exit() untuk mengosongkan pemandangan selepas fungsi ini kembali.

Tiga cara untuk menyemak bilangan utas di bawah Linux:

1.cat/proc/pid/status

2.pstree-ppid

3.top-H-ppid

4.psxH, lihat semua benang sedia ada

5.ps-mppid

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

6.ps-eLf|grep

Lajur kedua hasil pertanyaan perintah ialah PID, lajur ketiga ialah PPID, lajur keempat ialah LWP, dan lajur keenam ialah NLWP.

jstack30420|kurang, kemudian cari nid=0x44bf, oh, jumpa

Kod Shell

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

java.lang.Thread.State:RUNNABLE

di.SocketInputStream.socketRead0(NativeMethod)

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

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

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

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

Arahan jstack telah tamat, mari kita tidak mengkajinya buat masa ini

Atas ialah kandungan terperinci Perbezaan antara utas dan proses: Proses ialah unit asas untuk memperuntukkan sumber dan utas ialah aliran kawalan yang dijalankan secara bebas.. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn