C-同时使用open()和fdopen()时关闭文件的正确方法 [英] C - Proper way to close files when using both open() and fdopen()

查看:120
本文介绍了C-同时使用open()和fdopen()时关闭文件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在用C构建Unix minishell,并正在实现输入,输出和err重定向,并且遇到了文件问题.我在找到重定向操作符的循环中打开文件,然后使用open()返回fd.然后,我相应地为孩子分配fd,并调用execute函数.

So I'm building a Unix minishell in C, and am implementing input, output, and err redirection, and have come across a problem with files. I open my files in a loop where I find redirection operators, and use open(), which returns an fd. I then assign the child's fd accordingly, and call an execute function.

当我的shell刚好外出并查找程序,并使用execvp()执行它们时,我没有太大的问题.唯一的问题是知道我是否需要在提示下一个命令行之前在文件描述符上调用close().我担心出现fd泄漏,但不完全了解它的工作原理.

When my shell is just going out and finding programs, and executing them with execvp(), I don't have much of a problem. The only problem is knowing whether or not I need to call close() on the file descriptors before prompting for the next command line. I'm worried about having an fd leak, but don't exactly understand how it works.

使用内置命令时,我真正的问题出现了.我有一个名为"read"的内置命令,它带有一个参数,即环境变量名称(可能是尚不存在的名称). Read然后提示输入值,并将该值分配给变量.这是一个示例:

My real problem arises when using builtin commands. I have a builtin command called "read", that takes one argument, an environmental variable name(could be one that doesn't yet exist). Read then prompts for a value, and assigns that value to the variable. Here's an example:

% read TESTVAR
test value test value test value
% echo  ${TESTVAR}
test value test value test value

好吧,我可以尝试这样的事情:

Well lets say that I try something like this:

% echo here's another test value > f1
% read TESTVAR < f1
% echo  ${TESTVAR}
here's another test value

这很好用,请记住,读取是在父进程内部执行的,我不使用execvp调用read,因为它是内置的.读取使用gets,它需要一个流变量,而不是fd.因此,在irc论坛上闲逛了一会之后,我被告知要使用fdopen从文件描述符中获取流.所以在打电话之前,我先打电话:

This works great, keep in mind that read executes inside the parent process, I don't call read with execvp since it's builtin. Read uses gets, which requires a stream variable, not an fd. So after poking around on the irc forums a bit I was told to use fdopen, to get the stream from the file descriptor. So before calling gets, I call:

rdStream = fdopen(inFD, "r");

然后打电话

if(fgets(buffer, envValLen, rdStream) != buffer) 
{
    if(inFD) fclose(rdStream);
    return -1;
}
if(inFD) fclose(rdStream);

如您所见,此刻我正在用fclose()关闭流,除非它等于stdin(它是0).这有必要吗?我需要关闭流吗?还是只是文件描述符?或两者?我对应该关闭哪个文件感到很困惑,因为它们都以不同的方式引用相同的文件.目前,我还没有关闭fd,但是我认为我绝对应该关闭.我希望有人能帮助确保我的外壳程序不会泄漏任何文件,因为我希望它能够在一个会话中执行数千个命令而不会泄漏内存.

As you can see, at the moment I'm closing the stream with fclose(), unless it is equal to stdin(which is 0). Is this necessary? Do I need to close the stream? Or just the file descriptor? Or both? I'm quite confused on which I should close, since they both refer to the same file, in a different manner. At the moment I'm not closing the fd, however I think that I definitely should. I would just like somebody to help make sure my shell isn't leaking any files, as I want it to be able to execute several thousand commands in a single session without leaking memory.

谢谢,如果你们要我发布更多代码,请问.

Thanks, if you guys want me to post anymore code just ask.

推荐答案

标准说:

fclose()函数必须在计算机上执行close() 的等效功能. 与由指向的流关联的文件描述符 流.

The fclose() function shall perform the equivalent of a close() on the file descriptor that is associated with the stream pointed to by stream.

因此,调用fclose就足够了;也会关闭描述符.

So calling fclose is enough; it will also close the descriptor.

这篇关于C-同时使用open()和fdopen()时关闭文件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆