Home Operation and Maintenance Safety What is the relationship between threads and processes

What is the relationship between threads and processes

May 22, 2023 pm 10:35 PM
process thread

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain what the explorer.exe process is Explain what the explorer.exe process is Feb 18, 2024 pm 12:11 PM

What process is explorer.exe? When we use the Windows operating system, we often hear the term "explorer.exe". So, are you curious about what this process is? In this article, we will explain in detail what process explorer.exe is and its functions and effects. First of all, explorer.exe is a key process of the Windows operating system. It is responsible for managing and controlling Windows Explorer (Window

What kind of process is ccsvchst.exe? What kind of process is ccsvchst.exe? Feb 19, 2024 pm 11:33 PM

ccsvchst.exe is a common process file that is part of the Symantec Endpoint Protection (SEP) software, and SEP is an endpoint protection solution developed by the well-known network security company Symantec. As part of the software, ccsvchst.exe is responsible for managing and monitoring SEP-related processes. First, let’s take a look at SymantecEndpointProtection(

How to properly kill zombie processes in Linux How to properly kill zombie processes in Linux Feb 19, 2024 am 10:40 AM

In Linux systems, zombie processes are special processes that have been terminated but still remain in the system. Although zombie processes do not consume many resources, if there are too many, they may cause system resource exhaustion. This article will introduce how to correctly remove zombie processes to ensure the normal operation of the system. 1Linux zombie process After the child process completes its task, if the parent process does not check the status in time, the child process will become a zombie process. The child process is waiting for confirmation from the parent process, and the system will not recycle it until it is completed. Otherwise, the zombie process will continue to hang in the system. To check whether there are zombie processes in the system, you can run the command top to view all running processes and possible zombie processes. The result of the ‘top’ command can be seen from the figure above in Linux.

Detailed explanation of Linux process priority adjustment method Detailed explanation of Linux process priority adjustment method Mar 15, 2024 am 08:39 AM

Detailed explanation of the Linux process priority adjustment method. In the Linux system, the priority of a process determines its execution order and resource allocation in the system. Reasonably adjusting the priority of the process can improve the performance and efficiency of the system. This article will introduce in detail how to adjust the priority of the process in Linux and provide specific code examples. 1. Overview of process priority In the Linux system, each process has a priority associated with it. The priority range is generally -20 to 19, where -20 represents the highest priority and 19 represents

How to stop Task Manager process updates and kill tasks more easily in Windows 11 How to stop Task Manager process updates and kill tasks more easily in Windows 11 Aug 20, 2023 am 11:05 AM

How to Pause Task Manager Process Updates in Windows 11 and Windows 10 Press CTRL+Window Key+Delete to open Task Manager. By default, Task Manager will open the Processes window. As you can see here, all the apps are endlessly moving around and it can be hard to point them down when you want to select them. So, press CTRL and hold it, this will pause the task manager. You can still select apps and even scroll down, but you must hold down the CTRL button at all times.

Why do processes in Linux sleep? Why do processes in Linux sleep? Mar 20, 2024 pm 02:09 PM

Why do processes in Linux sleep? In the Linux operating system, a process can become dormant due to a number of different reasons and conditions. When a process is in a dormant state, it means that the process is temporarily suspended and cannot continue execution until certain conditions are met before it can be awakened to continue execution. Next, we will introduce in detail several common situations when a process enters hibernation in Linux, and illustrate them with specific code examples. Waiting for I/O to complete: When a process initiates an I/O operation (such as reading

C++ Concurrent Programming: How to avoid thread starvation and priority inversion? C++ Concurrent Programming: How to avoid thread starvation and priority inversion? May 06, 2024 pm 05:27 PM

To avoid thread starvation, you can use fair locks to ensure fair allocation of resources, or set thread priorities. To solve priority inversion, you can use priority inheritance, which temporarily increases the priority of the thread holding the resource; or use lock promotion, which increases the priority of the thread that needs the resource.

C++ Concurrent Programming: How to do thread termination and cancellation? C++ Concurrent Programming: How to do thread termination and cancellation? May 06, 2024 pm 02:12 PM

Thread termination and cancellation mechanisms in C++ include: Thread termination: std::thread::join() blocks the current thread until the target thread completes execution; std::thread::detach() detaches the target thread from thread management. Thread cancellation: std::thread::request_termination() requests the target thread to terminate execution; std::thread::get_id() obtains the target thread ID and can be used with std::terminate() to immediately terminate the target thread. In actual combat, request_termination() allows the thread to decide the timing of termination, and join() ensures that on the main line

See all articles