PHP face Test 1-the difference between threads and processes (incidentally, the coprocessor)

Source: Internet
Author: User
Tags php programming
first, what is the process

A process is an instance of program execution, a process 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, and I/O.

A process is a dynamic execution of a program in a dataset and can be simply understood as "executing program", which is a separate unit of CPU resource allocation and scheduling.
Process is generally composed of program, dataset, Process Control block three parts. The program we write is used to describe what the process is going to accomplish and how to do it; the dataset is the resource that the program needs to use in the process of execution, the process control block is used to record the external characteristics of the process, to describe the process of execution change, the system can use it to control and manage the process, it is the only mark of the existence of the system aware process.
The limitation of the process is that the overhead of creating, undoing, and switching is relatively high. second, what is the thread

A thread is a stream of execution of a process, and a thread cannot allocate system resources, it is part of a process, and it is smaller than a process that runs independently.
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 dispatch execution, refers to the process execution path, also called program execution flow. Threads are also sometimes called lightweight processes.

Threads are concepts developed after a process. A thread is also called a lightweight process, which is a basic CPU execution unit and the smallest unit of program execution, composed of thread IDs, program counters, register collections, and stacks. A process can contain multiple threads.
The advantage of the thread is that it reduces the overhead of concurrent execution of the program, improves the concurrency performance of the operating system, the disadvantage is that the thread does not have its own system resources and only has the necessary resources at run time, but the threads of the same process can share the system resources owned by the process, if the process Then a thread is like a worker in a workshop. However, for some exclusive resources exist lock mechanism, improper handling may produce "deadlock". Iii. What is a co-process

The coprocessor is a kind of user-state lightweight thread, also known as micro-threading, English name Coroutine, and the scheduling of the coprocessor is completely controlled by the user. People usually compare the process and subroutine (function) to understand.
A subroutine call is always an entry, one return, and the execution of the subroutine is completed once the exit is exited.
The start of a coprocessor is the first entry point, in the coprocessor, after which the return point is the next entry point. In Python, a coprocessor can invoke other threads through yield. The process of transferring execution through yield is not the relationship between the caller and the callee, but is symmetrical and equal to each other and accomplishes the task through mutual cooperation. The approximate process of its operation is as follows:
The first step, the start of the process a implementation.
In the second step, the process a executes to half, enters the pause, and transfers execution to the yield through the command.
Third step, (after a period of time) to return to the executive power.
Fourth step, the process a resumes execution.

The characteristic of a coprocessor is that it is a thread execution, and its advantages are embodied in the following:
* The execution efficiency of the process is very high . Because the subroutine switch is not a thread switch, but is controlled by the program itself, therefore, there is no thread switching overhead, and multithreading ratio, the number of threads, the performance advantage of the more obvious.
* The coprocessor does not require a multiple-threading lock mechanism. In the process of controlling shared resources without locking, just have to judge the state of the good.
Tips: The simplest way to make use of multi-core CPUs is to take advantage of multiple processes + coprocessor, and make full use of multiple cores, and give full play to the high efficiency of the system, which can obtain a great performance. Iv. process and thread relationships

Processes are like landlords, with land (system resources), threads like tenants (threads, executing the farming process). Each landlord (process) has a working tenant (thread).
Process-the smallest unit of resource allocation, relatively robust, crashes generally do not affect other processes, but switching processes is resource-intensive and inefficient.
Thread-the smallest unit of program execution, there is no independent address space, a thread die may be the whole process died, but save resources, switching efficiency high. v. Common processes and threads in PHP programming

1, in Web applications, we each visit to PHP, set up a PHP process , of course, will also create at least one PHP thread.
2, PHP uses pcntl for multiple process programming
3, PHP uses pthreads for multithreaded Programming
4, Nginx Each process has only one thread , each thread can handle multiple clients Access
5, PHP-FPM uses multi-process model , each process only a thread, each thread can handle only one client access .
6, Apache may use a multiple process model or a multithreaded model, depending on which SAPI is used.
7, the process is the smallest unit of CPU resource allocation , and the thread is the smallest unit of CPU dispatch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.