search
HomeOperation and MaintenanceSafetyWhat is the relationship between threads and processes

A process must have at least one thread, but can have multiple threads. A thread can only execute within the address space of one process. 2. Resources are allocated to processes, and all threads of the same process share all resources of the process. 3. The CPU is allocated to threads, that is, the threads are actually running on the processor. In order to achieve synchronization, executing threads need to cooperate with each other, and cross-process threads should use message communication.

The process is the basic unit of resource allocation, and the thread is the basic unit of CPU scheduling and dispatch.

Threads are part of the process. A thread can only belong to one process. A process can have Multiple threads, but at least one thread

Each process has an independent code and data space (program context). Switching between programs is expensive. Threads can be regarded as lightweight processes, of the same type. Threads share code and data space. Each thread has its own independent running stack and program counter (PC). The cost of switching between threads is small. In the operating system, multiple processes (programs) can be run at the same time. Multiple threads in the same process (program) are executed simultaneously (through CPU scheduling, only one thread is executed in each time slice)

The system will allocate different memory space to each process when running. In addition to the CPU, the system does not allocate memory for threads (the resources used by threads come from the resources of the process to which they belong), and thread groups can only share resources

No existing process can be regarded as single-threaded , if there are multiple threads in a process, the execution process is not one line, multiple threads (threads) are completed together

Threads are part of the process, so threads are called lightweight processes/lightweight processes



The relationship between processes and threads

1. A process can have multiple threads, but there is at least one thread; and a thread can only be in one process activities within the address space.

2. Resources are allocated to processes, and all threads of the same process share all resources of the process.

3. The CPU is allocated to threads, that is, the threads are actually running on the processor.

4. Threads need to cooperate and synchronize during execution. Threads in different processes must use message communication to achieve synchronization.

What can be shared between processes?

The environment shared by threads includes: process code segments, public data of the process (using these shared data, threads can easily communicate with each other), file descriptors opened by the process, and signals The processor, the current directory of the process, and the process user ID and process group ID.

While processes have many commonalities, they also have their own personality. With these personalities, threads can achieve concurrency. These characteristics include:

1. Thread ID

Each thread has its own thread ID, which is unique in this process. Processes use this to identify threads.

2. Register group value

Since threads run concurrently, each thread has its own different running clues. When switching from one thread to another, , the state of the original thread's register set must be saved so that the thread can be restored when it is switched again in the future.

3. Thread stack

The stack is necessary to ensure that the thread runs independently. Thread functions can call functions, and the called functions can be nested layer by layer, so the thread must have its own function stack so that the function call can be executed normally without being affected by other threads.

4. Error return code

Since there are many threads running in the same process at the same time, it is possible that a certain thread sets the errno value after making a system call, and in this thread This error has not been processed yet, and another thread is put into operation by the scheduler at this time, so the error value may be modified.
So, different threads should have their own error return code variables.

5. Thread's signal masking code

Since the signals that each thread is interested in are different, the thread's signal masking code should be managed by the thread itself. But all threads share the same signal handler.

6. Thread priority

Since threads need to be scheduled like processes, there must be parameters available for scheduling. This parameter is the priority of the thread. .

Five ways of inter-process communication

1. (Unnamed) pipe

Half-duplex, that is, not simultaneous Transfer data in both directions. Some systems may support full duplex.

Only between parent and child processes. In its classic form, the parent process creates a pipe and then forks the child process, thereby enabling use between the parent and child processes.

2. Named pipe (FIFO)

Irrelevant processes can also exchange data.

3. Message Queue

A message queue is a list of messages stored in the kernel, similar to a linked list of messages. User processes can add messages to the message queue and read messages from the message queue.

Compared with pipeline communication, the advantage of message queue is to specify a specific message type for each message. When receiving, you do not need to follow the queue order, but can receive specific types of messages based on custom conditions.

You can think of a message as a record, with a specific format and a specific priority. A process with write permissions on the message queue can add new messages to the message queue according to certain rules, and a process with read permissions on the message queue can read messages from the message queue.

4. Semaphore

When multiple processes need to access shared data, the semaphore is a counter mainly used for this purpose. If you need to ensure that the same data is not accessed by two processes at the same time, you can use a semaphore.

Its main process is as follows:

Check the semaphore that controls the resource

If the semaphore value is greater than 0, the resource is available, and it is reduced by 1, indicating that the current Has been used

If the semaphore value is 0, the process sleeps until the semaphore value is greater than 0

That is to say, it actually provides a different process or different threads of the process. means of access synchronization between

5. Shared memory

Shared memory allows two or more processes to share a given storage area. This storage area can be used by two or more processes. The above process is mapped to its own address space. The information written by one process to the shared memory can be read out by other processes using this shared memory through a simple memory read, thereby realizing inter-process communication.

One of the main benefits of using shared memory for communication is high efficiency, because the process can directly read and write memory without any copy of data. For communication methods such as pipes and message queues, the kernel needs to be Data is copied four times to user space, while shared memory is copied only twice: once from the input file to the shared memory area, and the other time from the shared memory to the output file.

Generally speaking, when processes share memory, they do not always unmap after reading and writing a small amount of data. When there is new communication, the shared memory area is re-established; instead, the shared area is maintained until Until the communication is completed, the data content is kept in the shared memory and is not written back to the file. Because the contents of shared memory are usually written back to the file when they are unmapped, the method of using shared memory for communication is extremely efficient.

6. Socket:

Socket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used between different computers. process communication.

7. Signal (sinal)

The signal is a relatively complex communication method used to notify the receiving process that an event has occurred

The above is the detailed content of What is the relationship between threads and processes. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
解释一下explorer.exe进程是什么解释一下explorer.exe进程是什么Feb 18, 2024 pm 12:11 PM

explorer.exe是什么进程在我们使用Windows操作系统的时候,经常会听到一个名词"explorer.exe".那么,你是否好奇这个进程到底是什么?在本文中,我们将详细解释explorer.exe是什么进程以及其功能和作用。首先,explorer.exe是Windows操作系统的一个关键进程,它负责管理和控制Windows资源管理器(Window

com surrogate是什么进程com surrogate是什么进程Sep 14, 2022 pm 02:56 PM

“com surrogate”是“C:\Windows\System32\dllhost.exe”的进程;出现该进程通常是“COM+”组件停止工作,这个进程占用高甚至直接未响应,这是由于电脑在加载文件图标的时候出现问题,导致电脑变的卡顿,可以在计算机属性设置中解决卡顿问题。

请问ccsvchst.exe是哪种进程?请问ccsvchst.exe是哪种进程?Feb 19, 2024 pm 11:33 PM

ccsvchst.exe是一种常见的进程文件,它是SymantecEndpointProtection(SEP)软件的一部分,而SEP则是一款由著名的网络安全公司Symantec开发的终端保护解决方案。作为该软件的一部分,ccsvchst.exe负责管理和监控SEP的相关进程。首先,我们来了解一下SymantecEndpointProtection(

如何正确杀死Linux中的僵尸进程如何正确杀死Linux中的僵尸进程Feb 19, 2024 am 10:40 AM

在Linux系统中,僵尸进程是已经终止但仍保留在系统中的特殊进程。虽然僵尸进程不会消耗太多资源,但如果数量过多,可能会导致系统资源耗尽。本文将介绍如何正确清除僵尸进程,以确保系统的正常运行。1Linux僵尸进程子进程完成任务后,如果父进程没有及时检查状态,子进程将成为僵尸进程。子进程在等待父进程确认,完成后系统才会回收。否则,僵尸进程会继续悬挂在系统中。要检查系统中是否存在僵尸进程,可通过运行命令top来查看所有正在运行的进程及可能存在的僵尸进程。‘top’命令的结果从上图可以看到Linux中进

如何在 Windows 11 中停止任务管理器进程更新并更方便地终止任务如何在 Windows 11 中停止任务管理器进程更新并更方便地终止任务Aug 20, 2023 am 11:05 AM

如何在Windows11和Windows10中暂停任务管理器进程更新按CTRL+窗口键+删除打开任务管理器。默认情况下,任务管理器将打开“进程”窗口。正如您在此处看到的,所有应用程序都在无休止地移动,当您想要选择它们时,可能很难将它们指向下方。因此,按CTRL并按住它,这将暂停任务管理器。您仍然可以选择应用程序,甚至可以向下滚动,但您必须始终按住CTRL按钮。

什么是程序运行时指令流的最小单位什么是程序运行时指令流的最小单位Aug 23, 2022 pm 02:16 PM

“线程”是程序运行时指令流的最小单位。进程是指一个具有一定独立功能的程序,而线程是进程的一部分,描述指令流执行状态;线程是进程中的指令执行流的最小单位,是CPU调度的基本单位。一个线程是一个任务(一个程序段)的一次执行过程;线程不占有内存空间,它包括在进程的内存空间中。在同一个进程内,多个线程共享进程的资源;一个进程至少有一个线程。

multitip.exe是什么进程multitip.exe是什么进程Mar 13, 2023 pm 01:41 PM

multitip.exe是360内置的广告弹窗进程,其中包括了今日优品、焦点资讯、每日趣玩等广告弹窗;其删除方法:1、找到并删除sesvc.exe文件;2、在原来的目录下建立一个sesvc.exe目录,然后将这个目录的所有用户的“完全控制”权限设置为“拒绝”即可。

一个进程被唤醒意味着什么一个进程被唤醒意味着什么Jul 08, 2022 pm 04:50 PM

一个进程被唤醒意味着进程变为就绪状态。进程状态反映进程执行过程的变化,进程被唤醒就是激活这个进程,意味着它变为就绪状态,也表示该进程具备运行条件,等待系统分配处理器以便运行;但进入就绪状态后并不马上移至就绪队列队首,而是在队尾参加排队。

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),