Nginx interprocess communication

http://www.programering.com/a/MjN1kjMwATM.html


Nginx interprocess communication


Linux under the IPC, nginx processes are related to their communication process, we choose the TCP socket communication. TCP socket used to do process communication advantages, 1.socket is a file descriptor, simple operation. 2 bidirectional flow. 3 another important benefit: records can reproduce, we can use tcpdump to capture information, convenient debugging. 

Of course to process large amounts of data sharing come very naturally we use shared memory.

Using socketpair () function to create anonymous socket master process (the parent) and work process (process) and the communication between work and process.

On the socketpair ()Here you can.

Have a look first to define nginx process

ngx_process.h


typedef struct {
    ngx_pid_t           pid;
    int                 status;
    ngx_socket_t        channel[2];//This is used to two descriptor socketpair

    ngx_spawn_proc_pt   proc;
    void               *data;

In nginx_process.c


ngx_pid_t
ngx_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 that 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 fork, First call socketpair () to create a socket descriptor variables in ngx_process[s].channel, In the fork (after), The child process inherits the parent process socket descriptor, Nginx will now put to the parent process using channel[0], The use of channel[1] to the child process. Communication from this can realize the process of. 

    if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
                              ngx_channel_handler)   //The child process adds channel to listen for events, the callback function ngx_channel_handler as an incident response
        == NGX_ERROR)
    {



Then the child process and the child process is directly how communications?

In fact, the child process between work_process and these socket communication: Master parent fork each time a new process will bring this new process information has been generated above the child process

In 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_processes[i].channel[0] == -1)
        {
            continue;
        }

Parameter ch contains just create new sub process PID, process information, the channel descriptor in the global array. 

But up to now, Nginx is not the child process to write information to the parent process, communication between processes is of no practical use.

This process directly to have their own information including the socket descriptor. So can each other can complete communication through this.

Reference book: "in-depth analysis of Nginx> Gao Qunkai


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章