免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2234 | 回复: 6
打印 上一主题 下一主题

dup使得四个文件描述符指向一个文件表项,为什么必须关掉一个呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-17 21:01 |只看该作者 |倒序浏览
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
if (fd >2)
  close(fd);
答案是这么说的,可是书的章节内容怎么没提到呢?

不好意思 ,写了很多的错误,

不过能告诉我为什么非要关掉,我要四个文件描述符指向一个文件表项难道不可以么?

[ 本帖最后由 yangbo8481 于 2006-10-17 21:54 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-10-17 21:05 |只看该作者
fp 是什么?和 fd 有什么关系?

论坛徽章:
0
3 [报告]
发表于 2006-10-17 21:11 |只看该作者

回复 2楼 langue 的帖子

不好意思,写错了, fp是fd,fd是file descripter ,open返回的整数值

论坛徽章:
0
4 [报告]
发表于 2006-10-17 21:44 |只看该作者
另外,你一定记错了。dup() 只接受一个参数。前面有个朋友问过这个问题。

参照 dup(2):

DUP(2)                    OpenBSD Programmer's Manual                   DUP(2)

NAME
     dup, dup2 - duplicate an existing file descriptor

SYNOPSIS
     #include <unistd.h>

     int
     dup(int oldd);

     int
     dup2(int oldd, int newd);

DESCRIPTION
     dup() duplicates an existing object descriptor and returns its value to
     the calling process (newd = dup(oldd)). The argument oldd is a small non-
     negative integer index in the per-process descriptor table.  The value
     must be less than the size of the table, which is returned by
     getdtablesize(3). The new descriptor returned by the call is the lowest
     numbered descriptor currently not in use by the process.

     The object referenced by the descriptor does not distinguish between oldd
     and newd in any way.  Thus if newd and oldd are duplicate references to
     an open file, read(2), write(2) and lseek(2) calls all move a single
     pointer into the file, and append mode, non-blocking I/O and asynchronous
     I/O options are shared between the references.  If a separate pointer in-
     to the file is desired, a different object reference to the file must be
     obtained by issuing an additional open(2) call.  The close-on-exec flag
     on the new file descriptor is unset.

     In dup2(), the value of the new descriptor newd is specified.  If this
     descriptor is already in use, it is first deallocated as if a close(2)
     call had been done first.

RETURN VALUES
     The value -1 is returned if an error occurs in either call.  The external
     variable errno indicates the cause of the error.

ERRORS
     dup() and dup2() fail if:

     [EBADF]       oldd or newd is not a valid active descriptor

     [EMFILE]      Too many descriptors are active.

SEE ALSO
     accept(2), close(2), fcntl(2), open(2), pipe(2), socket(2),
     socketpair(2), getdtablesize(3)

STANDARDS
     dup() and dup2() are expected to conform to IEEE Std1003.1-1988
     (``POSIX'').


[ 本帖最后由 langue 于 2006-10-17 21:49 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2006-10-17 23:53 |只看该作者
LZ说的应该是dup2

论坛徽章:
0
6 [报告]
发表于 2006-10-18 08:46 |只看该作者
0
1
2

系统默认的标准文件输入/输出 错误输出

论坛徽章:
0
7 [报告]
发表于 2006-10-18 10:20 |只看该作者
不是非关不可。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP