gpt4 book ai didi

file - 为什么 touch 会调用 dup2() 系统调用?

转载 作者:行者123 更新时间:2023-12-01 04:51:59 24 4
gpt4 key购买 nike

这是脱落this question .为什么是 touch调用 dup2() 系统调用?

$ > 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 +++

最佳答案

是历史文物。

open()+dup2() 模式来自 fd_reopen() 函数,coreutils 代码库中的多个程序都使用该函数。

之前 coreutils commit e373bb1 , fd_reopen() 没有做 open()+dup2() ,而是在打开新的文件描述符之前关闭了所需的文件描述符。当 touch 在 coreutils commit 478bd89 开始使用此功能时就是这种情况。 .根据该提交消息,目的是减少 touch 将打开的文件描述符的数量。

关于file - 为什么 touch 会调用 dup2() 系统调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40446555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com