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

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!

Statement
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
What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Boost Productivity with Custom Command Shortcuts Using Linux AliasesBoost Productivity with Custom Command Shortcuts Using Linux AliasesApr 12, 2025 am 11:43 AM

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

What is Linux actually good for?What is Linux actually good for?Apr 12, 2025 am 12:20 AM

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Essential Tools and Frameworks for Mastering Ethical Hacking on LinuxEssential Tools and Frameworks for Mastering Ethical Hacking on LinuxApr 11, 2025 am 09:11 AM

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

How to learn Linux basics?How to learn Linux basics?Apr 10, 2025 am 09:32 AM

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.

What is the most use of Linux?What is the most use of Linux?Apr 09, 2025 am 12:02 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)