


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.
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
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Linux is best used as server management, embedded systems and desktop environments. 1) In server management, Linux is used to host websites, databases, and applications, providing stability and reliability. 2) In embedded systems, Linux is widely used in smart home and automotive electronic systems because of its flexibility and stability. 3) In the desktop environment, Linux provides rich applications and efficient performance.

The five basic components of Linux are: 1. The kernel, managing hardware resources; 2. The system library, providing functions and services; 3. Shell, the interface for users to interact with the system; 4. The file system, storing and organizing data; 5. Applications, using system resources to implement functions.

Linux system management ensures the system stability, efficiency and security through configuration, monitoring and maintenance. 1. Master shell commands such as top and systemctl. 2. Use apt or yum to manage the software package. 3. Write automated scripts to improve efficiency. 4. Common debugging errors such as permission problems. 5. Optimize performance through monitoring tools.

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

Linuxisfundamentallyfree,embodying"freeasinfreedom"whichallowsuserstorun,study,share,andmodifythesoftware.However,costsmayarisefromprofessionalsupport,commercialdistributions,proprietaryhardwaredrivers,andlearningresources.Despitethesepoten

Linux devices are hardware devices running Linux operating systems, including servers, personal computers, smartphones and embedded systems. They take advantage of the power of Linux to perform various tasks such as website hosting and big data analytics.

The disadvantages of Linux include user experience, software compatibility, hardware support, and learning curve. 1. The user experience is not as friendly as Windows or macOS, and it relies on the command line interface. 2. The software compatibility is not as good as other systems and lacks native versions of many commercial software. 3. Hardware support is not as comprehensive as Windows, and drivers may be compiled manually. 4. The learning curve is steep, and mastering command line operations requires time and patience.
