accept4 is a non-standard linux extension.The real difference is in the 4th argument (
flags) which is not present in
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); int accept4(int sockfd, struct sockaddr *addr,socklen_t *addrlen, int flags);
From: accept man page
If flags is 0, then accept4() is the same as accept(). The following values can be bitwise ORed in flags to obtain different behavior:
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.
And from: open man page
By default, the new file descriptor is set to remain open across an execve(2) (i.e., the FD_CLOEXEC file descriptor flag described in fcntl(2) is initially disabled); the O_CLOEXEC flag, described below, can be used to change this default.
And for eg, by using this flag (
SOCK_CLOEXEC), one can avoid race conditions in multithreaded programs where it may lead to the file descriptor returned by open() being unintentionally leaked to the program executed by the child process created