Nginx Source Analysis--nginx interprocess communication

Source: Internet
Author: User
Tags blank page apache log

There are many IPC under Linux, the Nginx process is the process of affinity, for their communication we choose TCP socket to communicate. The advantage of TCP sockets for process communication is that 1.socket is a file descriptor and simple to operate. 2. Two-way flow. 3. There is another important benefit: The record can be reproduced, we can use tcpdump to crawl information, easy to debug.

Of course, for the sharing of large amounts of data between processes, we use shared memory naturally.

Use the Socketpair () function to create an anonymous socket for communication between the master process (parent) and the work process (child processes) and the work process.

Socketpair () see here.

First look at the definition of nginx process

Ngx_process.h

typedef struct {  ngx_pid_t   pid;  int status;  ngx_socket_tchannel[2];//here is the two descriptors used to store Socketpair  ngx_spawn_proc_pt   proc;  void   *data;

In the NGINX_PROCESS.C

Ngx_pid_tngx_spawn_process (ngx_cycle_t *cycle, ngx_spawn_proc_pt proc, void *data,  char *name, ngx_int_t respawn) {  u_long on;  ngx_pid_t  pid;  ngx_int_t  S;  .. if (Socketpair (Af_unix, Sock_stream, 0, ngx_processes[s].channel) = =-1)  //note before fork Oh    {      ngx_log_error ( Ngx_log_alert, Cycle->log, Ngx_errno,              "Socketpair () failed while spawning \"%s\ "," name ";      return ngx_invalid_pid;    } ...  Pid=fork ();..

Before executing the fork, call Socketpair () to create a pair of socket descriptors in the variable ngx_process[s].channel, after fork (), the child process inherits the pair of the socket descriptor of the parent process, and Nginx will now Use Channel[0] to the parent process, and channel[1] to the child process. You can then implement communication for the parent-child process.

if (Ngx_add_channel_event (cycle, Ngx_channel, ngx_read_event,                              Ngx_channel_handler)   //Sub-process joins the channel to the Listener event , Ngx_channel_handler is the callback function for the event response        = = Ngx_error)    {

So how does the child process and the child process communicate directly?

In fact, the subprocess work_process directly communicates through this socket: The master parent process will tell the new process to the child process that was generated before each fork of a new process.

In the NGX_PROCESS_CYCLE.C

Ngx_pass_open_channel (ngx_cycle_t *cycle, ngx_channel_t *ch) {  ngx_int_t  i;  for (i = 0; i < ngx_last_process; i++) {    if (i = = Ngx_process_slot      | | ngx_processes[i].pid = = 1      | | ngx_pro Cesses[i].channel[0] = =-1)    {      continue;    }

The parameter ch contains the new subprocess PID, process information, and channel descriptors that were just created in the global array.

This way the process directly has its own information including the socket descriptor. This allows communication to be done with each other through this.

    • Related articles recommended:
    • Different nginx and Apache log formats
    • How to open Directory browsing with Apache and Nginx
    • Nginx php (PHP-FPM) upload large file settings
    • Nginx Run PHP program, return 200, but blank page
    • This article from: Hobby Linux Technology Network
    • This article link: http://www.ahlinux.com/nginx/9112.html

Nginx Source Analysis--nginx interprocess communication

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.