Nginx Source Code Analysis--Framework design & Master-worker Process Model

Source: Internet
Author: User

Nginx Framework Design-process model

Before that, let us first clarify a few facts:

Nginx as a high-performance server features, in fact, this is also the characteristics of all high-performance servers, relying on epoll system calls efficient (efficient is relative select/poll these system calls, the bottom has a list and red black tree, avoid polling, Reduced data transfer between user space and system space, non-blocking (all operations are non-blocking, so), multi-process (Master-slave process model), which makes nginx a prerequisite for a high-performance server.

As a software (HTTP server), compared to a network library, there must be more sophisticated features. It can be updated without the need to shut down the system, can accept external signals, according to different signals to do the corresponding processing; customizable, users can develop third-party modules and add modules to 7 processing stages according to their own needs (HTTP requests have 11 phases, However, 4 of these stages are not supported by adding user-defined modules, it can be used as a reverse proxy, it can support high concurrency, its operating mode is based on the configuration file, a configuration file determines the Nginx mode of operation, the configuration file resolution completes the initialization of all the work, The parsing of configuration files occupies a pivotal position in Nginx. Based on the above facts, the design concept of nginx is slowly elaborated.

Preferred from the large framework, since it is a multi-process, then how to allocate the tasks of the various processes, the number of cores to open the CPU (of course, if the multi-process can be configured by the configuration file), divided into Master-worker model, Master is responsible for the worker process to open, The master process is responsible for communicating with the worker process, the master process is responsible for receiving external signals, and the master process is responsible for signaling some of the signals (a total of 8 signals, but corresponding to the three variables that the worker process is interested in, and three variables of Ngx_quit/ngx_reopen /ngx_terminate, etc.) is passed to the worker process, so the worker process does not need to pay attention to the external signal, the master process needs to pay attention to the worker process, such as receiving the SIGCHLD signal, that is, the worker process exits, It is also necessary to check that the worker process is dead if the worker process needs to be prevented from becoming a zombie process, and if an abnormal death requires a restart of the worker process. So what does the worker process do? Inter-process communication is that all?

In fact, the master process and worker process time is a communication, the worker process can also have communication between, but there is no communication, the way of communication is through the Socketpair, there is shared memory, both have different roles. What did the worker do? Responsible for monitoring, accept the connection, after accepting the responsibility and the connection process interaction, until the end of the connection! In fact, the process model here has ended, only a few worker process how to do is another thing!

The following is an analysis of everything mentioned above from the code:

In Src/core/nginx.c, there is the entry main function, which basically understands what the main function does.

1), Ngx_get_options function is based on user input parameters to set some global variables, such as user input-S, then set the NGX_SIGNAL flag, this flag indicates that the user needs to give ngxin input a signal, such as global variables. Then proceed with the global variable.

2), the next if (Ngx_show_version) is set in step 1). Indicates whether the user wants to see the version information. (This is, of course, the first global variable to be set in ngx_get_options)

3) the Ngx_time_init () function sets the initialization of the representation time, the last line in the function ngx_time_update is a very important function, is used to update the time of the function, according to the configuration file, determine the way to update the time, later discussion, Since each call to Gettimeofday () This system call is a waste of time, then need a time cache, the update time of two, ngx_time_sigsafe_update and Ngx_time_update, the former refers to the time responsible for Err_log, The latter is responsible for updating a lot of time

4) Next is Ngx_getpid (), create a file for the master process, and save the PID of the master process separately.

5) Next is the ready to initialize the global variable ngx_cycle, this one big, first create a memory pool, save the relevant user passed in Parameters ngx_save_argv,

6) The next thing to do with the selection is to see what parameters are saved in step 5 based on the parameters entered by the user.

7) The Ngx_add_inherited_sockets function is the processing of integrated sockets, which are set according to the NGINX environment variable, if there is no such environment variable, then do not operate, These sockets are required to be saved to the cycle in the listening as a listening socket.

8) Ngx_init_cycle is the initialization of the global big, here do a lot of configuration, especially one of the ngx_conf_parse () function, this function parsing configuration file.

9) Next is the process of processing step 1 according to the user input parameters are set what global variables, such as Ngx_test_config and ngx_signal, if the signal requires the corresponding operation Ngx_signal_process.

10) If it is not signal processing, then it is necessary to initialize the signal that the master process needs to pay attention to.

11) Determine if it is necessary to set the daemon process and finally enter the model to open the worker process. Ngx_single_process_cycle and Ngx_master_process_cycle.

So far, the main function has been analyzed, of which three points is more important, a ngx_init_cycle ()

Ngx_master_process_cycle and Ngx_init_signals () functions. Let's start with a simple look.

How to handle the signal in Nginx:

In the Ngx_init_signals function, traverse the signals Global array, which holds the signals that Nginx needs to pay attention to and assigns a corresponding signal processing function to each signal. All the signals correspond to the same signal processing function ngx_signal_handler () function, in this function have seen a function mentioned earlier, and update time about, Ngx_time_sigsafe_update, this function is update error time, that is, There is an update time operation in signal processing, although only the time of the error message is updated. Look at the way of signal processing, first, according to Ngx_process to determine the current way of transmitting the signal, is to pass to the master process? Or is it for all the processes to be preached?

In a word, the master process receives the signal from the outside, then the master process is activated, and all signals correspond to the same signal processing function ngx_signal_handler processing, which only handles the corresponding global flag bit, Then the master process according to the flag bit to do the corresponding processing, most of the signal processing is the ngx_signal_worker_process function, that is, the need to receive the master process signal to the worker process to pass a copy, There are only three signals that need to be passed to the worker process, which means that the worker process only needs to focus on three signals (not three signals, maybe several signals, but there are multiple signals that correspond to the same processing, and eventually the worker process only needs to focus on ngx_reopen/ngx_ Quit/ngx_terminate of these three variables). The way the signal is passed may be socketpait, or it may be called with the kill system.

A simple introduction signal processing is: Since Nginx is a multi-process, then not all processes can accept the external signal, then this task to master process, master process will need to register their attention signal, while the corresponding signal processing function, Then after fork out the child process, the child process needs to empty the integrated signal (because the external signal processing to the master process), the parent-child process of communication through socketpair processing, then the child process on this channel read event processing is Ngx_channel_handler , in this function, modify the global variables according to the commands sent by the master process. Note that the process is activated by the signal the next action is to determine the global variables, then the changes to the global variables for different processes are not the same, the master process is through the signal processing function (Ngx_signal_handler processing), Worker process is a function of a socket created by Socketpair (Ngx_channel_handler)

After the parent-child process is activated by the signal, it is determined by the global variables to do what processing. So who's going to modify the global variables? So for the master process, the task of modifying the global variable is given to the Ngx_signal_handler function, and for the subprocess, this task is ngx_channel_handler to handle. And the master process is concerned about more signals than the child process, all the signals are not to be passed to the sub-process, so there is ngx_process this variable. For example, in the Ngx_get_options function, if the-s option is found, then the user is ready to send a signal to nginx, if the signal is stop and so on, you need to set ngx_process to Ngx_process_signaller, Indicates that this signal needs to be sent to the child process. Ngx_process are not many places to be assigned, in NGINX.C, ngx_cycle.c are assigned operations. Ngx_process is assigned in Ngx_get_options if the user enters a signal such as stop. In the main function, if there is a MASTER configuration, it is assigned a value of Ngx_process_master.

Nginx Source Code Analysis--Framework design & Master-worker Process Model

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.