3.12 dup 不共享文件描述符属性
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/syscall.h>
#include <sys/times.h>
#include <unistd.h>
extern int errno;
void
err_sys (const char *msg)
{
perror (msg);
exit (errno);
}
int
main (int argc, char **argv)
{
int fd = open ("A.txt", O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if (fd == -1)
err_sys ("open");
int ret, fd2;
if ((ret = fcntl (fd, F_SETFD, FD_CLOEXEC)) == -1)
err_sys ("fcntl set fd failed");
fd2 = dup (fd);
if (fd2 == -1)
err_sys ("dup");
if ((ret = fcntl (fd, F_GETFD, FD_CLOEXEC)) == -1)
err_sys ("fcntl get fd failed");
printf ("ret=%d\n", ret);
if ((ret = fcntl (fd2, F_GETFD, FD_CLOEXEC)) == -1)
err_sys ("fcntl get fd2 failed");
printf ("ret=%d\n", ret);
}
结果:
ret=1
ret=0
FD_CLOEXEC
被清理掉了。