Nginx inter-process communication-socketpair

Source: Internet
Author: User
: This article mainly introduces the inter-process nginx communication-socketpair. if you are interested in the PHP Tutorial, refer to it. In nginx, the master process and worker process use a full-duplex communication method-socketpair. After the socketpair function is successfully executed, a pair of connected socket pairs will be created. The two processes that communicate with each other use one of the sockets for read/write operations to implement communication between the two processes.

View the nginx source code. you can see that the following function creates socketpair.

Cycle (ngx_cycle_t * cycle, cycle proc, void * data, char * name, ngx_int_t respawn) {u_long on; ngx_pid_t pid; ngx_int_t s ;/....... omitted ....... /if (respawn! = NGX_PROCESS_DETACHED) {/* Solaris 9 still has no AF_LOCAL * // create socketpair if (socketpair (AF_UNIX, SOCK_STREAM, 0, ngx_processes [s]. channel) =-1) {ngx_log_error (NGX_LOG_ALERT, cycle-> log, ngx_errno, "socketpair () failed while spawning \" % s \ "", name ); return NGX_INVALID_PID;} // non-blocking if (ngx_nonblocking (ngx_processes [s]. channel [0]) =-1) {ngx_close_channel (ngx_processes [s]. channel, cycle-> lo G); return NGX_INVALID_PID;} // non-blocking if (ngx_nonblocking (ngx_processes [s]. channel [1]) =-1) {ngx_close_channel (ngx_processes [s]. channel, cycle-> log); return NGX_INVALID_PID;} // asynchronous on = 1; if (ioctl (ngx_processes [s]. channel [0], FIOASYNC, & on) =-1) {ngx_close_channel (ngx_processes [s]. channel, cycle-> log); return NGX_INVALID_PID;} // you can specify the process or group ID for receiving SIGIO or SIGURG event signals on the file description fd. If (fcntl (ngx_processes [s]. channel [0], F_SETOWN, ngx_pid) =-1) {ngx_close_channel (ngx_processes [s]. channel, cycle-> log); return NGX_INVALID_PID;} // Set close_on_exec. after a new process is created through the exec function family, if (fcntl (ngx_processes [s]. channel [0], F_SETFD, FD_CLOEXEC) =-1) {ngx_close_channel (ngx_processes [s]. channel, cycle-> log); return NGX_INVALID_PID;} // Set close_on_exec. after a new process is created through the exec function family, if (fcntl (ngx_processes [s]. channel [1], F_SETFD, FD_CLOEXEC) =-1) {ngx_close_channel (ngx_processes [s]. channel, cycle-> log); return NGX_INVALID_PID;} ngx_channel = ngx_processes [s]. channel [1];} else {ngx_processes [s]. channel [0] =-1; ngx_processes [s]. channel [1] =-1;} ngx_process_slot = s; pid = fork (); switch (pid) {case-1: ngx_close_channel (ngx_processes [s]. channel, cycle-> log); return NGX_INVALID_PID; case 0: // fork successful. the sub-process is created. at the same time, the related socket descriptor will also be copied with ngx_pid = ngx_getpid (); proc (cycle, data); break; default: break ;}/....... omitted ....... /return pid ;}

After fork is successful, the descriptor of the original process will also be copied. if this descriptor is no longer used in the fork process, we need to close it in time.

If we use the fork-> exec function family to create a new process, we can use a better method to ensure that the original descriptor is properly disabled to avoid resource leakage. That is, the above code calls the fcntl (FD_CLOEXEC) function on the socket and sets the socket attribute: When the exec function family is called, the socket will be automatically closed. This method can be used to set the FD_CLOEXEC attribute immediately after the socket is created, avoiding the operation of manually disabling the relevant socket before the exec creation process, this is especially useful when a large number of descriptors are created and managed.

Most of the time, we use the following method for operations:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     int main(){    pid_t  pid;    int    fds[2];    int    valRead, valWrite;    if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, fds))    {        return 0;    }    pid = fork();    if (0 == pid)    {        pid = getpid();        printf("[%d]-child process start", pid);        close(fds[0]);                //read write on fds[1]        write(fds[1], &valWrite, sizeof(valWrite));         read(fds[1], &valRead, sizeof(valRead));    }    else if (0 < pid)    {        pid = getpid();        printf("[%d]-parent process continue", pid);        close(fds[1]);        //read write on fds[0]        read(fds[0], &valRead, sizeof(valRead));        write(fds[0], &valWrite, sizeof(valWrite));     }    else    {        printf("%s", "fork failed");    }    return 0;}
    
   
  
 

As you can see, before fork, the current process created a pair of sockets, that is, socketpair. The socket can be regarded as a connection established between the fds [0] on the server side and the fds [1] on the client side through fds [0] and fds [1, we can complete full duplex communication.

After fork is executed, a sub-process is created. In a child process, the socketpair created by the parent process is also copied as fds and stored in the child process. The parent process continues to run. At this time, the same socketpair exists in the parent and child processes.

Imagine that we write data to fds [0] in the main process, and the data will be read on fds '[1] in the child process, in this way, the communication between the parent process and the child process is realized. Of course, write data on the main process fds [1] and read the written data on the child process fds '[0. In actual use, we only need to keep a pair of sockets for communication. The other two sockets can be disabled in the parent process and child process respectively.

Of course, if we can pass a socket in fds to another process in some way, we can also implement inter-process communication between socketpair.


The above introduces the inter-process nginx communication-socketpair, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.