OS 上没有 dup2 系统调用的最简单解决方法是什么?

What's the easiest workaround on the OS, which doesn't have the dup2 syscall?

我正在使用 xv6 OS,我需要用文件替换 stdio。这通常是通过 dup2 系统调用完成的,而 OS 没有。有什么解决方法吗?我是否必须实现自己的系统调用来尝试模仿 POSIX 的一种行为?或者我需要的功能(替换文件描述符)可以在 c 程序的常规函数​​中实现吗?谢谢

dup2( filedes, filedes2 ); 

表示

close( filedes2 );
fcntl( filedes, F_DUPFD, filedes2 );

你可以做到

close(0);
dup(fd);

受竞争条件影响,因为 dup2 原子函数但 close 和 dup 包括两个函数调用。