Turn "Nginx series (three)--management process, multi-work process design

Source: Internet
Author: User

Original blog out of: http://blog.csdn.net/liutengteng130/article/details/46700999 thanks!

Nginx consists of a master process and multiple worker processes, but the master process or worker process does not create threads again.

I. The role of the master process and the worker process

Master Process

Do not need to handle network events, not responsible for the execution of the business, only through the management of workers and other sub-processes to achieve restart services, smooth upgrade, change log files, configuration files in real-time function and other functions.

Master is the process of calling child processes through the fork system to implement communication with child processes.

Worker Process

A request to handle the master process Fork .

Worker processes are processed by signals to communicate with Master .

====================================================================================

Second, the signal processing process

How does the Master process receive the signal to be processed?

After the master process receives the signal, it reloads the configuration file, starts the new process, and sends a signal to all the old processes that they can retire honorably. After the new process starts, it begins accepting new requests, and the old process no longer receives the new request after it receives the master signal, and exits after all outstanding requests in the current process have been processed.

How does the Worker process receive the signal processing?

First of allWorkerProcesses are equal, and every process, the opportunity to process requests, is the same. When we provide80 http service, a connection request comes in, each process has the possibility to handle this connection, how to do it? First of all, each worker process is from master process fork come over, in master process inside, first to establish a good need listen socketforkworker processes so that each worker The process can accept this socketaccept this socket The above process will be notified, and only one process can accept the connection, others accept failed, this is called the surprise group phenomenon.

in order to solve this problem,Nginx provides a Accept_mutex(optional, open by default). This is a shared place added to the accept . With this lock, at the same moment, there will be only one process in the accept connection , so there will be no surprise group problem.

when a worker process begins to accept the connection, it starts to read the request, parses the request, processes the request, generates the data, returns it to the client, and finally disconnects. A request is handled entirely by the worker process and is handled only in a worker process.

====================================================================================

Third, the advantages of such design

1. Utilize the concurrent processing capability of multi-core system

now the operating system is multi-core CPU, if there is only one process in the work, then wasted resources, if the worker process status inequality will inevitably cause the process bottleneck problem,Nginx To avoid this problem, design equality between worker processes. In addition, the number of workers and the number of CPU cores is the same, it will achieve the full utilization of CPU resources, if the number of workers than the number of CPU cores, is bound to cause the competition of resources , if it is less than the CPU core, it will cause waste of resources. Improve network performance and reduce request latency.

2. Load Balancing

Load Balancing is achieved through process communication between multiple workers. That is, when a request arrives, it is easier to allocate it to a worker process with less load. This reduces the latency of the request and, to a certain extent, improves network performance

3, the management process is responsible for monitoring the status of the work process, and responsible for the management of its behavior

the benefits of doing this are:

A. how much system resources will not be consumed by the management process

B. The management process is responsible for monitoring the status of the worker process, and if a worker process dies, the management process is responsible for creating a new worker process to avoid system performance degradation. Improve the reliability of the system.

C. management process to support the operation of Nginx service program upgrade, configuration item modification, making dynamic scalability, dynamic customization, dynamic evolution is easier to implement.

Turn "Nginx series (three)--management process, multi-work process design

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.