PHP development requires knowledge of processes and threads, as well as multi-threaded development. So what are processes and threads? Let's have a chatter.
Content Summary:
1. What is a process
2. What is a thread
3. Process and thread relationships
4. Common processes and threads for PHP programming
I. What is a process
A process is an instance of program execution that can be allocated to resources such as CPU and memory. Processes typically include instruction sets and system resources, where the instruction set is your code, and system resources refer to CPU, memory, I/O, and so on.
Second, what is a thread
A thread is an execution flow of a process, and a thread cannot allocate system resources, it is part of a process, and is a unit of independent operation that is smaller than the process.
Explain: The process has two characteristics: one is the ownership of the resource, one is the dispatch execution (instruction set), the thread is a part of the schedule execution, refers to the process execution process path, also called the program execution flow. Threads are also sometimes called lightweight processes.
From the in-depth understanding of the PHP kernel website
Iii. relationship of processes and threads
The process is like a landlord with land (system resources), and threads are like tenants (threads that perform farming processes). Each landlord (process) has only one tenant (thread) to work with.
Process-the smallest unit of resource allocation, relatively robust, crashes generally do not affect other processes, but the process of switching costs resources, less efficient.
Thread-Program execution of the smallest unit, no independent address space, a thread dead may be the entire process will die, but save resources, switching efficiency.
Iv. common processes and threads for PHP programming
1, in the Web application, every time we access PHP, we build a PHP process, of course, will also build at least one PHP thread.
2. PHP uses PCNTL for multi-process programming
3. Use Pthreads for multithreaded programming in PHP
4, Nginx each process only one thread, each thread can handle the access of multiple clients
5, PHP-FPM uses a multi-process model, each process has only one thread, and each thread can only handle one client access.
6, Apache may use a multi-process model, or the use of multithreaded models, depending on which sapi to use.
Conclusion
There are many threads and processes, such as switching between threads, priority, shared resources, communication between threads, daemons, process locks, thread safety, and so on.
Documenting the process and threading understanding of PHP