没有也没有。
您只需要小心并在您关心的所有文件描述符上设置 close-on-exec。
不过,设置起来很简单:
#include <fcntl.h>
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
#include <unistd.h>
/* please don't do this */
for (i = getdtablesize(); i --> 3;) {
if ((flags = fcntl(i, F_GETFD)) != -1)
fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
}
如果您运行的 Linux 内核 ≥2.6.23 且 glibc ≥2.7,open(以及其他类似的系统调用)接受一个新标志 O_CLOEXEC:
#include <unistd.h>
fd = open("...", ... | O_CLOEXEC);
如果您运行的 Linux 内核 ≥2.6.24 且 glibc ≥2.7,fcntl 接受一个新参数 F_DUPFD_CLOEXEC:
#include <fcntl.h>
newfd = fcntl(oldfd, F_DUPFD_CLOEXEC);
如果您运行的 Linux 内核 ≥2.6.27 且 glibc ≥2.9,则会有新的系统调用 pipe2、dup3 等,并且更多的系统调用会获得新的 *_CLOEXEC 标志:
#define _GNU_SOURCE
#include <unistd.h>
pipe2(pipefds, O_CLOEXEC);
dup3(oldfd, newfd, O_CLOEXEC);
注意POSIXspecifies那个
popen() 函数应确保在父进程中保持打开的先前 popen() 调用的任何流在新的子进程中关闭。 p>
所以,如果您担心 泄漏,请不要担心。