Home > Article > Operation and Maintenance > What 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 processes1. 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.
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.
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.
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.
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.
Since threads need to be scheduled like processes, there must be parameters available for scheduling. This parameter is the priority of the thread. .
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 QueueA 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!