关闭后重新打开 stdout 和 stdin 文件描述符 [英] Re-opening stdout and stdin file descriptors after closing them
问题描述
我正在编写一个函数,给定一个参数,该函数要么将标准输出重定向到文件,要么从文件中读取标准输入.为此,我关闭了与 stdout 或 stdin 关联的文件描述符,以便当我打开文件时,它会在我刚刚关闭的描述符下打开.这有效,但问题是一旦完成,我需要将 stdout 和 stdin 恢复到它们真正应该的样子.
I'm writing a function, which, given an argument, will either redirect the stdout to a file or read the stdin from a file. To do this I close the file descriptor associated with the stdout or stdin, so that when I open the file it opens under the descriptor that I just closed. This works, but the problem is that once this is done, I need to restore the stdout and stdin to what they should really be.
我能为标准输出做的是 open("/dev/tty",O_WRONLY);但我不确定为什么会这样,更重要的是我不知道 stdin 的等效语句.
What I can do for stdout is open("/dev/tty",O_WRONLY); But I'm not sure why this works, and more importantly I don't know of an equivalent statement for stdin.
所以我有,对于标准输出
So I have, for stdout
close(1);
if (creat(filePath, O_RDWR) == -1)
{
exit(1);
}
和标准输入
close(0);
if (open(filePath, O_RDONLY) == -1)
{
exit(1);
}
推荐答案
您应该使用 dup() 和 dup2() 来克隆文件描述符.
You should use dup() and dup2() to clone a file descriptor.
int stdin_copy = dup(0);
int stdout_copy = dup(1);
close(0);
close(1);
int file1 = open(...);
int file2 = open(...);
< do your work. file1 and file2 must be 0 and 1, because open always returns lowest unused fd >
close(file1);
close(file2);
dup2(stdin_copy, 0);
dup2(stdout_copy, 1);
close(stdin_copy);
close(stdout_copy);
但是,您可能需要注意一个小细节(来自 man dup):
However, there's a minor detail you might want to be careful with (from man dup):
这两个描述符不共享文件描述符标志(close-on-execflag).close-on-exec 标志(FD_CLOEXEC;参见 fcntl(2))因为重复描述符已关闭.
The two descriptors do not share file descriptor flags (the close-on-execflag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.
如果这是一个问题,您可能需要恢复 close-on-exec 标志,可能使用 dup3() 而不是 dup2() 以避免竞争条件.
If this is a problem, you might have to restore the close-on-exec flag, possibly using dup3() instead of dup2() to avoid race conditions.
另外,请注意,如果您的程序是多线程的,其他线程可能会意外写入/读取您重新映射的 stdin/stdout.
Also, be aware that if your program is multi-threaded, other threads may accidentally write/read to your remapped stdin/stdout.
这篇关于关闭后重新打开 stdout 和 stdin 文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!