Home > Article > Backend Development > What are the differences between the PHP process model, process communication methods, and process threads?
The PHP process model is an executing program, an entity that can be assigned to a processor and executed by the processor; PHP process communication methods include pipes and named pipes, signals, shared memory, etc.; PHP process threads The difference is that a process is an independent unit for resource allocation and scheduling, while a thread is the basic unit of CPU scheduling.
The differences between the PHP process model, process communication methods, and process threads are:
1. PHP process model
The concept of process is the basis of the structure of the operating system. The designers of Multics first used this technical term in the 1960s, and it is more general than homework. The definition of process is as follows:
1. An executing program.
2. An instance of a program running on the computer.
3. An entity that can be assigned to and executed by the processor.
4. An activity unit described by a single sequential thread of execution, a current state, and a set of related system resources.
Related learning recommendations: PHP programming from entry to proficiency
2. The difference between processes and threads
Process
is the basic unit of resource allocation. All resources related to the process are recorded in the process control block PCB
. To indicate that the process owns these resources or is using them.
In addition, the process is also the scheduling unit that preempts the processor, and it has a complete virtual address space. When processes are scheduled, different processes have different virtual address spaces, and different threads within the same process share the same address space.
Corresponding to the process, the thread has nothing to do with resource allocation. It belongs to a certain process and shares the resources of the process with other threads in the process.
Thread
is composed only of relevant stack (system stack or user stack) registers and thread control table TCB
. Registers can be used to store local variables within a thread, but cannot store variables related to other threads.
Usually a process can contain several threads, which can utilize the resources owned by the process. In operating systems that introduce threads, processes are usually regarded as the basic unit of resource allocation, and threads are regarded as the basic unit of independent operation and independent scheduling. Since threads are smaller than processes and basically do not own system resources, the overhead for scheduling them will be much smaller, which can more efficiently increase the degree of concurrent execution among multiple programs in the system, thereby significantly increasing system resources. utilization and throughput. Therefore, general-purpose operating systems launched in recent years have introduced threads to further improve the concurrency of the system, and regard it as an important indicator of modern operating systems.
The difference between threads and processes can be summarized as the following four points:
(1) A process is an independent unit for resource allocation and scheduling, while a thread is a CPU scheduling The basic unit
(2) The same process can include multiple threads, and the threads share the resources of the entire process (registers, stacks, context), and one process includes at least one thread.
(3) The creation of a process calls fork or vfork, and the creation of a thread calls pthread_create. After the process ends, all threads it owns will be destroyed, and the end of the thread will not affect other threads in the same process. End
(4) Threads are lightweight processes, and their creation and destruction take much less time than processes. All execution functions in the operating system are completed by creating threads
(5) Synchronization and mutual exclusion are generally required when executing in threads, because they share all resources of the same process
(6) Threads have their own private attributes TCB, thread id, registers, and hardware Context, and the process also has its own private attribute process control block PCB. These private attributes are not shared and are used to mark a process or a thread.
3. Inter-process communication method
1. Pipe (Pipe) and named pipe (named pipe): Pipes can be used for communication between processes with affinity relationships. Named pipes overcome the limitation that pipes do not have names. Therefore, In addition to the functions of pipes, it also allows communication between unrelated processes;
2. Signal: Signal is a relatively complex communication method used for notifications When a certain event occurs in a receiving process, in addition to being used for inter-process communication, the process can also send signals to the process itself; in addition to supporting the early Unix signal semantic function sigal, Linux also supports the signal function sigaction whose semantics conform to the Posix.1 standard (actually This function is based on BSD. In order to achieve a reliable signal mechanism and unify the external interface, BSD re-implemented the signal function using the sigaction function);
3. Message queue (message queue): The message queue is a linked list of messages, including the Posix message queue system V message queue. A process with sufficient permissions can add messages to the queue, and a process granted read permissions can read messages from the queue. The message queue overcomes the shortcomings of signals carrying a small amount of information, pipes can only carry unformatted byte streams, and buffer sizes are limited.
4. Shared memory: Allows multiple processes to access the same memory space, which is the fastest available IPC form. It is designed for the lower operating efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
Semaphore: Mainly used as a means of synchronization between processes and between different threads of the same process.
5. Socket: A more general inter-process communication mechanism that can be used for inter-process communication between different machines. It was originally developed for the BSD branch of Unix systems, but is now generally portable to other Unix-like systems: both Linux and System V variants support sockets.
The above is the detailed content of What are the differences between the PHP process model, process communication methods, and process threads?. For more information, please follow other related articles on the PHP Chinese website!