在socket创建的时候使用SOCK_CLOEXEC设置close-on-exec

查看手册man socket

 

NAME
       socket - create an endpoint for communication

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int socket(int domain, int type, int protocol);


 

       Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the bitwise OR of any of the follow‐
       ing values, to modify the behavior of socket():

       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.


在创建socket的时候在type字段或上SOCK_CLOEXEC

 

socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);

 

 

你可能感兴趣的:(linux,socket,SOCK_CLOEXEC)