accept4和accept之间有什么区别

Mag*_*gic 4 sockets linux nginx

我看到了accept4系统支持时使用的nginx源代码.
accept4在Linux 2.6.28之后谷歌支持它.
accept4和accept之间有什么区别?

bro*_*oot 7

accept4是一个非标准的Linux扩展.真正的区别在于第4个参数(flags),它不存在于accept:

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int accept4(int sockfd, struct sockaddr *addr,socklen_t *addrlen, int flags);
Run Code Online (Sandbox Code Playgroud)

来自:接受手册页

如果flags为0,则accept4()与accept()相同.可以在标志中对以下值进行按位OR运算以获得不同的行为:

   SOCK_NONBLOCK   Set the O_NONBLOCK file status flag on the new open
                   file description.  Using this flag saves extra calls
                   to fcntl(2) to achieve the same result.

   SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new
                   file descriptor.  See the description of the
                   O_CLOEXEC flag in open(2) for reasons why this may be
                   useful.
Run Code Online (Sandbox Code Playgroud)

来自:打开手册页

默认情况下,新文件描述符设置为在execve(2)上保持打开(即,最初禁用fcntl(2)中描述的FD_CLOEXEC文件描述符标志); 下面描述的O_CLOEXEC标志可用于更改此默认值.

例如,通过使用此标志(SOCK_CLOEXEC),可以避免多线程程序中的竞争条件,其中可能导致open()返回的文件描述符被无意泄漏到由fork(2)创建的子进程执行的程序.