Home > Common Problem > What is the relationship between threads and processes?

What is the relationship between threads and processes?

青灯夜游
Release: 2023-01-31 14:18:14
Original
12542 people have browsed it

Relationship: 1. A process can have multiple threads, but there is at least one thread; and a thread can only be active 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. 4. Threads need to cooperate and synchronize during execution. Threads in different processes must use message communication to achieve synchronization.

What is the relationship between threads and processes?

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

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

Thread is part of the process. A thread can only belong to one process, and a process can have multiple threads. , but there is at least one thread

Each process has independent code and data space (program context), switching between programs is expensive, threads can be regarded as lightweight processes, and threads of the same type 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. In the same process Multiple threads in (program) are executed simultaneously (through CPU scheduling, only one thread is executed in each time slice)

The system will allocate different memory space for each process when running. Threads except CPU In addition, the system will 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 a If there are multiple threads in the process, the execution process is not one line. Multiple threads (threads) are completed together
Threads are part of the process, so threads are called light-weight processes/lightweight processes

The relationship between processes and threads

1. A process can have multiple threads, but at least one thread; and a thread can only Activities within the address space of a 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.

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), and file descriptions opened by the process. symbol, signal handler, 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.

Therefore, 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, i.e. data cannot be transmitted in both directions at the same time. Some systems may support full duplex.

Only between parent and child processes. The classic form is that the pipe is created by the parent process. After the process forks the child process, it can be used between the parent and child processes.

2. Named pipe (FIFO)

Irrelevant processes can also exchange data.

3. Message Queue

The message queue is a linked list of messages, a list of a series of messages stored in the kernel. 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

The semaphore is a counter, which is mainly used when multiple processes need to access shared data. Considering this situation, two processes cannot access the same data at the same time, so such a thing can be accomplished with the help of semaphores.

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. The content in shared memory is often written back to the file when it is unmapped. Therefore, the communication method using shared memory is very 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

For more related knowledge, please visit the FAQ column!

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!

Related labels:
source:php.cn
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