开发者

So sockets generated by socketpair() are availble across different processes?

开发者 https://www.devze.com 2023-03-09 20:02 出处:网络
As we know fd(file descriptor,an int to be exact) is per process,that is,the same file opened in different processes may have different fd.

As we know fd(file descriptor,an int to be exact) is per process,that is,the same file opened in different processes may have different fd.

And I thought so should be for sockets.

But when reading nginx source code I found it's using sockets to communicate between processes:

    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;
    }

Here ngx_processes[s].channel[0] is sent to other process.

But as I said fd is per process,how can it ensure that the same int will point to the same socket?

UPDATE

Why question is now how this works(it's the same way that nginx u开发者_StackOverflowses)?

http://swtch.com/usr/local/plan9/src/lib9/sendfd.c


nginx uses unix domain sockets ancillary messages (specifically, the SCM_RIGHTS message, see the man page for the unix protocol for more information on this) to pass file descriptors around.

When you receive an SCM_RIGHTS message, the kernel basically gives you a duplicate (as in dup) file descriptor, valid in the receiving process. This fd may or may not have the same number, which matters very little as the receiving side should use the contents of the message and not some prior knowledge.

0

精彩评论

暂无评论...
验证码 换一张
取 消