Home > System Tutorial > LINUX > body text

The difference between threads and processes: A process is the basic unit for allocating resources, and a thread is an independently running control flow.

王林
Release: 2024-07-23 10:57:54
Original
1060 people have browsed it

The difference between threads and processes: A process is the basic unit for allocating resources, and a thread is an independently running control flow.

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

이러한 할당 형식은 프로세스의 모든 스레드(이미 종료된 스레드 포함)가 동일한 스레드 ID를 갖지 않도록 하고 스레드 ID 유형 pthread_t가 unsigned long int(unsignedlongint)로 정의되어 합리적인 실행 시간도 보장합니다. 내부 스레드 ID는 반복되지 않습니다.

스레드 ID에서 스레드 데이터 구조 검색은 pthread_handle() 함수에서 완료됩니다. 실제로는 PTHREAD_THREADS_MAX 모듈로 스레드 번호일 뿐이며 결과는 __pthread_handles에 있는 스레드의 인덱스입니다.

5. 스레드 생성

pthread_create()가 관리 스레드에 REQ_CREATE 요청을 보낸 후 관리 스레드는 pthread_handle_create()를 호출하여 새 스레드를 생성합니다. 스택을 할당하고 스레드 속성을 설정한 후 pthread_start_thread()를 함수 항목으로 사용하고 __clone()을 호출하여 새 스레드를 생성하고 시작합니다. pthread_start_thread()는 자신의 프로세스 ID 번호를 읽어서 Thread Description 구조체에 저장하고, 거기에 기록된 스케줄링 방식에 따라 스케줄링을 구성한다. 모든 것이 준비되면 실제 스레드 실행 함수를 호출하고, 이 함수가 반환된 후 pthread_exit()를 호출하여 장면을 삭제합니다.

Linux에서 스레드 수를 확인하는 세 가지 방법:

1.cat/proc/pid/status

2.pstree-ppid

3.top-H-ppid

4.psxH, 기존 스레드 모두 보기

5.ps-mppid

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

6.ps-eLf|grep

명령 쿼리 결과의 두 번째 열은 PID, 세 번째 열은 PPID, 네 번째 열은 LWP, 여섯 번째 열은 NLWP입니다.

jstack30420|less, 그런 다음 nid=0x44bf를 검색하세요. 오, 찾았어요

쉘 코드

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

java.lang.Thread.State:RUNNABLE

at.SocketInputStream.socketRead0(네이티브메소드)

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)

jstack 명령이 끝났으니 지금은 공부하지 말자

The above is the detailed content of The difference between threads and processes: A process is the basic unit for allocating resources, and a thread is an independently running control flow.. For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template