Nginx interprocess communication

43 篇文章 0 订阅

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值