dup, dup2一图胜千言
来源:互联网 发布:多角色数据库设计 编辑:程序博客网 时间:2024/06/08 18:18
一图胜千言啊,看了必须懂啊^_^
下面这一段原文:http://os.chinaunix.net/a2009/0602/1049/000001049422_7.shtml
在C语言里,操纵文件的渠道则是FILE结构,不难想象,C语言中的FILE结构必定和fd有一对一的关系,每个FILE结构都会记录自己唯一对应的fd。
FILE、fd、打开文件表和打开文件对象的关系如图11-4所示。
图11-4中,内核指针p指向该进程的打开文件表,所以只要有fd,就可以用fd+p来得到打开文件表的某一项地址。stdin、stdout、stderr均是FILE结构的指针。
对于Windows中的句柄,与Linux中的fd大同小异,不过Windows的句柄并不是打开文件表的下标,而是其下标经过某种线性变换之后的结果。
在大致了解了I/O为何物之后,我们就能知道I/O初始化的职责是什么了。首先I/O初始化函数需要在用户空间中建立stdin、stdout、stderr及其对应的FILE结构,使得程序进入main之后可以直接使用printf、scanf等函数。
下面这一段原文:http://www.01happy.com/c-dup-dup2/
C语言中dup和dup2函数的不同和使用
下面这段是CSDN下载的:
上面好像吵得很凶,下面的描述对于所有POSIX兼容的操作系统和ANSI兼容的C编译器都正确,别吵了,:)
open和close是操作系统开放的两个系统调用接口,也就是说,进行这两个调用后,进程会进入内核态,由内核完成文件系统的操作任务。
要注意的是,所有的系统调用其实都是二进制接口,而不是C语言接口,相关的转换工作由编译器完成(也有的是用库来封装,但是不推荐),也就是说,编译器会识别系统调用,将其封装为二进制的编码。
这两个系统调用都是posix标准规定的,并且是“无缓冲”文件IO,也就是说,文件读写直接针对文件系统(注意,我可没有说“直接针对物理文件”,因为文件系统往往是有自己的缓冲的)。
还有,这两个函数要求参数是“文件描述符”,也就是一个非负整数,类型是int,这一点 guyanhun(天涯-明月 C++从头开始) 说的很正确。
至于fopen和fclose是ANSI C的标准,注意拉,上面那个是操作系统标准,这个是编程语言的标准,也就是说,c的这两个函数“应该”能够在所有操作系统和文件系统上运行,而不依赖于文件系统的物理介质。
这样的话,c就需要对文件操作建立一个单独的缓冲,因为可能有些文件系统就是需要这个东西(比如网络文件系统,必须将要写的数据打包,然后才能发出去,在我们常见的文件系统上其实不是必须的)。
刚好,既然缓冲有不少好处(例如,可以不用关心每次读写多大的数据块),因此就被充分的用起来啦,不过其实C语言标准也没有说“必须要缓冲”,所以写代码的时候你也不用做这个假设。
fopen和fclose会给每个打开的文件建立文件描述结构,也就是我们见到的FILE结构。这一点与上面的两个不同,因为使用整数做文件描述符是POSIX标准规定的。万一碰上其他标准的操作系统咋办?所以C语言就建立了自己文件描述结构。
以下一段来自水木清华论坛:http://www.newsmth.net/nForum/#!article/LinuxDev/9513
fd = open(...);
fp = fdopen(fd,"r");
....
这里需要fclose(fp)吗?
close(fd);
these operations are equal, i think the only different aspect is buffer.
不是吧,以前看一本数说不应该fclose,否则会关闭open打开的fd
【 在 Soaris (Ok Computer) 的大作中提到: 】
: 如果不fclose会有内存泄漏
不fclose是不行的,一是FILE里的buffered output没有flush,
二是FILE本身可能分配了buffer,需要用fclose释放。
当然,调用了fclose就不用close了。
open是glibc封装的系统调用
fopen是stdio提供的接口,封装了open
初级,可能翻译成基础比较好,基本是系统调用。
而FILE是buffer了的,读写速度快点。
称作是标准I/O我想是因为它标准c要求的吧。
View this article only
网上论坛:comp.unix.programmer
日期:1992-07-20 19:59:12 PST
volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe) writes:
>Folks-
> I'm looking for advice on the following scenario:
>
> My routine is handed a file descriptor opened and managed by some
> calling routine. (The descriptor actually corresponds to a connected
> internet-domain socket, but hopefully that will not be relevant.) In my
> routine, I would like to do some stdio-type operations (e.g. fprintf) on
> the descriptor, so I open a stream with `stream=fdopen(fd,"w");'. Now,
> before I return to the caller, I want to free up the stream since I no
> longer need it, and since this routine may be called may times with
> the same descriptor or different descriptors, and I don't want to
> run out of streams after doing too many fdopens. However, I don't want
> to disturb the underlying descriptor, so I can't do a "fclose(stream)".
> Is there some way to "undo" an fdopen call? Something like an "fdclose"
> call that might close the stream without closing the descriptor?
You could dup() the file descriptor at the beginning of your routine,
then associate this duplicated fd with a stream via fdopen(). At the
end of your routine you can fclose() this stream without closing the
socket connection because the original fd still holds it open. So
after this you can continue to write to the socket from elsewhere
in your program via the original fd that was before passed to your
routine.
Hope that helps.
Uwe
原文:http://bytes.com/topic/c/answers/811133-temporarily-close-stdout
The dup() approach with /dev/fd would be something like (not tested):Worked like charm :-)
* * char buf[20];
* * int saved_stdout = dup(1);
* * freopen("/dev/null", "w", stdout);
* * lsb_submit( &request , &reply );
* * sprintf(buf, "/dev/fd/%d", saved_stdout);
* * freopen(buf, "w", stdout);
Joakim
- dup, dup2一图胜千言
- dup2()&&dup()
- dup/dup2
- dup、dup2
- dup dup2
- dup,dup2
- dup, dup2
- dup()、dup2()
- dup & dup2
- 理解dup和dup2
- 理解dup和dup2
- dup and dup2
- 理解dup和dup2
- 理解dup和dup2
- dup2和dup
- dup和dup2函数
- 解惑dup、dup2
- 理解dup和dup2
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- 百度蜘蛛IP分析
- java中判断字符编码的格式以及转码
- wx.ListBox创建列表框
- EasyUI的增删查改(后台ASP.NET)
- dup, dup2一图胜千言
- 1.1系统的安装
- c# cad中插入另一个dwg的图块
- camshift跟踪算法介绍
- 数学小模版
- c# 开发《Id扫描录入系统》总结
- Windows常用的批处理
- mac os 苹果操作系统如何切换输入法
- shell 之 seq 命令