这是来自this question的。为什么touch要调用dup2() syscall?
$ > strace touch 1 2>&1 | tail
close(3) = 0
open("1", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
dup2(3, 0) = 0
close(3) = 0
utimensat(0, NULL, NULL, 0) = 0
close(0) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?
+++ exited with 0 +++发布于 2018-05-01 15:15:08
这是一件历史文物。
fd_reopen() +dup2()模式来自coreutils代码库中的几个程序使用的coreutils()函数。
在coreutils commit e373bb1之前,fd_reopen()没有执行open()+dup2(),而是在打开新的文件描述符之前关闭了所需的文件描述符。这就是当touch在coreutils commit 478bd89上开始使用这个功能时的情况。根据该提交消息,其目的是减少接触的文件描述符的数量。
https://stackoverflow.com/questions/40446555
复制相似问题